-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also still invalid, |
||
| ++++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
| | ||
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 | ||
|
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(); | ||
fee1-dead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | ||
|
||
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(); | ||
fee1-dead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | ||
|
||
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]; | ||
fee1-dead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| +++ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0596`. |
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`. |
There was a problem hiding this comment.
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]
.