Sitelet https://github.com/starkware-libs/cairo/pull/10094
Skip to content

feat(defs): populate MacroPluginMetadata::allowed_features - #10094

Merged
orizi merged 1 commit into
mainfrom
orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features
Jun 16, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features

Conversation

@orizi

@orizi orizi commented Jun 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

MacroPluginMetadata::allowed_features now reflects the full set of features in scope for each item: the #[feature("...")] attributes declared on the item itself plus those inherited from all ancestor modules up to the crate root.

Previously, allowed_features was always an empty Default::default(), meaning macro plugins had no visibility into which features were enabled for a given item. Now, module_allowed_features (a memoized #[salsa::tracked] function) walks the module hierarchy once per ModuleId and accumulates #[feature] attributes from each submodule declaration. Per-item, extract_allowed_features collects the item's own #[feature] attributes and merges them with the module-level set. When an item adds no features of its own, the module set is reused directly to avoid a per-item allocation.

A new test (test_allowed_features_accumulated_with_item) verifies the four cases: no features, module-only features, item-only features, and both combined.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

Macro plugins receive a MacroPluginMetadata struct that is supposed to communicate which features are allowed in scope. The allowed_features field was always empty, so plugins could not correctly gate behavior on feature availability. This made the feature-gating mechanism non-functional at the macro expansion level.


What was the behavior or documentation before?

allowed_features in MacroPluginMetadata was always an empty set, regardless of any #[feature("...")] attributes present on the item or its enclosing modules.


What is the behavior or documentation after?

allowed_features contains the union of #[feature("...")] attributes from the item itself and all of its ancestor modules. Module-level feature sets are memoized per ModuleId so the ancestor walk is not repeated for every item in the same module.


Related issue or discussion (if any)

Resolves the TODO(orizi): Actually extract the allowed features per module. comment that was previously left in module_sub_files.


Additional context

The module_allowed_features function handles all three ModuleId variants: CrateRoot returns an empty set, Submodule extracts features from the submodule declaration node and merges with the parent's set, and MacroCall delegates directly to its parent module's set.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator Author

@orizi
orizi marked this pull request as ready for review June 14, 2026 11:33
@cursor

cursor Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes macro-expansion feature gating and semantic module feature inheritance; mis-scoping could silence or wrongly emit feature-gated diagnostics, but scope is compiler internals with targeted tests.

Overview
Macro plugins now receive a non-empty MacroPluginMetadata::allowed_features: inherited #[feature("...")] from ancestor modules (memoized via module_allowed_features) unioned with each item’s own attributes before generate_code runs.

Semantic module_feature_config is refactored to the same parent-walk shape and now reads #[feature] / #[allow] on item-scope macro call AST nodes, so features on calls like #[feature(...)] make_const!(); apply to expanded code (covered by new inline-macro tests for consteval_int! deprecation).

A defs integration test asserts module-only, item-only, and combined feature sets via a reporting plugin.

Reviewed by Cursor Bugbot for commit 61f97b4. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@eytan-starkware reviewed 2 files and all commit messages, and made 2 comments.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).


crates/cairo-lang-defs/src/db.rs line 1505 at r1 (raw file):

        }
        ModuleId::MacroCall { id, .. } => {
            module_allowed_features(db, (), id.parent_module(db)).clone()

We should consider supporting #[feature(...)] macro_creating_items!() which to my understanding will not work right now


crates/cairo-lang-defs/src/test.rs line 495 at r1 (raw file):

                #[feature("inner4")]
                fn f4() {}
            }

Add nested modules and maybe a macro call inside the function

@orizi
orizi force-pushed the orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features branch from 30760d2 to a25d6f4 Compare June 15, 2026 16:18

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a25d6f4. Configure here.

Comment thread crates/cairo-lang-defs/src/db.rs

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 2 comments.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-defs/src/db.rs line 1505 at r1 (raw file):

Previously, eytan-starkware wrote…

We should consider supporting #[feature(...)] macro_creating_items!() which to my understanding will not work right now

Done


crates/cairo-lang-defs/src/test.rs line 495 at r1 (raw file):

Previously, eytan-starkware wrote…

Add nested modules and maybe a macro call inside the function

macro-call isn't actually active here - but added the rest.

`module_sub_files` previously passed an empty `allowed_features` set to every
  macro plugin (a `TODO`), so item-level macro plugins could not gate behavior on
  `#[feature("...")]` the way inline-macro plugins already can.

  Extract the features in scope for each item — the `#[feature(...)]` attributes on
  the item plus those on its enclosing modules (walked via `module_allowed_features`,
  reading the `mod` declarations straight from the syntax tree to avoid a
  `module_data` -> `module_sub_files` query cycle) — and pass them in the metadata.

  No behavior change today: no in-tree plugin reads `allowed_features` at the defs
  layer yet. Adds a `defs` unit test covering the empty, item-level, module-scope,
  and accumulated cases.
@orizi
orizi force-pushed the orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features branch from a25d6f4 to 61f97b4 Compare June 16, 2026 07:00

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

@eytan-starkware reviewed 4 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jun 16, 2026
Merged via the queue into main with commit 4409061 Jun 16, 2026
55 checks passed
@orizi
orizi deleted the orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features branch June 16, 2026 11:01
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.

3 participants