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

Invalid suggestion for use of mutable ref; use ref mut mut key #34337

Closed
alexandermerritt opened this issue Jun 18, 2016 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@alexandermerritt
Copy link
Contributor

alexandermerritt commented Jun 18, 2016

Compiling this code:

fn get(key: &mut String) { }
fn main() {
    let mut v: Vec<String> = Vec::new();
    let ref mut key = v[0];
    get(&mut key);
}

leads to this resulting error output on nightly:

test.rs:6:14: 6:17 error: cannot borrow immutable local variable `key` as mutable
test.rs:6     get(&mut key);
                       ^~~
test.rs:5:9: 5:20 note: use `ref mut mut key` here to make mutable
test.rs:5     let ref mut key = v[0];
                  ^~~~~~~~~~~
error: aborting due to previous error

The suggestion use ref mut mut key here to make mutable seems bogus. I noticed this, as I had originally written let mut key = v[0]; and rustc had suggested to put mut ref.

I would expect the output to suggest me to not use ref mut and instead let mut key = &mut v[0], or to invoke get(key) and declare let ref mut key.

Interestingly, if you run this on play.rust-lang.org the error message is the following:

error: cannot borrow immutable local variable `key` as mutable
 --> <anon>:6:14
5 |>     let ref mut key = v[0];
  |>         ----------- use `mut ref mut key` here to make mutable
6 |>     get(&mut key);
  |>              ^^^ cannot borrow mutably

Here, the suggestion is use mut ref mut key here to make mutable.

Meta

rustc --version --verbose
rustc 1.11.0-nightly (bb4a79b08 2016-06-15)
binary: rustc
commit-hash: bb4a79b087158f396b984bdf552d2c90890b12a3
commit-date: 2016-06-15
host: x86_64-unknown-linux-gnu
release: 1.11.0-nightly

No backtrace available because the compiler halts the build.

@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 24, 2016
bors added a commit that referenced this issue Nov 12, 2016
Fix invalid "ref mut mut" sugestion

Change output from:

```nocode
error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
11 |         TestEnum::Item(ref mut x) => {
   |                        --------- use `ref mut mut x` here to make mutable
12 |             test(&mut x);
   |                       ^ cannot borrow mutably
```

to

```nocode
error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
12 |             test(&mut x);
   |                       ^
   |                       |
   |                       cannot reborrow mutably
   |                       try removing `&mut` here
```
Fixes #37139, #34337, #34126
@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@nikomatsakis
Copy link
Contributor

Fixed by #37531

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

4 participants