Open
Description
let mut mem_buffer : &[u8] = &b"foo"[..];
let mut reader = &mut mem_buffer as &std::io::Read;
let mut read_buffer = [0u8, 10];
reader.read(&mut read_buffer);
reports
error[E0596]: cannot borrow immutable borrowed content `*reader` as mutable
--> src/main.rs:5:5
|
5 | reader.read(&mut read_buffer);
| ^^^^^^ cannot borrow as mutable
Which is correct, but the fix is to modify the creation of the reader
variable. While this case is easy, it's probably less so in the general case. Possible avenues of improvement:
- The easy way this error message can be improved is to mention that the type is
&T
, but should be&mut T
. - If there are explicit type annotations on the variable, a note should point to it
- If the variable's type is inferred, a note should point to its initializer and maybe even do some primitive checks that can lead the developer towards the solution.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
estebank commentedon Dec 13, 2018
Current output:
Applying the suggestion is (correctly) a parse error. We need to change that suggestion to find the appropriate place for the
mut
: it should be&mut mem_buffer as &mut std::io::Read
.estebank commentedon Feb 3, 2023
Current output:
Provide structured suggestion for binding needing type on E0594
Rollup merge of rust-lang#107646 - estebank:specific-span, r=compiler…
estebank commentedon May 2, 2024
Current output:
Applying the suggestion we get
Only thing left is to detect when an E0308 is pointing at an
as
casting expression and point only at the type part of it, and opportunistically suggest changing it to the right type. Having said that, I'm satisfied with the current output.