-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experimental): Implement comptime mut globals (#4926)
# Description ## Problem\* Resolves #4917 ## Summary\* Implements mutable comptime globals ## Additional Context Changes should be relatively straightforward - mostly parser changes to add parsing `mut` to globals, resolution changes to track this and error if the global is not also comptime, etc. The one bug that was fixed is that globals were being re-evaluated in the interpreter each time which would reset their value whenever they were referenced. This has been fixed with a lookup check. ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [x] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
- Loading branch information
1 parent
59861cf
commit dc90de9
Showing
12 changed files
with
114 additions
and
15 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
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/comptime_mut_global/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_mut_global" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.28.0" | ||
|
||
[dependencies] |
30 changes: 30 additions & 0 deletions
30
test_programs/compile_success_empty/comptime_mut_global/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,30 @@ | ||
comptime mut global COUNTER: Field = 0; | ||
|
||
comptime fn get_unique_id() -> Field { | ||
let id = COUNTER; | ||
COUNTER += 1; | ||
id | ||
} | ||
|
||
fn id1() -> Field { | ||
comptime | ||
{ | ||
get_unique_id() | ||
} | ||
} | ||
|
||
fn id2() -> Field { | ||
comptime | ||
{ | ||
get_unique_id() | ||
} | ||
} | ||
|
||
fn main() { | ||
// Order of comptime evaluation between functions isn't guaranteed | ||
// so we don't know if (id1 == 0 && id2 == 1) or if (id1 == 1 && id2 == 0). | ||
// we only know they are not equal | ||
let id1 = id1(); | ||
let id2 = id2(); | ||
assert(id1 != id2); | ||
} |