Skip to content

overhaul &mut suggestions in borrowck errors #145013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
773 changes: 408 additions & 365 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/ui/array-slice-vec/slice-mut-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
LL | let _ = &mut x[2..4];
| ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
help: consider changing this binding's type
|
LL | let x: &[isize] = &mut [1, 2, 3, 4, 5];
| +++
LL | let x: &mut [isize] = &[1, 2, 3, 4, 5];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still invalid, it needs to be &mut [1, 2, 3, 4, 5].

| +++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `*const` pointer
LL | let q = &raw mut *x;
| ^^^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
|
help: consider changing this to be a mutable pointer
help: consider specifying this binding's type
|
LL | let x = &mut 0 as *const i32;
| +++
LL | let x: *mut i32 = &0 as *const i32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also still invalid, let x = &mut 0 as *mut i32; would work, without needing the type annotation.

| ++++++++++

error: aborting due to 2 previous errors

Expand Down
7 changes: 4 additions & 3 deletions tests/ui/borrowck/borrowck-access-permissions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ error[E0596]: cannot borrow `*ptr_x` as mutable, as it is behind a `*const` poin
LL | let _y1 = &mut *ptr_x;
| ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
|
help: consider changing this to be a mutable pointer
help: consider changing this binding's type
|
LL - let ptr_x: *const _ = &x;
LL + let ptr_x: *mut i32 = &x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be &x instead of &mut x.

|
LL | let ptr_x: *const _ = &mut x;
| +++

error[E0596]: cannot borrow `*foo_ref.f` as mutable, as it is behind a `&` reference
--> $DIR/borrowck-access-permissions.rs:59:18
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ run-rustfix
fn main() {
let mut map = std::collections::BTreeMap::new();
map.insert(0, "string".to_owned());

let string = map.get_mut(&0).unwrap();
string.push_str("test");
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference

let mut map = std::collections::HashMap::new();
map.insert(0, "string".to_owned());

let string = map.get_mut(&0).unwrap();
string.push_str("test");
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference

let mut vec = vec![String::new(), String::new()];
let string = &mut vec[0];
string.push_str("test");
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ run-rustfix
fn main() {
let mut map = std::collections::BTreeMap::new();
map.insert(0, "string".to_owned());

let string = &map[&0];
string.push_str("test");
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference

let mut map = std::collections::HashMap::new();
map.insert(0, "string".to_owned());

let string = &map[&0];
string.push_str("test");
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference

let mut vec = vec![String::new(), String::new()];
let string = &vec[0];
string.push_str("test");
//~^ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
--> $DIR/overloaded-index-not-mut-but-should-be-mut.rs:7:5
|
LL | string.push_str("test");
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider using `get_mut`
|
LL - let string = &map[&0];
LL + let string = map.get_mut(&0).unwrap();
|

error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
--> $DIR/overloaded-index-not-mut-but-should-be-mut.rs:14:5
|
LL | string.push_str("test");
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider using `get_mut`
|
LL - let string = &map[&0];
LL + let string = map.get_mut(&0).unwrap();
|

error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
--> $DIR/overloaded-index-not-mut-but-should-be-mut.rs:19:5
|
LL | string.push_str("test");
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
LL | let string = &mut vec[0];
| +++

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0596`.
16 changes: 16 additions & 0 deletions tests/ui/borrowck/suggestions/overloaded-index-without-indexmut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::ops::Index;

struct MyType;
impl Index<usize> for MyType {
type Output = String;
fn index(&self, _idx: usize) -> &String {
const { &String::new() }
}
}

fn main() {
let x = MyType;
let y = &x[0];
y.push_str("");
//~^ ERROR cannot borrow `*y` as mutable, as it is behind a `&` reference
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0596]: cannot borrow `*y` as mutable, as it is behind a `&` reference
--> $DIR/overloaded-index-without-indexmut.rs:14:5
|
LL | y.push_str("");
| ^ `y` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0596`.
Loading