]> git.lizzy.rs Git - rust.git/blob - src/test/ui/binop/binop-move-semantics.rs
:arrow_up: rust-analyzer
[rust.git] / src / test / ui / binop / binop-move-semantics.rs
1 // Test that move restrictions are enforced on overloaded binary operations
2
3 use std::ops::Add;
4
5 fn double_move<T: Add<Output=()>>(x: T) {
6     x
7     +
8     x;  //~ ERROR: use of moved value
9 }
10
11 fn move_then_borrow<T: Add<Output=()> + Clone>(x: T) {
12     x
13     +
14     x.clone();  //~ ERROR: borrow of moved value
15 }
16
17 fn move_borrowed<T: Add<Output=()>>(x: T, mut y: T) {
18     let m = &x;
19     let n = &mut y;
20
21     x  //~ ERROR: cannot move out of `x` because it is borrowed
22     +
23     y;  //~ ERROR: cannot move out of `y` because it is borrowed
24     use_mut(n); use_imm(m);
25 }
26 fn illegal_dereference<T: Add<Output=()>>(mut x: T, y: T) {
27     let m = &mut x;
28     let n = &y;
29
30     *m  //~ ERROR: cannot move
31     +
32     *n;  //~ ERROR: cannot move
33     use_imm(n); use_mut(m);
34 }
35 struct Foo;
36
37 impl<'a, 'b> Add<&'b Foo> for &'a mut Foo {
38     type Output = ();
39
40     fn add(self, _: &Foo) {}
41 }
42
43 impl<'a, 'b> Add<&'b mut Foo> for &'a Foo {
44     type Output = ();
45
46     fn add(self, _: &mut Foo) {}
47 }
48
49 fn mut_plus_immut() {
50     let mut f = Foo;
51
52     &mut f
53     +
54     &f;  //~ ERROR: cannot borrow `f` as immutable because it is also borrowed as mutable
55 }
56
57 fn immut_plus_mut() {
58     let mut f = Foo;
59
60     &f
61     +
62     &mut f;  //~ ERROR: cannot borrow `f` as mutable because it is also borrowed as immutable
63 }
64
65 fn main() {}
66
67 fn use_mut<T>(_: &mut T) { }
68 fn use_imm<T>(_: &T) { }