-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metaprogramming): Add
#[use_callers_scope]
(#6050)
# Description ## Problem\* We have many methods now such as `Quoted::as_type` or `Quoted::as_trait_constraint` which resolve items in the current scope. It is useful to change the scope these are resolved in but somewhat impractical to do what we did with `Expression::resolve` and have them all take module parameters. ## Summary\* I've added `#[use_callers_scope]` which can be added to a comptime function to change the scope any builtins resolve in to that of the caller. This also works in helper functions and closures. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [x] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information
Showing
8 changed files
with
122 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/use_callers_scope/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "use_callers_scope" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.34.0" | ||
|
||
[dependencies] |
34 changes: 34 additions & 0 deletions
34
test_programs/compile_success_empty/use_callers_scope/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#[bar::struct_attr] | ||
struct Foo {} | ||
|
||
struct Bar {} | ||
|
||
#[bar::fn_attr] | ||
fn main() {} | ||
|
||
mod bar { | ||
#[use_callers_scope] | ||
pub comptime fn struct_attr(_: StructDefinition) { | ||
let _ = quote { Bar }.as_type(); | ||
} | ||
|
||
#[use_callers_scope] | ||
pub comptime fn fn_attr(_: FunctionDefinition) { | ||
let _ = quote { Bar }.as_type(); | ||
let _ = nested(); | ||
|
||
// Ensure closures can still access Bar even | ||
// though `map` separates them from `fn_attr`. | ||
let _ = &[1, 2, 3].map( | ||
|_| { | ||
quote { Bar }.as_type() | ||
} | ||
); | ||
} | ||
|
||
// use_callers_scope should also work nested | ||
#[use_callers_scope] | ||
comptime fn nested() -> Type { | ||
quote { Bar }.as_type() | ||
} | ||
} |