]> git.proxmox.com Git - rustc.git/blob - tests/ui/binding/issue-53114-safety-checks.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / tests / ui / binding / issue-53114-safety-checks.rs
1 // Issue #53114: NLL's borrow check had some deviations from the old borrow
2 // checker, and both had some deviations from our ideal state. This test
3 // captures the behavior of how `_` bindings are handled with respect to how we
4 // flag expressions that are meant to request unsafe blocks.
5
6 #[derive(Copy, Clone)]
7 struct I(i64);
8 #[derive(Copy, Clone)]
9 struct F(f64);
10
11 union U { a: I, b: F }
12
13 #[repr(packed)]
14 struct P {
15 a: &'static i8,
16 b: &'static u32,
17 }
18
19 fn let_wild_gets_unsafe_field() {
20 let u1 = U { a: I(0) };
21 let u2 = U { a: I(1) };
22 let p = P { a: &2, b: &3 };
23 let _ = &p.b; //~ ERROR reference to packed field
24 let _ = u1.a; //~ ERROR [E0133]
25 let _ = &u2.a; //~ ERROR [E0133]
26
27 // variation on above with `_` in substructure
28 let (_,) = (&p.b,); //~ ERROR reference to packed field
29 let (_,) = (u1.a,); //~ ERROR [E0133]
30 let (_,) = (&u2.a,); //~ ERROR [E0133]
31 }
32
33 fn let_ascribe_gets_unsafe_field() {
34 let u1 = U { a: I(0) };
35 let u2 = U { a: I(1) };
36 let p = P { a: &2, b: &3 };
37 let _: _ = &p.b; //~ ERROR reference to packed field
38 let _: _ = u1.a; //~ ERROR [E0133]
39 let _: _ = &u2.a; //~ ERROR [E0133]
40
41 // variation on above with `_` in substructure
42 let (_,): _ = (&p.b,); //~ ERROR reference to packed field
43 let (_,): _ = (u1.a,); //~ ERROR [E0133]
44 let (_,): _ = (&u2.a,); //~ ERROR [E0133]
45 }
46
47 fn match_unsafe_field_to_wild() {
48 let u1 = U { a: I(0) };
49 let u2 = U { a: I(1) };
50 let p = P { a: &2, b: &3 };
51 match &p.b { _ => { } } //~ ERROR reference to packed field
52 match u1.a { _ => { } } //~ ERROR [E0133]
53 match &u2.a { _ => { } } //~ ERROR [E0133]
54
55 // variation on above with `_` in substructure
56 match (&p.b,) { (_,) => { } } //~ ERROR reference to packed field
57 match (u1.a,) { (_,) => { } } //~ ERROR [E0133]
58 match (&u2.a,) { (_,) => { } } //~ ERROR [E0133]
59 }
60
61 fn main() { }