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

comptime interpreter handles scopes incorrectly when calling builtin/foreign/oracle functions #5274

Closed
TomAFrench opened this issue Jun 18, 2024 · 4 comments · Fixed by #5276
Labels
bug Something isn't working

Comments

@TomAFrench
Copy link
Member

The below program compiles just fine

fn main() {
    comptime {
        let ws: [Field; 1] = [1];
        let ws_as_slice: [Field] = ws.as_slice();

        assert_eq(ws_as_slice[0], 1);
    }
}

however if I change the assertion to act on ws rather than ws_as_slice then the program fails to compile with the error message below.

fn main() {
    comptime {
        let ws: [Field; 1] = [1];
        let ws_as_slice: [Field] = ws.as_slice();

        assert_eq(ws[0], 1);
    }
}
$ nargo compile
warning: unused variable ws_as_slice
  ┌─ /home/tom/Programming/aztec/noir/test_programs/compile_success_empty/comptime_as_slice/src/main.nr:4:13
  │
4 │         let ws_as_slice: [Field] = ws.as_slice();
  │             ----------- unused variable 
  │

error: Non-comptime variable `ws` referenced in comptime code
  ┌─ /home/tom/Programming/aztec/noir/test_programs/compile_success_empty/comptime_as_slice/src/main.nr:6:19
  │
6 │         assert_eq(ws[0], 1);
  │                   -- Non-comptime variables can't be used in comptime code
  │

@TomAFrench TomAFrench added the bug Something isn't working label Jun 18, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jun 18, 2024
@TomAFrench
Copy link
Member Author

Actually, this seems to be specific to as_slice.

@TomAFrench TomAFrench closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jun 18, 2024
@jfecher
Copy link
Contributor

jfecher commented Jun 18, 2024

@TomAFrench It is also because of the fact that any time a variable is not in scope the interpreter assumes the variable is not comptime. I'm not sure why it'd be happening here but we should probably make that error more general.

@TomAFrench TomAFrench reopened this Jun 18, 2024
@TomAFrench TomAFrench changed the title comptime interpreter appears to "forget" variables are comptime once used comptime interpreter handles scopes incorrectly when calling builtin/foreign/oracle functions Jun 18, 2024
@TomAFrench
Copy link
Member Author

I've fixed this issue in #5276. We were performing an early return in the situation where we hit a builtin/foreign/oracle function so we missed the call to exit_function

@jfecher
Copy link
Contributor

jfecher commented Jun 18, 2024

@TomAFrench Thanks, I was about to say I had this fixed as well yesterday but it looks like I never made the PR. We found the same issue though.

github-merge-queue bot pushed a commit that referenced this issue Jun 18, 2024
# Description

## Problem\*

Resolves #5274

## Summary\*

This PR implements the `as_slice` builtin function for the comptime
interpreter. I've also fixed an issue where due to
builtin/foreign/oracle functions having an early return, we don't call
`exit_function` which results in the scopes being left in an
inconsistent state.

## 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.
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
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants