Skip to content

Commit 4ce6b9a

Browse files
committed
Auto merge of #46859 - gereeter:uninhabited-unions, r=eddyb
Only mark unions as uninhabited if all of their fields are uninhabited Fixes #46845.
2 parents 11a24d9 + da97917 commit 4ce6b9a

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/librustc/ty/layout.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1352,17 +1352,6 @@ impl<'a, 'tcx> LayoutDetails {
13521352
}).collect::<Result<Vec<_>, _>>()
13531353
}).collect::<Result<Vec<_>, _>>()?;
13541354

1355-
let (inh_first, inh_second) = {
1356-
let mut inh_variants = (0..variants.len()).filter(|&v| {
1357-
variants[v].iter().all(|f| f.abi != Abi::Uninhabited)
1358-
});
1359-
(inh_variants.next(), inh_variants.next())
1360-
};
1361-
if inh_first.is_none() {
1362-
// Uninhabited because it has no variants, or only uninhabited ones.
1363-
return Ok(tcx.intern_layout(LayoutDetails::uninhabited(0)));
1364-
}
1365-
13661355
if def.is_union() {
13671356
let packed = def.repr.packed();
13681357
if packed && def.repr.align > 0 {
@@ -1400,6 +1389,17 @@ impl<'a, 'tcx> LayoutDetails {
14001389
}));
14011390
}
14021391

1392+
let (inh_first, inh_second) = {
1393+
let mut inh_variants = (0..variants.len()).filter(|&v| {
1394+
variants[v].iter().all(|f| f.abi != Abi::Uninhabited)
1395+
});
1396+
(inh_variants.next(), inh_variants.next())
1397+
};
1398+
if inh_first.is_none() {
1399+
// Uninhabited because it has no variants, or only uninhabited ones.
1400+
return Ok(tcx.intern_layout(LayoutDetails::uninhabited(0)));
1401+
}
1402+
14031403
let is_struct = !def.is_enum() ||
14041404
// Only one variant is inhabited.
14051405
(inh_second.is_none() &&

src/test/run-pass/issue-46845.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
union Bar {
28+
_a: (Never, u64),
29+
_b: (u64, Never)
30+
}
31+
32+
fn main() {
33+
assert_eq!(mem::size_of::<Foo>(), 8);
34+
assert_eq!(mem::size_of::<Bar>(), 0);
35+
36+
let f = [Foo { a: 42 }, Foo { a: 10 }];
37+
println!("{}", unsafe { f[0].a });
38+
assert_eq!(unsafe { f[1].a }, 10);
39+
}

0 commit comments

Comments
 (0)