summaryrefslogtreecommitdiffstats
path: root/tests/ui/nll/match-on-borrowed.rs
blob: 447dabeb47e598f619a5d916c1dd1b11853079cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Test that a (partially) mutably borrowed place can be matched on, so long as
// we don't have to read any values that are mutably borrowed to determine
// which arm to take.
//
// Test that we don't allow mutating the value being matched on in a way that
// changes which patterns it matches, until we have chosen an arm.

struct A(i32, i32);

fn struct_example(mut a: A) {
    let x = &mut a.0;
    match a { // OK, no access of borrowed data
        _ if false => (),
        A(_, r) => (),
    }
    x;
}

fn indirect_struct_example(mut b: &mut A) {
    let x = &mut b.0;
    match *b { // OK, no access of borrowed data
        _ if false => (),
        A(_, r) => (),
    }
    x;
}

fn underscore_example(mut c: i32) {
    let r = &mut c;
    match c { // OK, no access of borrowed data (or any data at all)
        _ if false => (),
        _ => (),
    }
    r;
}

enum E {
    V(i32, i32),
    W,
}

fn enum_example(mut e: E) {
    let x = match e {
        E::V(ref mut x, _) => x,
        E::W => panic!(),
    };
    match e { // Don't know that E uses a tag for its discriminant
        //~^ ERROR
        _ if false => (),
        E::V(_, r) => (),
        E::W => (),
    }
    x;
}

fn indirect_enum_example(mut f: &mut E) {
    let x = match *f {
        E::V(ref mut x, _) => x,
        E::W => panic!(),
    };
    match f { // Don't know that E uses a tag for its discriminant
        //~^ ERROR
        _ if false => (),
        E::V(_, r) => (),
        E::W => (),
    }
    x;
}

fn match_on_muatbly_borrowed_ref(mut p: &bool) {
    let r = &mut p;
    match *p { // OK, no access at all
        _ if false => (),
        _ => (),
    }
    r;
}

fn match_on_borrowed(mut t: bool) {
    let x = &mut t;
    match t {
        //~^ ERROR
        true => (),
        false => (),
    }
    x;
}

enum Never {}

fn never_init() {
    let n: Never;
    match n {} //~ ERROR
}

fn main() {}