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 references from immutable variables are allowed and result in copies #5721

Closed
nventuro opened this issue Aug 14, 2024 · 0 comments · Fixed by #6037
Closed

Mutable references from immutable variables are allowed and result in copies #5721

nventuro opened this issue Aug 14, 2024 · 0 comments · Fixed by #6037
Assignees
Labels
bug Something isn't working

Comments

@nventuro
Copy link
Contributor

nventuro commented Aug 14, 2024

Aim

I should not be allowed to take a &mut from a non-mut variable. Not only does Noir allow it, it apparently creates a copy of the value when doing so, which means that the mutable reference does not refer to the original value, leading to hard-to-track bugs in which expected mutations are not carried out.

Expected Behavior

Given

fn bar(x: &mut Field) {
  *x = 2;
}

then none of the following calls to bar should compile, but they all do

fn foo(x: Field) {
  bar(&mut x);
  
  let y = x;
  bar(&mut y);

  let (j, k) = baz(x);
  bar(&mut k);
}

fn baz(x: Field) -> (Field, Field) {
  (x, x)
}

Bug

These two tests pass, but take_ref_from_non_mut should not even compile.

fn mutate_contents(x: &mut Field) {
    *x = 2;
}

fn take_ref_from_non_mut(x: Field) -> Field {
    mutate_contents(&mut x);
    x
}

fn take_ref_from_mut(mut x: Field) -> Field {
    mutate_contents(&mut x);
    x
}

fn test_ref_from_non_mut() {
    assert_eq(take_ref_from_non_mut(5), 0);
}

fn test_ref_from_mut() {
    assert_eq(take_ref_from_mut(5), 2);
}
@nventuro nventuro added the bug Something isn't working label Aug 14, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Aug 14, 2024
@asterite asterite self-assigned this Sep 13, 2024
github-merge-queue bot pushed a commit that referenced this issue Sep 13, 2024
# Description

## Problem

Resolves #5721

## Summary

Today I bumped into this bug so I thought about trying to fix it.

## 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 13, 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.

2 participants