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