]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/match-guards-partially-borrow.rs
fix merge conflicts
[rust.git] / src / test / ui / nll / match-guards-partially-borrow.rs
1 // Test that a (partially) mutably borrowed place can be matched on, so long as
2 // we don't have to read any values that are mutably borrowed to determine
3 // which arm to take.
4 //
5 // Test that we don't allow mutating the value being matched on in a way that
6 // changes which patterns it matches, until we have chosen an arm.
7
8
9 #![feature(bind_by_move_pattern_guards)]
10 #![feature(nll)]
11
12 fn ok_mutation_in_guard(mut q: i32) {
13     match q {
14         // OK, mutation doesn't change which patterns g matches
15         _ if { q = 1; false } => (),
16         _ => (),
17     }
18 }
19
20 fn ok_mutation_in_guard2(mut u: bool) {
21     // OK value of u is unused before modification
22     match u {
23         _ => (),
24         _ if {
25             u = true;
26             false
27         } => (),
28         x => (),
29     }
30 }
31
32 fn ok_mutation_in_guard4(mut w: (&mut bool,)) {
33     // OK value of u is unused before modification
34     match w {
35         _ => (),
36         _ if {
37             *w.0 = true;
38             false
39         } => (),
40         x => (),
41     }
42 }
43
44 fn ok_indirect_mutation_in_guard(mut p: &bool) {
45     match *p {
46         // OK, mutation doesn't change which patterns s matches
47         _ if {
48             p = &true;
49             false
50         } => (),
51         _ => (),
52     }
53 }
54
55 fn mutation_invalidates_pattern_in_guard(mut q: bool) {
56     match q {
57         // q doesn't match the pattern with the guard by the end of the guard.
58         false if {
59             q = true; //~ ERROR
60             true
61         } => (),
62         _ => (),
63     }
64 }
65
66 fn mutation_invalidates_previous_pattern_in_guard(mut r: bool) {
67     match r {
68         // r matches a previous pattern by the end of the guard.
69         true => (),
70         _ if {
71             r = true; //~ ERROR
72             true
73         } => (),
74         _ => (),
75     }
76 }
77
78 fn match_on_borrowed_early_end(mut s: bool) {
79     let h = &mut s;
80     // OK value of s is unused before modification.
81     match s {
82         _ if {
83             *h = !*h;
84             false
85         } => (),
86         true => (),
87         false => (),
88     }
89 }
90
91 fn bad_mutation_in_guard(mut t: bool) {
92     match t {
93         true => (),
94         false if {
95             t = true; //~ ERROR
96             false
97         } => (),
98         false => (),
99     }
100 }
101
102 fn bad_mutation_in_guard2(mut x: Option<Option<&i32>>) {
103     // Check that nested patterns are checked.
104     match x {
105         None => (),
106         Some(None) => (),
107         _ if {
108             match x {
109                 Some(ref mut r) => *r = None, //~ ERROR
110                 _ => return,
111             };
112             false
113         } => (),
114         Some(Some(r)) => println!("{}", r),
115     }
116 }
117
118 fn bad_mutation_in_guard3(mut t: bool) {
119     match t {
120         s if {
121             t = !t; //~ ERROR
122             false
123         } => (), // What value should `s` have in the arm?
124         _ => (),
125     }
126 }
127
128 fn bad_indirect_mutation_in_guard(mut y: &bool) {
129     match *y {
130         true => (),
131         false if {
132             y = &true; //~ ERROR
133             false
134         } => (),
135         false => (),
136     }
137 }
138
139 fn bad_indirect_mutation_in_guard2(mut z: &bool) {
140     match z {
141         &true => (),
142         &false if {
143             z = &true; //~ ERROR
144             false
145         } => (),
146         &false => (),
147     }
148 }
149
150 fn bad_indirect_mutation_in_guard3(mut a: &bool) {
151     // Same as bad_indirect_mutation_in_guard2, but using match ergonomics
152     match a {
153         true => (),
154         false if {
155             a = &true; //~ ERROR
156             false
157         } => (),
158         false => (),
159     }
160 }
161
162 fn bad_indirect_mutation_in_guard4(mut b: &bool) {
163     match b {
164         &_ => (),
165         &_ if {
166             b = &true; //~ ERROR
167             false
168         } => (),
169         &b => (),
170     }
171 }
172
173 fn main() {}