Closed
Description
The compiler makes an invalid suggestion for how to correct the immutability of a reference in a match
block. To reproduce, try to compile the following code:
use std::fs::File;
use std::io::Write;
fn main() {
let mut op = Some(File::open("").unwrap());
match op {
Some(ref mut f) => { write!(f, "!").unwrap(); },
None => {},
}
}
On stable and beta, the compiler will print:
<anon>:7:33: 7:34 error: cannot borrow immutable local variable `f` as mutable
<anon>:7 Some(ref f) => { write!(f, "!").unwrap(); },
^
<anon>:7:26: 7:40 note: in this expansion of write! (defined in <std macros>)
<anon>:7:14: 7:19 help: to make the local variable mutable, use `mut` as shown:
<anon>: Some(mut ref f) => { write!(f, "!").unwrap(); },
On nightly, it will print:
error: cannot borrow immutable local variable `f` as mutable
--> <anon>:7:33
7 |> Some(ref f) => { write!(f, "!").unwrap(); },
|> ----- ^ cannot borrow mutably
|> |
|> use `mut ref f` here to make mutable
Both of these suggestions are erroneous, as mut ref f
is invalid syntax, and should be ref mut f
.