]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/issue-46845.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / test / run-pass / issue-46845.rs
1 // Copyright 2017 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 // To work around #46855
12 // compile-flags: -Z mir-opt-level=0
13 // Regression test for the inhabitedness of unions with uninhabited variants, issue #46845
14
15 use std::mem;
16
17 #[derive(Copy, Clone)]
18 enum Never { }
19
20 // A single uninhabited variant shouldn't make the whole union uninhabited.
21 union Foo {
22 a: u64,
23 _b: Never
24 }
25
26 // If all the variants are uninhabited, however, the union should be uninhabited.
27 // NOTE(#49298) the union being uninhabited shouldn't change its size.
28 union Bar {
29 _a: (Never, u64),
30 _b: (u64, Never)
31 }
32
33 fn main() {
34 assert_eq!(mem::size_of::<Foo>(), 8);
35 // See the note on `Bar`'s definition for why this isn't `0`.
36 assert_eq!(mem::size_of::<Bar>(), 8);
37
38 let f = [Foo { a: 42 }, Foo { a: 10 }];
39 println!("{}", unsafe { f[0].a });
40 assert_eq!(unsafe { f[1].a }, 10);
41 }