]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/issue-57100.rs
fix merge conflicts
[rust.git] / src / test / ui / nll / issue-57100.rs
1 #![allow(unused)]
2 #![feature(nll)]
3
4 // ignore-tidy-linelength
5
6 // This tests the error messages for borrows of union fields when the unions are embedded in other
7 // structs or unions.
8
9 #[derive(Clone, Copy, Default)]
10 struct Leaf {
11     l1_u8: u8,
12     l2_u8: u8,
13 }
14
15 #[derive(Clone, Copy)]
16 union First {
17     f1_leaf: Leaf,
18     f2_leaf: Leaf,
19     f3_union: Second,
20 }
21
22 #[derive(Clone, Copy)]
23 union Second {
24     s1_leaf: Leaf,
25     s2_leaf: Leaf,
26 }
27
28 struct Root {
29     r1_u8: u8,
30     r2_union: First,
31 }
32
33 // Borrow a different field of the nested union.
34 fn nested_union() {
35     unsafe {
36         let mut r = Root {
37             r1_u8: 3,
38             r2_union: First { f3_union: Second { s2_leaf: Leaf { l1_u8: 8, l2_u8: 4 } } }
39         };
40
41         let mref = &mut r.r2_union.f3_union.s1_leaf.l1_u8;
42         //                                  ^^^^^^^
43         *mref = 22;
44         let nref = &r.r2_union.f3_union.s2_leaf.l1_u8;
45         //                              ^^^^^^^
46         //~^^ ERROR cannot borrow `r.r2_union.f3_union` (via `r.r2_union.f3_union.s2_leaf.l1_u8`) as immutable because it is also borrowed as mutable (via `r.r2_union.f3_union.s1_leaf.l1_u8`) [E0502]
47         println!("{} {}", mref, nref)
48     }
49 }
50
51 // Borrow a different field of the first union.
52 fn first_union() {
53     unsafe {
54         let mut r = Root {
55             r1_u8: 3,
56             r2_union: First { f3_union: Second { s2_leaf: Leaf { l1_u8: 8, l2_u8: 4 } } }
57         };
58
59         let mref = &mut r.r2_union.f2_leaf.l1_u8;
60         //                         ^^^^^^^
61         *mref = 22;
62         let nref = &r.r2_union.f1_leaf.l1_u8;
63         //                     ^^^^^^^
64         //~^^ ERROR cannot borrow `r.r2_union` (via `r.r2_union.f1_leaf.l1_u8`) as immutable because it is also borrowed as mutable (via `r.r2_union.f2_leaf.l1_u8`) [E0502]
65         println!("{} {}", mref, nref)
66     }
67 }
68
69 fn main() {}