summaryrefslogtreecommitdiffstats
path: root/tests/ui/binding/issue-53114-safety-checks.rs
blob: f4be2b482a7e65eb2ee059d5a2c8bdaca2f33d5d (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
// Issue #53114: NLL's borrow check had some deviations from the old borrow
// checker, and both had some deviations from our ideal state. This test
// captures the behavior of how `_` bindings are handled with respect to how we
// flag expressions that are meant to request unsafe blocks.

#[derive(Copy, Clone)]
struct I(i64);
#[derive(Copy, Clone)]
struct F(f64);

union U { a: I, b: F }

#[repr(packed)]
struct P {
    a: &'static i8,
    b: &'static u32,
}

fn let_wild_gets_unsafe_field() {
    let u1 = U { a: I(0) };
    let u2 = U { a: I(1) };
    let p = P { a: &2, b: &3 };
    let _ = &p.b;  //~ ERROR    reference to packed field
    let _ = u1.a;  //~ ERROR  [E0133]
    let _ = &u2.a; //~ ERROR  [E0133]

    // variation on above with `_` in substructure
    let (_,) = (&p.b,);  //~ ERROR     reference to packed field
    let (_,) = (u1.a,);  //~ ERROR   [E0133]
    let (_,) = (&u2.a,); //~ ERROR   [E0133]
}

fn let_ascribe_gets_unsafe_field() {
    let u1 = U { a: I(0) };
    let u2 = U { a: I(1) };
    let p = P { a: &2, b: &3 };
    let _: _ = &p.b;  //~ ERROR    reference to packed field
    let _: _ = u1.a;  //~ ERROR  [E0133]
    let _: _ = &u2.a; //~ ERROR  [E0133]

    // variation on above with `_` in substructure
    let (_,): _ = (&p.b,);  //~ ERROR     reference to packed field
    let (_,): _ = (u1.a,);  //~ ERROR   [E0133]
    let (_,): _ = (&u2.a,); //~ ERROR   [E0133]
}

fn match_unsafe_field_to_wild() {
    let u1 = U { a: I(0) };
    let u2 = U { a: I(1) };
    let p = P { a: &2, b: &3 };
    match &p.b  { _ => { } } //~ ERROR     reference to packed field
    match u1.a  { _ => { } } //~ ERROR   [E0133]
    match &u2.a { _ => { } } //~ ERROR   [E0133]

    // variation on above with `_` in substructure
    match (&p.b,)  { (_,) => { } } //~ ERROR     reference to packed field
    match (u1.a,)  { (_,) => { } } //~ ERROR   [E0133]
    match (&u2.a,) { (_,) => { } } //~ ERROR   [E0133]
}

fn main() { }