diff options
| author | Michael Goulet <michael@errs.io> | 2022-03-05 01:57:43 -0800 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2022-05-17 21:20:24 -0700 |
| commit | d50d3fccdd3038b2621245e9591ea6e20eebde2a (patch) | |
| tree | f8709c00fa23bce6de8f8215722a505cec13d366 /src | |
| parent | b26580f2149c7f4196eac76525cc1d53f215b29b (diff) | |
| download | rust-d50d3fccdd3038b2621245e9591ea6e20eebde2a.tar.gz rust-d50d3fccdd3038b2621245e9591ea6e20eebde2a.zip | |
better lvalue errors for things implementing DerefMut
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/ui/suggestions/mut-ref-reassignment.stderr | 4 | ||||
| -rw-r--r-- | src/test/ui/typeck/assign-non-lval-derefmut.fixed | 15 | ||||
| -rw-r--r-- | src/test/ui/typeck/assign-non-lval-derefmut.rs | 15 | ||||
| -rw-r--r-- | src/test/ui/typeck/assign-non-lval-derefmut.stderr | 58 | ||||
| -rw-r--r-- | src/test/ui/typeck/assign-non-lval-mut-ref.fixed | 6 | ||||
| -rw-r--r-- | src/test/ui/typeck/assign-non-lval-mut-ref.rs | 6 | ||||
| -rw-r--r-- | src/test/ui/typeck/assign-non-lval-mut-ref.stderr | 32 | ||||
| -rw-r--r-- | src/test/ui/typeck/issue-93486.stderr | 2 |
8 files changed, 132 insertions, 6 deletions
diff --git a/src/test/ui/suggestions/mut-ref-reassignment.stderr b/src/test/ui/suggestions/mut-ref-reassignment.stderr index 3bd98c76307..b3cb6dd0614 100644 --- a/src/test/ui/suggestions/mut-ref-reassignment.stderr +++ b/src/test/ui/suggestions/mut-ref-reassignment.stderr @@ -8,7 +8,7 @@ LL | opt = None; | = note: expected mutable reference `&mut Option<String>` found enum `Option<_>` -help: consider dereferencing here to assign to the mutable borrowed piece of memory +help: consider dereferencing here to assign to the mutably borrowed value | LL | *opt = None; | + @@ -34,7 +34,7 @@ LL | opt = Some(String::new()) | = note: expected mutable reference `&mut Option<String>` found enum `Option<String>` -help: consider dereferencing here to assign to the mutable borrowed piece of memory +help: consider dereferencing here to assign to the mutably borrowed value | LL | *opt = Some(String::new()) | + diff --git a/src/test/ui/typeck/assign-non-lval-derefmut.fixed b/src/test/ui/typeck/assign-non-lval-derefmut.fixed new file mode 100644 index 00000000000..0c23199af22 --- /dev/null +++ b/src/test/ui/typeck/assign-non-lval-derefmut.fixed @@ -0,0 +1,15 @@ +// run-rustfix + +fn main() { + let x = std::sync::Mutex::new(1usize); + *x.lock().unwrap() = 2; + //~^ ERROR invalid left-hand side of assignment + *x.lock().unwrap() += 1; + //~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` + + let mut y = x.lock().unwrap(); + *y = 2; + //~^ ERROR mismatched types + *y += 1; + //~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` +} diff --git a/src/test/ui/typeck/assign-non-lval-derefmut.rs b/src/test/ui/typeck/assign-non-lval-derefmut.rs new file mode 100644 index 00000000000..ec1882f5271 --- /dev/null +++ b/src/test/ui/typeck/assign-non-lval-derefmut.rs @@ -0,0 +1,15 @@ +// run-rustfix + +fn main() { + let x = std::sync::Mutex::new(1usize); + x.lock().unwrap() = 2; + //~^ ERROR invalid left-hand side of assignment + x.lock().unwrap() += 1; + //~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` + + let mut y = x.lock().unwrap(); + y = 2; + //~^ ERROR mismatched types + y += 1; + //~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` +} diff --git a/src/test/ui/typeck/assign-non-lval-derefmut.stderr b/src/test/ui/typeck/assign-non-lval-derefmut.stderr new file mode 100644 index 00000000000..a6fcdfe21f4 --- /dev/null +++ b/src/test/ui/typeck/assign-non-lval-derefmut.stderr @@ -0,0 +1,58 @@ +error[E0070]: invalid left-hand side of assignment + --> $DIR/assign-non-lval-derefmut.rs:5:23 + | +LL | x.lock().unwrap() = 2; + | ----------------- ^ + | | + | cannot assign to this expression + | +help: consider dereferencing here to assign to the mutably borrowed value + | +LL | *x.lock().unwrap() = 2; + | + + +error[E0368]: binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` + --> $DIR/assign-non-lval-derefmut.rs:7:5 + | +LL | x.lock().unwrap() += 1; + | -----------------^^^^^ + | | + | cannot use `+=` on type `MutexGuard<'_, usize>` + | +help: `+=` can be used on `usize`, you can dereference `x.lock().unwrap()` + | +LL | *x.lock().unwrap() += 1; + | + + +error[E0308]: mismatched types + --> $DIR/assign-non-lval-derefmut.rs:11:9 + | +LL | let mut y = x.lock().unwrap(); + | ----------------- expected due to this value +LL | y = 2; + | ^ expected struct `MutexGuard`, found integer + | + = note: expected struct `MutexGuard<'_, usize>` + found type `{integer}` +help: consider dereferencing here to assign to the mutably borrowed value + | +LL | *y = 2; + | + + +error[E0368]: binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` + --> $DIR/assign-non-lval-derefmut.rs:13:5 + | +LL | y += 1; + | -^^^^^ + | | + | cannot use `+=` on type `MutexGuard<'_, usize>` + | +help: `+=` can be used on `usize`, you can dereference `y` + | +LL | *y += 1; + | + + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0070, E0308, E0368. +For more information about an error, try `rustc --explain E0070`. diff --git a/src/test/ui/typeck/assign-non-lval-mut-ref.fixed b/src/test/ui/typeck/assign-non-lval-mut-ref.fixed index c4dadfbdfce..10c7b9dbfb3 100644 --- a/src/test/ui/typeck/assign-non-lval-mut-ref.fixed +++ b/src/test/ui/typeck/assign-non-lval-mut-ref.fixed @@ -6,4 +6,10 @@ fn main() { //~^ ERROR invalid left-hand side of assignment *x.last_mut().unwrap() += 1; //~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` + + let y = x.last_mut().unwrap(); + *y = 2; + //~^ ERROR mismatched types + *y += 1; + //~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` } diff --git a/src/test/ui/typeck/assign-non-lval-mut-ref.rs b/src/test/ui/typeck/assign-non-lval-mut-ref.rs index 39573ddb6d0..bceff0ef09d 100644 --- a/src/test/ui/typeck/assign-non-lval-mut-ref.rs +++ b/src/test/ui/typeck/assign-non-lval-mut-ref.rs @@ -6,4 +6,10 @@ fn main() { //~^ ERROR invalid left-hand side of assignment x.last_mut().unwrap() += 1; //~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` + + let y = x.last_mut().unwrap(); + y = 2; + //~^ ERROR mismatched types + y += 1; + //~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` } diff --git a/src/test/ui/typeck/assign-non-lval-mut-ref.stderr b/src/test/ui/typeck/assign-non-lval-mut-ref.stderr index b0ca089b709..be2e9fe95e8 100644 --- a/src/test/ui/typeck/assign-non-lval-mut-ref.stderr +++ b/src/test/ui/typeck/assign-non-lval-mut-ref.stderr @@ -6,7 +6,7 @@ LL | x.last_mut().unwrap() = 2; | | | cannot assign to this expression | -help: consider dereferencing here to assign to the mutable borrowed piece of memory +help: consider dereferencing here to assign to the mutably borrowed value | LL | *x.last_mut().unwrap() = 2; | + @@ -24,7 +24,33 @@ help: `+=` can be used on `usize`, you can dereference `x.last_mut().unwrap()` LL | *x.last_mut().unwrap() += 1; | + -error: aborting due to 2 previous errors +error[E0308]: mismatched types + --> $DIR/assign-non-lval-mut-ref.rs:11:9 + | +LL | let y = x.last_mut().unwrap(); + | --------------------- expected due to this value +LL | y = 2; + | ^ expected `&mut usize`, found integer + | +help: consider dereferencing here to assign to the mutably borrowed value + | +LL | *y = 2; + | + + +error[E0368]: binary assignment operation `+=` cannot be applied to type `&mut usize` + --> $DIR/assign-non-lval-mut-ref.rs:13:5 + | +LL | y += 1; + | -^^^^^ + | | + | cannot use `+=` on type `&mut usize` + | +help: `+=` can be used on `usize`, you can dereference `y` + | +LL | *y += 1; + | + + +error: aborting due to 4 previous errors -Some errors have detailed explanations: E0070, E0368. +Some errors have detailed explanations: E0070, E0308, E0368. For more information about an error, try `rustc --explain E0070`. diff --git a/src/test/ui/typeck/issue-93486.stderr b/src/test/ui/typeck/issue-93486.stderr index 95eb021965f..167edc8942a 100644 --- a/src/test/ui/typeck/issue-93486.stderr +++ b/src/test/ui/typeck/issue-93486.stderr @@ -6,7 +6,7 @@ LL | vec![].last_mut().unwrap() = 3_u8; | | | cannot assign to this expression | -help: consider dereferencing here to assign to the mutable borrowed piece of memory +help: consider dereferencing here to assign to the mutably borrowed value | LL | *vec![].last_mut().unwrap() = 3_u8; | + |
