Skip to content

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

Closed
@alexandermerritt

Description

@alexandermerritt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions