]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/match-on-borrowed.rs
Auto merge of #103600 - compiler-errors:early-binder-nits, r=spastorino
[rust.git] / src / test / ui / nll / match-on-borrowed.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 struct A(i32, i32);
9
10 fn struct_example(mut a: A) {
11     let x = &mut a.0;
12     match a { // OK, no access of borrowed data
13         _ if false => (),
14         A(_, r) => (),
15     }
16     x;
17 }
18
19 fn indirect_struct_example(mut b: &mut A) {
20     let x = &mut b.0;
21     match *b { // OK, no access of borrowed data
22         _ if false => (),
23         A(_, r) => (),
24     }
25     x;
26 }
27
28 fn underscore_example(mut c: i32) {
29     let r = &mut c;
30     match c { // OK, no access of borrowed data (or any data at all)
31         _ if false => (),
32         _ => (),
33     }
34     r;
35 }
36
37 enum E {
38     V(i32, i32),
39     W,
40 }
41
42 fn enum_example(mut e: E) {
43     let x = match e {
44         E::V(ref mut x, _) => x,
45         E::W => panic!(),
46     };
47     match e { // Don't know that E uses a tag for its discriminant
48         //~^ ERROR
49         _ if false => (),
50         E::V(_, r) => (),
51         E::W => (),
52     }
53     x;
54 }
55
56 fn indirect_enum_example(mut f: &mut E) {
57     let x = match *f {
58         E::V(ref mut x, _) => x,
59         E::W => panic!(),
60     };
61     match f { // Don't know that E uses a tag for its discriminant
62         //~^ ERROR
63         _ if false => (),
64         E::V(_, r) => (),
65         E::W => (),
66     }
67     x;
68 }
69
70 fn match_on_muatbly_borrowed_ref(mut p: &bool) {
71     let r = &mut p;
72     match *p { // OK, no access at all
73         _ if false => (),
74         _ => (),
75     }
76     r;
77 }
78
79 fn match_on_borrowed(mut t: bool) {
80     let x = &mut t;
81     match t {
82         //~^ ERROR
83         true => (),
84         false => (),
85     }
86     x;
87 }
88
89 enum Never {}
90
91 fn never_init() {
92     let n: Never;
93     match n {} //~ ERROR
94 }
95
96 fn main() {}