Skip to content

Commit 4556034

Browse files
committed
Fix ICE in borrowck mutability suggestion with multi-byte ref sigil
1 parent 12f35ad commit 4556034

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14111411
// If there is already a binding, we modify it to be `mut`.
14121412
} else if binding_exists {
14131413
// Shrink the span to just after the `&` in `&variable`.
1414-
let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo();
1414+
let amp_len = self
1415+
.infcx
1416+
.tcx
1417+
.sess
1418+
.source_map()
1419+
.span_to_snippet(span)
1420+
.ok()
1421+
.and_then(|s| s.chars().next().map(|c| c.len_utf8() as u32))
1422+
.unwrap_or(1);
1423+
let span = span.with_lo(span.lo() + BytePos(amp_len)).shrink_to_lo();
14151424
(span, "mut ".to_owned(), true)
14161425
} else {
14171426
// Otherwise, suggest that the user annotates the binding; We provide the

tests/crashes/139089.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/139089
2+
3+
fn foo(x:Vec<u8>) {
4+
//~^ ERROR unknown start of token
5+
x.push(0);
6+
//~^ ERROR cannot borrow `*x` as mutable
7+
}
8+
9+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: unknown start of token: \u{ff06}
2+
--> $DIR/mutability-suggestion-fullwidth-ampersand.rs:3:11
3+
|
4+
LL | fn foo(x: &Vec<u8>) {
5+
| ^^
6+
|
7+
help: Unicode character '&' (Fullwidth Ampersand) looks like '&' (Ampersand), but it is not
8+
|
9+
LL - fn foo(x: &Vec<u8>) {
10+
LL + fn foo(x: &Vec<u8>) {
11+
|
12+
13+
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
14+
--> $DIR/mutability-suggestion-fullwidth-ampersand.rs:5:5
15+
|
16+
LL | x.push(0);
17+
| ^ `x` is a `&` reference, so it cannot be borrowed as mutable
18+
|
19+
help: consider changing this to be a mutable reference
20+
|
21+
LL | fn foo(x: &mut Vec<u8>) {
22+
| +++
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)