Skip to content

Commit

Permalink
Rollup merge of #85823 - fee1-dead:borrowck-0, r=jackh726
Browse files Browse the repository at this point in the history
Do not suggest ampmut if rhs is already mutable

Removes invalid suggestion in #85765, although it should highlight the user type instead of the local variable.

Looking at the comments of this line:
https://github.com/rust-lang/rust/blob/84b1005bfd22e2cb2a4c13b0b81958fe72628354/compiler/rustc_mir_build/src/build/matches/mod.rs#L2107

It was intentionally set to `None`, causing it to highlight the local variable instead. I am not sure if I will be able to fix it.

Fixes #85765
  • Loading branch information
JohnTitor authored Jun 11, 2021
2 parents 3b47d33 + 0eda509 commit 883e1a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,13 @@ fn suggest_ampmut<'tcx>(
{
let lt_name = &src[1..ws_pos];
let ty = &src[ws_pos..];
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
if !ty.trim_start().starts_with("mut") {
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
}
} else if let Some(stripped) = src.strip_prefix('&') {
return (assignment_rhs_span, format!("&mut {}", stripped));
if !stripped.trim_start().starts_with("mut") {
return (assignment_rhs_span, format!("&mut {}", stripped));
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/borrowck/issue-85765.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let mut test = Vec::new();
let rofl: &Vec<Vec<i32>> = &mut test;
//~^ HELP consider changing this to be a mutable reference
rofl.push(Vec::new());
//~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference
//~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
}
12 changes: 12 additions & 0 deletions src/test/ui/borrowck/issue-85765.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
--> $DIR/issue-85765.rs:5:5
|
LL | let rofl: &Vec<Vec<i32>> = &mut test;
| ---- help: consider changing this to be a mutable reference: `&mut Vec<Vec<i32>>`
LL |
LL | rofl.push(Vec::new());
| ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.

0 comments on commit 883e1a5

Please sign in to comment.