Skip to content

Commit c1fe4a2

Browse files
committed
Only mark unions as uninhabited if all of their fields are uninhabited. Fixes #46845.
1 parent b76f224 commit c1fe4a2

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

src/librustc/ty/layout.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,22 +1352,19 @@ 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 {
13691358
bug!("Union cannot be packed and aligned");
13701359
}
1360+
if variants.len() != 1 {
1361+
bug!("Union must be represented as a single variant");
1362+
}
1363+
1364+
if variants[0].iter().all(|f| f.abi == Abi::Uninhabited) {
1365+
// Uninhabited because it has only uninhabited variants/fields.
1366+
return Ok(tcx.intern_layout(LayoutDetails::uninhabited(0)));
1367+
}
13711368

13721369
let mut align = if def.repr.packed() {
13731370
dl.i8_align
@@ -1400,6 +1397,17 @@ impl<'a, 'tcx> LayoutDetails {
14001397
}));
14011398
}
14021399

1400+
let (inh_first, inh_second) = {
1401+
let mut inh_variants = (0..variants.len()).filter(|&v| {
1402+
variants[v].iter().all(|f| f.abi != Abi::Uninhabited)
1403+
});
1404+
(inh_variants.next(), inh_variants.next())
1405+
};
1406+
if inh_first.is_none() {
1407+
// Uninhabited because it has no variants, or only uninhabited ones.
1408+
return Ok(tcx.intern_layout(LayoutDetails::uninhabited(0)));
1409+
}
1410+
14031411
let is_struct = !def.is_enum() ||
14041412
// Only one variant is inhabited.
14051413
(inh_second.is_none() &&

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

Lines changed: 39 additions & 0 deletions
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)