Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutable comptime globals #4917

Closed
Tracked by #4594
jfecher opened this issue Apr 24, 2024 · 1 comment
Closed
Tracked by #4594

Mutable comptime globals #4917

jfecher opened this issue Apr 24, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@jfecher
Copy link
Contributor

jfecher commented Apr 24, 2024

Problem

comptime globals should also be mutable - unlike runtime globals currently.

Happy Case

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);
}

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@jfecher jfecher added the enhancement New feature or request label Apr 24, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Apr 24, 2024
jfecher added a commit that referenced this issue Apr 29, 2024
# 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>
@jfecher
Copy link
Contributor Author

jfecher commented May 30, 2024

Implemented by #4926

@jfecher jfecher closed this as completed May 30, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: ✅ Done
Development

No branches or pull requests

1 participant