Skip to content

Commit

Permalink
Fix issue #51515 and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtneoi committed Jul 9, 2018
1 parent a49b75d commit dc8ae26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,8 +1905,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
// highlighted text will always be `&<expr>` and
// thus can transform to `&mut` by slicing off
// first ASCII character and prepending "&mut ".
let borrowed_expr = src[1..].to_string();
return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
if src.starts_with('&') {
let borrowed_expr = src[1..].to_string();
return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/suggestions/issue-51515.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

fn main() {
let foo = &16;
//~^ HELP consider changing this to be a mutable reference
//~| SUGGESTION &mut 16
*foo = 32;
//~^ ERROR cannot assign to `*foo` which is behind a `&` reference
let bar = foo;
//~^ HELP consider changing this to be a mutable reference
//~| SUGGESTION &mut i32
*bar = 64;
//~^ ERROR cannot assign to `*bar` which is behind a `&` reference
}
8 changes: 6 additions & 2 deletions src/test/ui/suggestions/issue-51515.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
error[E0594]: cannot assign to `*foo` which is behind a `&` reference
--> $DIR/issue-51515.rs:15:5
--> $DIR/issue-51515.rs:17:5
|
LL | let foo = &16;
| --- help: consider changing this to be a mutable reference: `&mut 16`
...
LL | *foo = 32;
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written

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

Expand Down

0 comments on commit dc8ae26

Please sign in to comment.