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