rust/tests/ui/borrowck/augmented-assignments.rs
Jieyou Xu 40b73322b9
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/`.
2025-04-19 18:42:24 +08:00

31 lines
757 B
Rust

//! 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);
}