diff options
Diffstat (limited to 'tests')
8 files changed, 115 insertions, 9 deletions
diff --git a/tests/ui/array-slice-vec/slice-mut-2.stderr b/tests/ui/array-slice-vec/slice-mut-2.stderr index 8cc2c6e0397..228417c873d 100644 --- a/tests/ui/array-slice-vec/slice-mut-2.stderr +++ b/tests/ui/array-slice-vec/slice-mut-2.stderr @@ -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]; + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/borrow-raw-address-of-deref-mutability.stderr b/tests/ui/borrowck/borrow-raw-address-of-deref-mutability.stderr index ac0241cf9a7..0a32cccff1d 100644 --- a/tests/ui/borrowck/borrow-raw-address-of-deref-mutability.stderr +++ b/tests/ui/borrowck/borrow-raw-address-of-deref-mutability.stderr @@ -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; + | ++++++++++ error: aborting due to 2 previous errors diff --git a/tests/ui/borrowck/borrowck-access-permissions.stderr b/tests/ui/borrowck/borrowck-access-permissions.stderr index ade10dbbfbd..87717a53290 100644 --- a/tests/ui/borrowck/borrowck-access-permissions.stderr +++ b/tests/ui/borrowck/borrowck-access-permissions.stderr @@ -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; | -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 diff --git a/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.fixed b/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.fixed new file mode 100644 index 00000000000..6303733967b --- /dev/null +++ b/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.fixed @@ -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 +} diff --git a/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.rs b/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.rs new file mode 100644 index 00000000000..be1a63a5e69 --- /dev/null +++ b/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.rs @@ -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 +} diff --git a/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.stderr b/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.stderr new file mode 100644 index 00000000000..44cc9aefcf1 --- /dev/null +++ b/tests/ui/borrowck/suggestions/overloaded-index-not-mut-but-should-be-mut.stderr @@ -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`. diff --git a/tests/ui/borrowck/suggestions/overloaded-index-without-indexmut.rs b/tests/ui/borrowck/suggestions/overloaded-index-without-indexmut.rs new file mode 100644 index 00000000000..06eb5b52e5f --- /dev/null +++ b/tests/ui/borrowck/suggestions/overloaded-index-without-indexmut.rs @@ -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 +} diff --git a/tests/ui/borrowck/suggestions/overloaded-index-without-indexmut.stderr b/tests/ui/borrowck/suggestions/overloaded-index-without-indexmut.stderr new file mode 100644 index 00000000000..6a46332a5d7 --- /dev/null +++ b/tests/ui/borrowck/suggestions/overloaded-index-without-indexmut.stderr @@ -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`. |
