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