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 treated as constants #5991

Closed
jfecher opened this issue Sep 10, 2024 · 0 comments · Fixed by #5995
Closed

Mutable comptime globals treated as constants #5991

jfecher opened this issue Sep 10, 2024 · 0 comments · Fixed by #5995
Assignees
Labels
bug Something isn't working

Comments

@jfecher
Copy link
Contributor

jfecher commented Sep 10, 2024

Aim

comptime mut global COUNTER = 0;

fn main() {
    comptime { increment() };
    comptime { increment() };
    
    assert_eq(get_counter(), 2);
}

fn get_counter() -> Field {
    COUNTER
}

comptime fn increment() {
    COUNTER += 1;

    let expected = get_counter();
    let actual = COUNTER;
    assert_eq(get_counter(), COUNTER, f"{expected} = {actual}");
}

Expected Behavior

The program to execute without error

Bug

Fails with:

error: 1 = 2
   ┌─ src/main.nr:5:16
   │
 5 │     comptime { increment() };
   │                -----------
   ·
19 │     assert_eq(get_counter(), COUNTER, f"{expected} = {actual}");
   │     ----------------------------------------------------------- Assertion failed
   │

Because get_counter is first elaborated within the comptime increment call, the value for COUNTER within it gets locked in as a constant - the current value of that constant which is 1 when it is first called. When it is later mutated and get_counter is called again, the value was inlined as a constant so the value is still 1 when it should be 2.

To Reproduce

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

No response

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@jfecher jfecher added the bug Something isn't working label Sep 10, 2024
@jfecher jfecher self-assigned this Sep 10, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 10, 2024
github-merge-queue bot pushed a commit that referenced this issue Sep 11, 2024
# Description

## Problem\*

Resolves #5991

## Summary\*

Fixes two issues:
1. The original linked issue where elaborating a function early would
cause any globals to be "locked in" in a sense.
2. The fact that we were never actually mutating underlying values in
the interpreter.

## Additional Context



## 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.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant