Skip to content

Commit 2d1f731

Browse files
committed
overhaul &mut suggestions in borrowck errors
1 parent dc0bae1 commit 2d1f731

9 files changed

+475
-332
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 360 additions & 323 deletions
Large diffs are not rendered by default.

tests/ui/array-slice-vec/slice-mut-2.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
44
LL | let _ = &mut x[2..4];
55
| ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
66
|
7-
help: consider changing this to be a mutable reference
7+
help: consider changing this binding's type
88
|
9-
LL | let x: &[isize] = &mut [1, 2, 3, 4, 5];
10-
| +++
9+
LL | let x: &mut [isize] = &[1, 2, 3, 4, 5];
10+
| +++
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/borrowck/borrow-raw-address-of-deref-mutability.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `*const` pointer
1515
LL | let q = &raw mut *x;
1616
| ^^^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
1717
|
18-
help: consider changing this to be a mutable pointer
18+
help: consider specifying this binding's type
1919
|
20-
LL | let x = &mut 0 as *const i32;
21-
| +++
20+
LL | let x: *mut i32 = &0 as *const i32;
21+
| ++++++++++
2222

2323
error: aborting due to 2 previous errors
2424

tests/ui/borrowck/borrowck-access-permissions.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ error[E0596]: cannot borrow `*ptr_x` as mutable, as it is behind a `*const` poin
4343
LL | let _y1 = &mut *ptr_x;
4444
| ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
4545
|
46-
help: consider changing this to be a mutable pointer
46+
help: consider changing this binding's type
47+
|
48+
LL - let ptr_x: *const _ = &x;
49+
LL + let ptr_x: *mut i32 = &x;
4750
|
48-
LL | let ptr_x: *const _ = &mut x;
49-
| +++
5051

5152
error[E0596]: cannot borrow `*foo_ref.f` as mutable, as it is behind a `&` reference
5253
--> $DIR/borrowck-access-permissions.rs:59:18
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ run-rustfix
2+
fn main() {
3+
let mut map = std::collections::BTreeMap::new();
4+
map.insert(0, "string".to_owned());
5+
6+
let string = map.get_mut(&0).unwrap();
7+
string.push_str("test");
8+
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
9+
10+
let mut map = std::collections::HashMap::new();
11+
map.insert(0, "string".to_owned());
12+
13+
let string = map.get_mut(&0).unwrap();
14+
string.push_str("test");
15+
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
16+
17+
let mut vec = vec![String::new(), String::new()];
18+
let string = &mut vec[0];
19+
string.push_str("test");
20+
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ run-rustfix
2+
fn main() {
3+
let mut map = std::collections::BTreeMap::new();
4+
map.insert(0, "string".to_owned());
5+
6+
let string = &map[&0];
7+
string.push_str("test");
8+
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
9+
10+
let mut map = std::collections::HashMap::new();
11+
map.insert(0, "string".to_owned());
12+
13+
let string = &map[&0];
14+
string.push_str("test");
15+
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
16+
17+
let mut vec = vec![String::new(), String::new()];
18+
let string = &vec[0];
19+
string.push_str("test");
20+
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
2+
--> $DIR/overloaded-index-not-mut-but-should-be-mut.rs:7:5
3+
|
4+
LL | string.push_str("test");
5+
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
6+
|
7+
help: consider using `get_mut`
8+
|
9+
LL - let string = &map[&0];
10+
LL + let string = map.get_mut(&0).unwrap();
11+
|
12+
13+
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
14+
--> $DIR/overloaded-index-not-mut-but-should-be-mut.rs:14:5
15+
|
16+
LL | string.push_str("test");
17+
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
18+
|
19+
help: consider using `get_mut`
20+
|
21+
LL - let string = &map[&0];
22+
LL + let string = map.get_mut(&0).unwrap();
23+
|
24+
25+
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
26+
--> $DIR/overloaded-index-not-mut-but-should-be-mut.rs:19:5
27+
|
28+
LL | string.push_str("test");
29+
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
30+
|
31+
help: consider changing this to be a mutable reference
32+
|
33+
LL | let string = &mut vec[0];
34+
| +++
35+
36+
error: aborting due to 3 previous errors
37+
38+
For more information about this error, try `rustc --explain E0596`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::ops::Index;
2+
3+
struct MyType;
4+
impl Index<usize> for MyType {
5+
type Output = String;
6+
fn index(&self, _idx: usize) -> &String {
7+
const { &String::new() }
8+
}
9+
}
10+
11+
fn main() {
12+
let x = MyType;
13+
let y = &x[0];
14+
y.push_str("");
15+
//~^ ERROR cannot borrow `*y` as mutable, as it is behind a `&` reference
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0596]: cannot borrow `*y` as mutable, as it is behind a `&` reference
2+
--> $DIR/overloaded-index-without-indexmut.rs:14:5
3+
|
4+
LL | y.push_str("");
5+
| ^ `y` is a `&` reference, so the data it refers to cannot be borrowed as mutable
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)