Skip to content

Commit dff2b39

Browse files
author
Jorge Aparicio
committed
Test binops move semantics
1 parent 949b55e commit dff2b39

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
fn double_move<T: Add<T, ()>>(x: T) {
14+
x
15+
+
16+
x; //~ ERROR: use of moved value
17+
}
18+
19+
fn move_then_borrow<T: Add<T, ()> + Clone>(x: T) {
20+
x
21+
+
22+
x.clone(); //~ ERROR: use of moved value
23+
}
24+
25+
fn move_borrowed<T: Add<T, ()>>(x: T, mut y: T) {
26+
let m = &x;
27+
let n = &mut y;
28+
29+
x //~ ERROR: cannot move out of `x` because it is borrowed
30+
+
31+
y; //~ ERROR: cannot move out of `y` because it is borrowed
32+
}
33+
34+
fn illegal_dereference<T: Add<T, ()>>(mut x: T, y: T) {
35+
let m = &mut x;
36+
let n = &y;
37+
38+
*m //~ ERROR: cannot move out of dereference of `&mut`-pointer
39+
+
40+
*n; //~ ERROR: cannot move out of dereference of `&`-pointer
41+
}
42+
43+
struct Foo;
44+
45+
impl<'a, 'b> Add<&'b Foo, ()> for &'a mut Foo {
46+
fn add(self, _: &Foo) {}
47+
}
48+
49+
impl<'a, 'b> Add<&'b mut Foo, ()> for &'a Foo {
50+
fn add(self, _: &mut Foo) {}
51+
}
52+
53+
fn mut_plus_immut() {
54+
let mut f = Foo;
55+
56+
&mut f
57+
+
58+
&f; //~ ERROR: cannot borrow `f` as immutable because it is also borrowed as mutable
59+
}
60+
61+
fn immut_plus_mut() {
62+
let mut f = Foo;
63+
64+
&f
65+
+
66+
&mut f; //~ ERROR: cannot borrow `f` as mutable because it is also borrowed as immutable
67+
}
68+
69+
fn main() {}

0 commit comments

Comments
 (0)