]> git.lizzy.rs Git - rust.git/blob - tests/ui/moves/moves-based-on-type-exprs.rs
Rollup merge of #106446 - bzEq:fix-unwind-lsda, r=Amanieu
[rust.git] / tests / ui / moves / moves-based-on-type-exprs.rs
1 // Tests that references to move-by-default values trigger moves when
2 // they occur as part of various kinds of expressions.
3
4
5 struct Foo<A> { f: A }
6 fn guard(_s: String) -> bool {panic!()}
7 fn touch<A>(_a: &A) {}
8
9 fn f10() {
10     let x = "hi".to_string();
11     let _y = Foo { f:x };
12     touch(&x); //~ ERROR borrow of moved value: `x`
13 }
14
15 fn f20() {
16     let x = "hi".to_string();
17     let _y = (x, 3);
18     touch(&x); //~ ERROR borrow of moved value: `x`
19 }
20
21 fn f21() {
22     let x = vec![1, 2, 3];
23     let _y = (x[0], 3);
24     touch(&x);
25 }
26
27 fn f30(cond: bool) {
28     let x = "hi".to_string();
29     let y = "ho".to_string();
30     let _y = if cond {
31         x
32     } else {
33         y
34     };
35     touch(&x); //~ ERROR borrow of moved value: `x`
36     touch(&y); //~ ERROR borrow of moved value: `y`
37 }
38
39 fn f40(cond: bool) {
40     let x = "hi".to_string();
41     let y = "ho".to_string();
42     let _y = match cond {
43         true => x,
44         false => y
45     };
46     touch(&x); //~ ERROR borrow of moved value: `x`
47     touch(&y); //~ ERROR borrow of moved value: `y`
48 }
49
50 fn f50(cond: bool) {
51     let x = "hi".to_string();
52     let y = "ho".to_string();
53     let _y = match cond {
54         _ if guard(x) => 10,
55         true => 10,
56         false => 20,
57     };
58     touch(&x); //~ ERROR borrow of moved value: `x`
59     touch(&y);
60 }
61
62 fn f70() {
63     let x = "hi".to_string();
64     let _y = [x];
65     touch(&x); //~ ERROR borrow of moved value: `x`
66 }
67
68 fn f80() {
69     let x = "hi".to_string();
70     let _y = vec![x];
71     touch(&x); //~ ERROR borrow of moved value: `x`
72 }
73
74 fn f100() {
75     let x = vec!["hi".to_string()];
76     let _y = x.into_iter().next().unwrap();
77     touch(&x); //~ ERROR borrow of moved value: `x`
78 }
79
80 fn f110() {
81     let x = vec!["hi".to_string()];
82     let _y = [x.into_iter().next().unwrap(); 1];
83     touch(&x); //~ ERROR borrow of moved value: `x`
84 }
85
86 fn f120() {
87     let mut x = vec!["hi".to_string(), "ho".to_string()];
88     x.swap(0, 1);
89     touch(&x[0]);
90     touch(&x[1]);
91 }
92
93 fn main() {}