Skip to content

Commit 5eb83f4

Browse files
committed
Improve error reporting for modifications behind & references
1 parent ce331ee commit 5eb83f4

34 files changed

+90
-57
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
147147
if let Some(desc) = access_place_desc {
148148
item_msg = format!("`{}`", desc);
149149
reason = match error_access {
150-
AccessKind::Mutate => format!(" which is behind {}", pointer_type),
150+
AccessKind::Mutate => format!(", which is behind {}", pointer_type),
151151
AccessKind::MutableBorrow => {
152152
format!(", as it is behind {}", pointer_type)
153153
}
@@ -897,16 +897,32 @@ fn suggest_ampmut<'tcx>(
897897
) -> (Span, String) {
898898
if let Some(assignment_rhs_span) = opt_assignment_rhs_span {
899899
if let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) {
900+
let is_mutbl = |ty: &str| -> bool {
901+
if ty.starts_with("mut") {
902+
let rest = &ty[3..];
903+
match rest.chars().next() {
904+
// e.g. `&mut x`
905+
Some(c) if c.is_whitespace() => true,
906+
// e.g. `&mut(x)`
907+
Some('(') => true,
908+
// e.g. `&mutablevar`
909+
_ => false,
910+
}
911+
} else {
912+
false
913+
}
914+
};
900915
if let (true, Some(ws_pos)) =
901916
(src.starts_with("&'"), src.find(|c: char| -> bool { c.is_whitespace() }))
902917
{
903918
let lt_name = &src[1..ws_pos];
904-
let ty = &src[ws_pos..];
905-
if !ty.trim_start().starts_with("mut") {
919+
let ty = src[ws_pos..].trim_start();
920+
if !is_mutbl(ty) {
906921
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
907922
}
908923
} else if let Some(stripped) = src.strip_prefix('&') {
909-
if !stripped.trim_start().starts_with("mut") {
924+
let stripped = stripped.trim_start();
925+
if !is_mutbl(stripped) {
910926
return (assignment_rhs_span, format!("&mut {}", stripped));
911927
}
912928
}

src/test/ui/borrowck/borrowck-assign-to-andmut-in-aliasable-loc.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0594]: cannot assign to `*s.pointer` which is behind a `&` reference
1+
error[E0594]: cannot assign to `*s.pointer`, which is behind a `&` reference
22
--> $DIR/borrowck-assign-to-andmut-in-aliasable-loc.rs:9:5
33
|
44
LL | fn a(s: &S) {
55
| -- help: consider changing this to be a mutable reference: `&mut S<'_>`
66
LL | *s.pointer += 1;
77
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written
88

9-
error[E0594]: cannot assign to `*s.pointer` which is behind a `&` reference
9+
error[E0594]: cannot assign to `*s.pointer`, which is behind a `&` reference
1010
--> $DIR/borrowck-assign-to-andmut-in-aliasable-loc.rs:17:5
1111
|
1212
LL | fn c(s: & &mut S) {

src/test/ui/borrowck/borrowck-borrow-mut-base-ptr-in-aliasable-loc.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0594]: cannot assign to `**t1` which is behind a `&` reference
1+
error[E0594]: cannot assign to `**t1`, which is behind a `&` reference
22
--> $DIR/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs:9:5
33
|
44
LL | let t1 = t0;

src/test/ui/borrowck/borrowck-issue-14498.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0594]: cannot assign to `***p` which is behind a `&` reference
1+
error[E0594]: cannot assign to `***p`, which is behind a `&` reference
22
--> $DIR/borrowck-issue-14498.rs:16:5
33
|
44
LL | let p = &y;

src/test/ui/borrowck/issue-69789-iterator-mut-suggestion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0594]: cannot assign to `*item` which is behind a `&` reference
1+
error[E0594]: cannot assign to `*item`, which is behind a `&` reference
22
--> $DIR/issue-69789-iterator-mut-suggestion.rs:7:9
33
|
44
LL | for item in &mut std::iter::empty::<&'static ()>() {

src/test/ui/borrowck/issue-83309-ice-immut-in-for-loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
for v in Query.iter_mut() {
1010
//~^ NOTE this iterator yields `&` references
1111
*v -= 1;
12-
//~^ ERROR cannot assign to `*v` which is behind a `&` reference
12+
//~^ ERROR cannot assign to `*v`, which is behind a `&` reference
1313
//~| NOTE `v` is a `&` reference, so the data it refers to cannot be written
1414
}
1515
}

src/test/ui/borrowck/issue-83309-ice-immut-in-for-loop.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0594]: cannot assign to `*v` which is behind a `&` reference
1+
error[E0594]: cannot assign to `*v`, which is behind a `&` reference
22
--> $DIR/issue-83309-ice-immut-in-for-loop.rs:11:9
33
|
44
LL | for v in Query.iter_mut() {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ fn main() {
55
rofl.push(Vec::new());
66
//~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference
77
//~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
8+
9+
let mut mutvar = 42;
10+
let r = &mutvar;
11+
//~^ HELP consider changing this to be a mutable reference
12+
*r = 0;
13+
//~^ ERROR cannot assign to `*r`, which is behind a `&` reference
14+
//~| NOTE `r` is a `&` reference, so the data it refers to cannot be written
815
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ LL |
77
LL | rofl.push(Vec::new());
88
| ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
99

10-
error: aborting due to previous error
10+
error[E0594]: cannot assign to `*r`, which is behind a `&` reference
11+
--> $DIR/issue-85765.rs:12:5
12+
|
13+
LL | let r = &mutvar;
14+
| ------- help: consider changing this to be a mutable reference: `&mut mutvar`
15+
LL |
16+
LL | *r = 0;
17+
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written
18+
19+
error: aborting due to 2 previous errors
1120

12-
For more information about this error, try `rustc --explain E0596`.
21+
Some errors have detailed explanations: E0594, E0596.
22+
For more information about an error, try `rustc --explain E0594`.

src/test/ui/borrowck/mutability-errors.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0594]: cannot assign to `*x` which is behind a `&` reference
1+
error[E0594]: cannot assign to `*x`, which is behind a `&` reference
22
--> $DIR/mutability-errors.rs:9:5
33
|
44
LL | fn named_ref(x: &(i32,)) {
55
| ------- help: consider changing this to be a mutable reference: `&mut (i32,)`
66
LL | *x = (1,);
77
| ^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
88

9-
error[E0594]: cannot assign to `x.0` which is behind a `&` reference
9+
error[E0594]: cannot assign to `x.0`, which is behind a `&` reference
1010
--> $DIR/mutability-errors.rs:10:5
1111
|
1212
LL | fn named_ref(x: &(i32,)) {
@@ -57,15 +57,15 @@ error[E0596]: cannot borrow data in a `&` reference as mutable
5757
LL | &mut f().0;
5858
| ^^^^^^^^^^ cannot borrow as mutable
5959

60-
error[E0594]: cannot assign to `*x` which is behind a `*const` pointer
60+
error[E0594]: cannot assign to `*x`, which is behind a `*const` pointer
6161
--> $DIR/mutability-errors.rs:23:5
6262
|
6363
LL | unsafe fn named_ptr(x: *const (i32,)) {
6464
| ------------- help: consider changing this to be a mutable pointer: `*mut (i32,)`
6565
LL | *x = (1,);
6666
| ^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be written
6767

68-
error[E0594]: cannot assign to `x.0` which is behind a `*const` pointer
68+
error[E0594]: cannot assign to `x.0`, which is behind a `*const` pointer
6969
--> $DIR/mutability-errors.rs:24:5
7070
|
7171
LL | unsafe fn named_ptr(x: *const (i32,)) {

0 commit comments

Comments
 (0)