diff options
| author | Jieyou Xu <jieyouxu@outlook.com> | 2025-04-19 17:05:05 +0800 |
|---|---|---|
| committer | Jieyou Xu <jieyouxu@outlook.com> | 2025-04-19 18:42:24 +0800 |
| commit | 40b73322b93bf1627a7cb58aec5a7fe019af1f66 (patch) | |
| tree | 1bde0dc2997685040bfcd3c53b48d6f8e4e0f8d7 /tests/ui/borrowck | |
| parent | a86df238d3f21ccb6ec25080bcc24b1f9153ea63 (diff) | |
| download | rust-40b73322b93bf1627a7cb58aec5a7fe019af1f66.tar.gz rust-40b73322b93bf1627a7cb58aec5a7fe019af1f66.zip | |
tests: adjust some `augmented-assignment*` tests
- `tests/ui/augmented-assignment-feature-gate-cross.rs`:
- This was *originally* to feature-gate overloaded OpAssign
cross-crate, but now let's keep it as a smoke test.
- Renamed as `augmented-assignment-cross-crate.rs`.
- Relocated under `tests/ui/binop/`.
- `tests/ui/augmented-assignments.rs`:
- Documented test intent.
- Moved under `tests/ui/borrowck/`.
- `tests/ui/augmented-assignment-rpass.rs`:
- Renamed to drop the `-rpass` suffix, since this was leftover from
when `run-pass` test suite was a thing.
- Moved under `tests/ui/binop/`.
Diffstat (limited to 'tests/ui/borrowck')
| -rw-r--r-- | tests/ui/borrowck/augmented-assignments.rs | 31 | ||||
| -rw-r--r-- | tests/ui/borrowck/augmented-assignments.stderr | 26 |
2 files changed, 57 insertions, 0 deletions
diff --git a/tests/ui/borrowck/augmented-assignments.rs b/tests/ui/borrowck/augmented-assignments.rs new file mode 100644 index 00000000000..d717dcc7935 --- /dev/null +++ b/tests/ui/borrowck/augmented-assignments.rs @@ -0,0 +1,31 @@ +//! Check that overloaded compound assignment operators respect usual borrowck rules and emit +//! reasonable diagnostics. + +use std::ops::AddAssign; + +#[derive(Clone)] +struct Int(i32); + +impl AddAssign for Int { + fn add_assign(&mut self, _: Int) { + unimplemented!() + } +} + +fn main() { + let mut x = Int(1); //~ NOTE binding `x` declared here + x + //~^ NOTE borrow of `x` occurs here + += + x; + //~^ ERROR cannot move out of `x` because it is borrowed + //~| NOTE move out of `x` occurs here + + let y = Int(2); + //~^ HELP consider changing this to be mutable + //~| SUGGESTION mut + y //~ ERROR cannot borrow `y` as mutable, as it is not declared as mutable + //~| NOTE cannot borrow as mutable + += + Int(1); +} diff --git a/tests/ui/borrowck/augmented-assignments.stderr b/tests/ui/borrowck/augmented-assignments.stderr new file mode 100644 index 00000000000..4b945cd998a --- /dev/null +++ b/tests/ui/borrowck/augmented-assignments.stderr @@ -0,0 +1,26 @@ +error[E0505]: cannot move out of `x` because it is borrowed + --> $DIR/augmented-assignments.rs:20:5 + | +LL | let mut x = Int(1); + | ----- binding `x` declared here +LL | x + | - borrow of `x` occurs here +... +LL | x; + | ^ move out of `x` occurs here + +error[E0596]: cannot borrow `y` as mutable, as it is not declared as mutable + --> $DIR/augmented-assignments.rs:27:5 + | +LL | y + | ^ cannot borrow as mutable + | +help: consider changing this to be mutable + | +LL | let mut y = Int(2); + | +++ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0505, E0596. +For more information about an error, try `rustc --explain E0505`. |
