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

fix: Prevent mutating immutable bindings to mutable reference types #3008

Closed
wants to merge 14 commits into from

Conversation

jfecher
Copy link
Contributor

@jfecher jfecher commented Oct 5, 2023

Description

Problem*

Finally Resolves #2911. There were several issues hidden there.

Summary*

In the simplified source code from the original bug:

#[test]
fn mutable_return_error() {
    let slice : &mut [Field] = &mut [];
    slice = &mut (*slice).push_back(1);
    assert((*slice)[0] == 1);
}

It turns out the panic was actually correct - it should have been unreachable. It stemmed from the mutation on slice when slice was not declared as mutable. Importantly, you can mutate mutable references through dereferencing them, but you should not have been able to swap out the reference itself.

I've fixed the mutability check to allow *slice = slice.push_back(1) (as before), but now disallow slice = &mut ... unless slice was also marked mutable.

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@jfecher jfecher requested a review from vezenovm October 5, 2023 22:01
kevaundray
kevaundray previously approved these changes Oct 5, 2023
@kevaundray kevaundray added this pull request to the merge queue Oct 5, 2023
@kevaundray kevaundray removed this pull request from the merge queue due to a manual request Oct 5, 2023
@kevaundray
Copy link
Contributor

Ah let's add a regression test to this

kevaundray
kevaundray previously approved these changes Oct 9, 2023
kevaundray
kevaundray previously approved these changes Oct 10, 2023
@jfecher
Copy link
Contributor Author

jfecher commented Oct 10, 2023

It looks like there was a bad merge since the diff of this PR got messed up. I'm just going to open another PR.

@jfecher jfecher closed this Oct 10, 2023
auto-merge was automatically disabled October 10, 2023 13:36

Pull request was closed

@TomAFrench TomAFrench deleted the jf/fix-mut-check branch November 20, 2024 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken dereference after any blocked scope (if, for, {})
2 participants