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