forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add some
Module
comptime functions (noir-lang#5684)
# Description ## Problem Part of noir-lang#5668 ## Summary Adds these comptime functions: - `Quoted::as_module` - `Module::name` - `Module::is_contract` - `Module::functions` - `FunctionDefinition::name` ## Additional Context Some notes: - I made `Quoted::as_module` return `Option<ModuleId>`, while other `Quoted::as_...` methods currently return a non-Option instead. Eventually it would be nice to unify these but doing that in this PR would have made it much bigger and complex, mainly because those methods rely on methods that are used in many places in the compiler. - For now I made `Module::name` and `FunctionDefinition::name` return `Quoted`. Once we have a `String` type we could change them to `String`, and the logic from going to Quoted -> String is very simple. I was wondering... would a `String` type be what a Slice is for Arrays? One more thought: I wonder if instead of returning `Option<ModuleId>` it wouldn't be better to return some kind of `Result`. That way the caller would know what the error is, if it happens. But I think we don't have `Result` yet in Noir, right? ## Documentation\* Check one: - [x] No documentation needed. - [ ] 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. --------- Co-authored-by: jfecher <[email protected]>
- Loading branch information
Showing
14 changed files
with
197 additions
and
18 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
impl Module { | ||
#[builtin(module_is_contract)] | ||
fn is_contract(self) -> bool {} | ||
|
||
#[builtin(module_functions)] | ||
fn functions(self) -> [FunctionDefinition] {} | ||
|
||
#[builtin(module_name)] | ||
fn name(self) -> Quoted {} | ||
} |
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/comptime_module/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 = "comptime_module" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.31.0" | ||
|
||
[dependencies] |
26 changes: 26 additions & 0 deletions
26
test_programs/compile_success_empty/comptime_module/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,26 @@ | ||
mod foo { | ||
fn x() {} | ||
fn y() {} | ||
} | ||
|
||
contract bar {} | ||
|
||
fn main() { | ||
comptime | ||
{ | ||
// Check Module::is_contract | ||
let foo = quote { foo }.as_module().unwrap(); | ||
assert(!foo.is_contract()); | ||
|
||
let bar = quote { bar }.as_module().unwrap(); | ||
assert(bar.is_contract()); | ||
|
||
// Check Module::functions | ||
assert_eq(foo.functions().len(), 2); | ||
assert_eq(bar.functions().len(), 0); | ||
|
||
// Check Module::name | ||
assert_eq(foo.name(), quote { foo }); | ||
assert_eq(bar.name(), quote { bar }); | ||
} | ||
} |