Skip to content

Commit dc8ae26

Browse files
committed
Fix issue #51515 and update test
1 parent a49b75d commit dc8ae26

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/librustc_mir/borrow_check/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1905,8 +1905,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
19051905
// highlighted text will always be `&<expr>` and
19061906
// thus can transform to `&mut` by slicing off
19071907
// first ASCII character and prepending "&mut ".
1908-
let borrowed_expr = src[1..].to_string();
1909-
return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
1908+
if src.starts_with('&') {
1909+
let borrowed_expr = src[1..].to_string();
1910+
return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
1911+
}
19101912
}
19111913
}
19121914

src/test/ui/suggestions/issue-51515.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212

1313
fn main() {
1414
let foo = &16;
15+
//~^ HELP consider changing this to be a mutable reference
16+
//~| SUGGESTION &mut 16
1517
*foo = 32;
18+
//~^ ERROR cannot assign to `*foo` which is behind a `&` reference
1619
let bar = foo;
20+
//~^ HELP consider changing this to be a mutable reference
21+
//~| SUGGESTION &mut i32
1722
*bar = 64;
23+
//~^ ERROR cannot assign to `*bar` which is behind a `&` reference
1824
}

src/test/ui/suggestions/issue-51515.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
error[E0594]: cannot assign to `*foo` which is behind a `&` reference
2-
--> $DIR/issue-51515.rs:15:5
2+
--> $DIR/issue-51515.rs:17:5
33
|
44
LL | let foo = &16;
55
| --- help: consider changing this to be a mutable reference: `&mut 16`
6+
...
67
LL | *foo = 32;
78
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
89

910
error[E0594]: cannot assign to `*bar` which is behind a `&` reference
10-
--> $DIR/issue-51515.rs:17:5
11+
--> $DIR/issue-51515.rs:22:5
1112
|
13+
LL | let bar = foo;
14+
| --- help: consider changing this to be a mutable reference: `&mut i32`
15+
...
1216
LL | *bar = 64;
1317
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
1418

0 commit comments

Comments
 (0)