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