]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-unsafe.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / union / union-unsafe.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(untagged_unions)]
12
13 union U1 {
14     a: u8
15 }
16
17 union U2 {
18     a: String
19 }
20
21 union U3<T> {
22     a: T
23 }
24
25 union U4<T: Copy> {
26     a: T
27 }
28
29 fn generic_noncopy<T: Default>() {
30     let mut u3 = U3 { a: T::default() };
31     u3.a = T::default(); //~ ERROR assignment to non-`Copy` union field is unsafe
32 }
33
34 fn generic_copy<T: Copy + Default>() {
35     let mut u3 = U3 { a: T::default() };
36     u3.a = T::default(); // OK
37     let mut u4 = U4 { a: T::default() };
38     u4.a = T::default(); // OK
39 }
40
41 fn main() {
42     let mut u1 = U1 { a: 10 }; // OK
43     let a = u1.a; //~ ERROR access to union field is unsafe
44     u1.a = 11; // OK
45     let U1 { a } = u1; //~ ERROR access to union field is unsafe
46     if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe
47     // let U1 { .. } = u1; // OK
48
49     let mut u2 = U2 { a: String::from("old") }; // OK
50     u2.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field is unsafe
51     let mut u3 = U3 { a: 0 }; // OK
52     u3.a = 1; // OK
53     let mut u3 = U3 { a: String::from("old") }; // OK
54     u3.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field is unsafe
55 }