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

Broken dereference after any blocked scope (if, for, {}) #2911

Closed
vezenovm opened this issue Sep 29, 2023 · 2 comments · Fixed by #3075
Closed

Broken dereference after any blocked scope (if, for, {}) #2911

vezenovm opened this issue Sep 29, 2023 · 2 comments · Fixed by #3075
Labels
bug Something isn't working ssa

Comments

@vezenovm
Copy link
Contributor

Aim

I want to be able to run this test:

#[test]
fn test_mutable_ref()
{
    let x = mutable_return_syntax_error();
    assert(x[0] == 1);
}

fn mutable_return_syntax_error() -> [Field] {
    let slice : &mut [Field] = &mut [];

    slice = &mut (*slice).push_back(1);

    for i in 0..2 { }  // or if (...) { } // or  { } -- same idea

    *slice
}

Expected Behavior

I should be able to return the dereferenced slice.

Bug

Instead I get this bug:
Screenshot 2023-09-29 at 5 16 55 PM

If I fix the scope by adding a semicolon to the end of {} block like so:

fn mutable_return_syntax_error() -> [Field] {
    let slice : &mut [Field] = &mut [];

    slice = &mut (*slice).push_back(1);

    for i in 0..2 { };  // or if (...) { } // or  { } -- same idea

    *slice
}

I will instead get this panic in SSA:

Message:  internal error: entered unreachable code: Cannot assign to a variable without a reference
Location: compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs:662

To Reproduce

  1. Copy the snippet in the issue over to a Noir program.
  2. Run nargo test

Installation Method

Compiled from source

Nargo Version

nargo 0.15.0 (git version hash: 9854416, is dirty: true)

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@vezenovm vezenovm added bug Something isn't working ssa P-HIGH labels Sep 29, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 29, 2023
@jfecher
Copy link
Contributor

jfecher commented Oct 2, 2023

I'm able to replicate the panic.

I believe the problem is this line:

    slice = &mut (*slice).push_back(1);

Which is rather odd. Usually it is written:

    *slice = slice.push_back(1);

To avoid creating the new allocation, but it seems something must have went wrong with creating the new allocation here, leading to the panic.

@jfecher
Copy link
Contributor

jfecher commented Oct 5, 2023

It turns out the panic was from slice not being declared as mutable. Normally you can mutate variables of type &mut T even if the variable itself is not declared as mutable - but crucially to mutate them you need to dereference them on the lhs of an assignment. Swapping out the reference to another reference as in this example should not have been possible, so the panic was correct and we were missing an extra check for when a variable can be mutated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ssa
Projects
Archived in project
2 participants