Skip to content

Commit 883e1a5

Browse files
authored
Rollup merge of #85823 - fee1-dead:borrowck-0, r=jackh726
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
2 parents 3b47d33 + 0eda509 commit 883e1a5

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,13 @@ fn suggest_ampmut<'tcx>(
902902
{
903903
let lt_name = &src[1..ws_pos];
904904
let ty = &src[ws_pos..];
905-
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
905+
if !ty.trim_start().starts_with("mut") {
906+
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
907+
}
906908
} else if let Some(stripped) = src.strip_prefix('&') {
907-
return (assignment_rhs_span, format!("&mut {}", stripped));
909+
if !stripped.trim_start().starts_with("mut") {
910+
return (assignment_rhs_span, format!("&mut {}", stripped));
911+
}
908912
}
909913
}
910914
}

src/test/ui/borrowck/issue-85765.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let mut test = Vec::new();
3+
let rofl: &Vec<Vec<i32>> = &mut test;
4+
//~^ HELP consider changing this to be a mutable reference
5+
rofl.push(Vec::new());
6+
//~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference
7+
//~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
2+
--> $DIR/issue-85765.rs:5:5
3+
|
4+
LL | let rofl: &Vec<Vec<i32>> = &mut test;
5+
| ---- help: consider changing this to be a mutable reference: `&mut Vec<Vec<i32>>`
6+
LL |
7+
LL | rofl.push(Vec::new());
8+
| ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)