]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/moves-based-on-type-exprs.rs
Imported Upstream version 1.0.0-alpha.2
[rustc.git] / src / test / compile-fail / moves-based-on-type-exprs.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 // Tests that references to move-by-default values trigger moves when
12 // they occur as part of various kinds of expressions.
13
14
15 struct Foo<A> { f: A }
16 fn guard(_s: String) -> bool {panic!()}
17 fn touch<A>(_a: &A) {}
18
19 fn f10() {
20 let x = "hi".to_string();
21 let _y = Foo { f:x };
22 touch(&x); //~ ERROR use of moved value: `x`
23 }
24
25 fn f20() {
26 let x = "hi".to_string();
27 let _y = (x, 3);
28 touch(&x); //~ ERROR use of moved value: `x`
29 }
30
31 fn f21() {
32 let x = vec!(1, 2, 3);
33 let _y = (x[0], 3);
34 touch(&x);
35 }
36
37 fn f30(cond: bool) {
38 let x = "hi".to_string();
39 let y = "ho".to_string();
40 let _y = if cond {
41 x
42 } else {
43 y
44 };
45 touch(&x); //~ ERROR use of moved value: `x`
46 touch(&y); //~ ERROR use of moved value: `y`
47 }
48
49 fn f40(cond: bool) {
50 let x = "hi".to_string();
51 let y = "ho".to_string();
52 let _y = match cond {
53 true => x,
54 false => y
55 };
56 touch(&x); //~ ERROR use of moved value: `x`
57 touch(&y); //~ ERROR use of moved value: `y`
58 }
59
60 fn f50(cond: bool) {
61 let x = "hi".to_string();
62 let y = "ho".to_string();
63 let _y = match cond {
64 _ if guard(x) => 10,
65 true => 10,
66 false => 20,
67 };
68 touch(&x); //~ ERROR use of moved value: `x`
69 touch(&y);
70 }
71
72 fn f70() {
73 let x = "hi".to_string();
74 let _y = [x];
75 touch(&x); //~ ERROR use of moved value: `x`
76 }
77
78 fn f80() {
79 let x = "hi".to_string();
80 let _y = vec!(x);
81 touch(&x); //~ ERROR use of moved value: `x`
82 }
83
84 fn f100() {
85 let x = vec!("hi".to_string());
86 let _y = x.into_iter().next().unwrap();
87 touch(&x); //~ ERROR use of moved value: `x`
88 }
89
90 fn f110() {
91 let x = vec!("hi".to_string());
92 let _y = [x.into_iter().next().unwrap(); 1];
93 touch(&x); //~ ERROR use of moved value: `x`
94 }
95
96 fn f120() {
97 let mut x = vec!("hi".to_string(), "ho".to_string());
98 x.swap(0, 1);
99 touch(&x[0]);
100 touch(&x[1]);
101 }
102
103 fn main() {}