You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Invalid suggestion for mutability correction in error message #33819
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.
Actually if you write ref mut v it doesn't work either, as then v is an immutable &mut i32. So I guess no note should be printed, and possibly mut ref [mut] $ident should be allowed in patterns.
You are right, but it was just my mistake. I updated the original code to the simplest thing I could think of which invokes the right error message and is fixable by ref mut.
In this particular case, but is that true in the general case? The heuristics for when to suggest something are necessarily going to have corner cases (which hopefully will get smoothed over time)
Activity
Update errors to use new error format
durka commentedon May 23, 2016
Actually if you write
ref mut vit doesn't work either, as thenvis an immutable&mut i32. So I guess no note should be printed, and possiblymut ref [mut] $identshould be allowed in patterns.Fix rust-lang#33819 and update ui test
Vtec234 commentedon May 24, 2016
You are right, but it was just my mistake. I updated the original code to the simplest thing I could think of which invokes the right error message and is fixable by
ref mut.sophiajt commentedon May 24, 2016
In this particular case, but is that true in the general case? The heuristics for when to suggest something are necessarily going to have corner cases (which hopefully will get smoothed over time)
Rollup merge of rust-lang#33820 - jonathandturner:format_readability_…
Rollup merge of rust-lang#81387 - c410-f3r:tests-tests-tests, r=petro…
Rollup merge of rust-lang#81387 - c410-f3r:tests-tests-tests, r=petro…