]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-unsafe.rs
Auto merge of #62766 - alexcrichton:stabilize-pipelined-compilation, r=oli-obk
[rust.git] / src / test / ui / union / union-unsafe.rs
1 #![feature(untagged_unions)]
2
3 union U1 {
4     a: u8
5 }
6
7 union U2 {
8     a: String
9 }
10
11 union U3<T> {
12     a: T
13 }
14
15 union U4<T: Copy> {
16     a: T
17 }
18
19 fn generic_noncopy<T: Default>() {
20     let mut u3 = U3 { a: T::default() };
21     u3.a = T::default(); //~ ERROR assignment to non-`Copy` union field is unsafe
22 }
23
24 fn generic_copy<T: Copy + Default>() {
25     let mut u3 = U3 { a: T::default() };
26     u3.a = T::default(); // OK
27     let mut u4 = U4 { a: T::default() };
28     u4.a = T::default(); // OK
29 }
30
31 fn main() {
32     let mut u1 = U1 { a: 10 }; // OK
33     let a = u1.a; //~ ERROR access to union field is unsafe
34     u1.a = 11; // OK
35     let U1 { a } = u1; //~ ERROR access to union field is unsafe
36     if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe
37     // let U1 { .. } = u1; // OK
38
39     let mut u2 = U2 { a: String::from("old") }; // OK
40     u2.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field is unsafe
41     let mut u3 = U3 { a: 0 }; // OK
42     u3.a = 1; // OK
43     let mut u3 = U3 { a: String::from("old") }; // OK
44     u3.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field is unsafe
45 }