Skip to content

Commit e5f6498

Browse files
committed
Auto merge of #51612 - ashtneoi:51515-missing-first-char, r=pnkfelix
NLL: fix E0594 "change to mutable ref" suggestion Fix #51515. Fix #51879. Questions: - [x] Is this the right place to fix this? It feels brittle, being so close to the frontend. **It's probably fine.** - [ ] Have I missed any other cases that trigger this behavior? - [x] Is it okay to use HELP and SUGGESTION in the UI test? **Yes.** - [x] Do I need more tests for this? **No.**
2 parents 90bd83c + dc8ae26 commit e5f6498

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
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

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(nll)]
12+
13+
fn main() {
14+
let foo = &16;
15+
//~^ HELP consider changing this to be a mutable reference
16+
//~| SUGGESTION &mut 16
17+
*foo = 32;
18+
//~^ ERROR cannot assign to `*foo` which is behind a `&` reference
19+
let bar = foo;
20+
//~^ HELP consider changing this to be a mutable reference
21+
//~| SUGGESTION &mut i32
22+
*bar = 64;
23+
//~^ ERROR cannot assign to `*bar` which is behind a `&` reference
24+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0594]: cannot assign to `*foo` which is behind a `&` reference
2+
--> $DIR/issue-51515.rs:17:5
3+
|
4+
LL | let foo = &16;
5+
| --- help: consider changing this to be a mutable reference: `&mut 16`
6+
...
7+
LL | *foo = 32;
8+
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
9+
10+
error[E0594]: cannot assign to `*bar` which is behind a `&` reference
11+
--> $DIR/issue-51515.rs:22:5
12+
|
13+
LL | let bar = foo;
14+
| --- help: consider changing this to be a mutable reference: `&mut i32`
15+
...
16+
LL | *bar = 64;
17+
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)