Skip to content

Refactor: AST Scope - #450

Merged
LesterEvSe merged 4 commits into
BlockstreamResearch:masterfrom
LesterEvSe:refactor/ast-scope
Oct 23, 2026
Merged

LesterEvSe merged 4 commits into
BlockstreamResearch:masterfrom
LesterEvSe:refactor/ast-scope

Conversation

@LesterEvSe

Copy link
Copy Markdown
Collaborator

Scope has grown too large and handles many unrelated responsibilities at once:

/// Scope for generating the abstract syntax tree.
///
/// The scope is used for:
/// 1. Assigning types to each variable
/// 2. Resolving type aliases
/// 3. Assigning types to each witness expression
/// 4. Resolving calls to custom functions
pub(super) struct Scope {
    /// Current position in the module tree. Push on `mod` enter, pop on exit.
    /// Empty path means we are at the root (main file) scope.
    pub(super) module_path: Vec<ModuleName>,

    /// Global scope where items from the main file that live at the root level.
    root: ModuleScope,

    /// Block-level variable scopes. Push on block enter, pop on block exit.
    variables: Vec<HashMap<Identifier, ResolvedType>>,
    parameters: HashMap<TemplateProgramWitness, ResolvedType>,
    witnesses: HashMap<TemplateProgramWitness, ResolvedType>,
    /// Allow enum constructions to name an enum by its declared name even
    /// when that name is not an alias in scope. Enabled only for value
    /// parsing (witness and argument files), which runs without a scope.
    pub(super) unscoped_enum_names: bool,
    is_main: bool,
    call_tracker: CallTracker,
    pub(super) jet_hinter: Box<dyn JetHinter>,
    /// Errors reported during analysis that did not stop it.
    pub(super) diagnostics: Vec<Diagnostic>,
}

This PR splits it into several smaller structs and replaces the pub(super) fields with getters.

Scope is now much simpler:

/// Scope for generating the abstract syntax tree.
///
/// The scope is used for:
/// 1. Assigning types to each variable
/// 2. Resolving type aliases
/// 3. Assigning types to each witness expression
/// 4. Resolving calls to custom functions
pub(super) struct Scope {
    modules: ModuleTree,
    locals: Locals,
    parts: ProgramParts,

    is_main: bool,
    /// Allow enum constructions to name an enum by its declared name even
    /// when that name is not an alias in scope. Enabled only for value
    /// parsing (witness and argument files), which runs without a scope.
    unscoped_enum_names: bool,
    jet_hinter: Box<dyn JetHinter>,
    /// Errors reported during analysis that did not stop it.
    diagnostics: Vec<Diagnostic>,
}

Checklist

  • New or changed language features are used in fixtures/kitchen_sink.simf (see its header).

@LesterEvSe
LesterEvSe requested a review from KyrylR October 22, 2026 13:42
@LesterEvSe LesterEvSe self-assigned this Oct 22, 2026
@LesterEvSe
LesterEvSe requested a review from delta1 as a code owner October 22, 2026 13:42

@KyrylR KyrylR left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 258e027; successfully ran local tests

@LesterEvSe
LesterEvSe requested a review from KyrylR October 22, 2026 15:24
@LesterEvSe

Copy link
Copy Markdown
Collaborator Author

Polished some code after that

@KyrylR KyrylR left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK cafba5e; successfully ran local tests

Ran tests and CI on every commit

@LesterEvSe
LesterEvSe merged commit e513a0e into BlockstreamResearch:master Oct 23, 2026
14 checks passed
@LesterEvSe

Copy link
Copy Markdown
Collaborator Author

Kyrylo previously suggested some minor improvements to the AST, such as replacing pub(super) fields with getters. I also noticed that Scope could be split into several smaller structs.
It has no breaking changes, so we merged it

@LesterEvSe
LesterEvSe deleted the refactor/ast-scope branch October 23, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants