]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/binop-move-semantics.rs
Fix invalid associated type rendering in rustdoc
[rust.git] / src / test / compile-fail / binop-move-semantics.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that move restrictions are enforced on overloaded binary operations
12
13 use std::ops::Add;
14
15 fn double_move<T: Add<Output=()>>(x: T) {
16     x
17     +
18     x;  //~ ERROR: use of moved value
19 }
20
21 fn move_then_borrow<T: Add<Output=()> + Clone>(x: T) {
22     x
23     +
24     x.clone();  //~ ERROR: use of moved value
25 }
26
27 fn move_borrowed<T: Add<Output=()>>(x: T, mut y: T) {
28     let m = &x;
29     let n = &mut y;
30
31     x  //~ ERROR: cannot move out of `x` because it is borrowed
32     +
33     y;  //~ ERROR: cannot move out of `y` because it is borrowed
34 }
35
36 fn illegal_dereference<T: Add<Output=()>>(mut x: T, y: T) {
37     let m = &mut x;
38     let n = &y;
39
40     *m  //~ ERROR: cannot move out of borrowed content
41     +
42     *n;  //~ ERROR: cannot move out of borrowed content
43 }
44
45 struct Foo;
46
47 impl<'a, 'b> Add<&'b Foo> for &'a mut Foo {
48     type Output = ();
49
50     fn add(self, _: &Foo) {}
51 }
52
53 impl<'a, 'b> Add<&'b mut Foo> for &'a Foo {
54     type Output = ();
55
56     fn add(self, _: &mut Foo) {}
57 }
58
59 fn mut_plus_immut() {
60     let mut f = Foo;
61
62     &mut f
63     +
64     &f;  //~ ERROR: cannot borrow `f` as immutable because it is also borrowed as mutable
65     //~^ cannot borrow `f` as immutable because it is also borrowed as mutable
66 }
67
68 fn immut_plus_mut() {
69     let mut f = Foo;
70
71     &f
72     +
73     &mut f;  //~ ERROR: cannot borrow `f` as mutable because it is also borrowed as immutable
74     //~^ cannot borrow `f` as mutable because it is also borrowed as immutable
75 }
76
77 fn main() {}