Skip to content

Commit 1bbae50

Browse files
mark payload fields of ScalarPair enums as Scalar::Union when they're not always initialized
1 parent 311e268 commit 1bbae50

File tree

3 files changed

+828
-16
lines changed

3 files changed

+828
-16
lines changed

compiler/rustc_middle/src/ty/layout.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -1120,21 +1120,12 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
11201120
match st[i].abi() {
11211121
Abi::Scalar(_) => Abi::Scalar(niche_scalar),
11221122
Abi::ScalarPair(first, second) => {
1123-
// We need to use scalar_unit to reset the
1124-
// valid range to the maximal one for that
1125-
// primitive, because only the niche is
1126-
// guaranteed to be initialised, not the
1127-
// other primitive.
1123+
// Only the niche is guaranteed to be initialised,
1124+
// so use union layout for the other primitive.
11281125
if offset.bytes() == 0 {
1129-
Abi::ScalarPair(
1130-
niche_scalar,
1131-
scalar_unit(second.primitive()),
1132-
)
1126+
Abi::ScalarPair(niche_scalar, second.to_union())
11331127
} else {
1134-
Abi::ScalarPair(
1135-
scalar_unit(first.primitive()),
1136-
niche_scalar,
1137-
)
1128+
Abi::ScalarPair(first.to_union(), niche_scalar)
11381129
}
11391130
}
11401131
_ => Abi::Aggregate { sized: true },
@@ -1329,22 +1320,30 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
13291320
} else {
13301321
// Try to use a ScalarPair for all tagged enums.
13311322
let mut common_prim = None;
1323+
let mut common_prim_initialized_in_all_variants = true;
13321324
for (field_layouts, layout_variant) in iter::zip(&variants, &layout_variants) {
13331325
let FieldsShape::Arbitrary { ref offsets, .. } = layout_variant.fields else {
13341326
bug!();
13351327
};
13361328
let mut fields =
13371329
iter::zip(field_layouts, offsets).filter(|p| !p.0.is_zst());
13381330
let (field, offset) = match (fields.next(), fields.next()) {
1339-
(None, None) => continue,
1331+
(None, None) => {
1332+
common_prim_initialized_in_all_variants = false;
1333+
continue;
1334+
}
13401335
(Some(pair), None) => pair,
13411336
_ => {
13421337
common_prim = None;
13431338
break;
13441339
}
13451340
};
13461341
let prim = match field.abi {
1347-
Abi::Scalar(scalar) => scalar.primitive(),
1342+
Abi::Scalar(scalar) => {
1343+
common_prim_initialized_in_all_variants &=
1344+
matches!(scalar, Scalar::Initialized { .. });
1345+
scalar.primitive()
1346+
}
13481347
_ => {
13491348
common_prim = None;
13501349
break;
@@ -1364,7 +1363,13 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
13641363
}
13651364
}
13661365
if let Some((prim, offset)) = common_prim {
1367-
let pair = self.scalar_pair(tag, scalar_unit(prim));
1366+
let prim_scalar = if common_prim_initialized_in_all_variants {
1367+
scalar_unit(prim)
1368+
} else {
1369+
// Common prim might be uninit.
1370+
Scalar::Union { value: prim }
1371+
};
1372+
let pair = self.scalar_pair(tag, prim_scalar);
13681373
let pair_offsets = match pair.fields {
13691374
FieldsShape::Arbitrary { ref offsets, ref memory_index } => {
13701375
assert_eq!(memory_index, &[0, 1]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#![crate_type = "lib"]
2+
#![feature(rustc_attrs)]
3+
4+
use std::mem::MaybeUninit;
5+
6+
enum HasNiche {
7+
A,
8+
B,
9+
C,
10+
}
11+
12+
// This should result in ScalarPair(Initialized, Union),
13+
// since the u8 payload will be uninit for `None`.
14+
#[rustc_layout(debug)]
15+
pub enum MissingPayloadField { //~ ERROR: layout_of
16+
Some(u8),
17+
None
18+
}
19+
20+
// This should result in ScalarPair(Initialized, Initialized),
21+
// since the u8 field is present in all variants,
22+
// and hence will always be initialized.
23+
#[rustc_layout(debug)]
24+
pub enum CommonPayloadField { //~ ERROR: layout_of
25+
A(u8),
26+
B(u8),
27+
}
28+
29+
// This should result in ScalarPair(Initialized, Union),
30+
// since, though a u8-sized field is present in all variants, it might be uninit.
31+
#[rustc_layout(debug)]
32+
pub enum CommonPayloadFieldIsMaybeUninit { //~ ERROR: layout_of
33+
A(u8),
34+
B(MaybeUninit<u8>),
35+
}
36+
37+
// This should result in ScalarPair(Initialized, Union),
38+
// since only the niche field (used for the tag) is guaranteed to be initialized.
39+
#[rustc_layout(debug)]
40+
pub enum NicheFirst { //~ ERROR: layout_of
41+
A(HasNiche, u8),
42+
B,
43+
C
44+
}
45+
46+
// This should result in ScalarPair(Union, Initialized),
47+
// since only the niche field (used for the tag) is guaranteed to be initialized.
48+
#[rustc_layout(debug)]
49+
pub enum NicheSecond { //~ ERROR: layout_of
50+
A(u8, HasNiche),
51+
B,
52+
C,
53+
}

0 commit comments

Comments
 (0)