Skip to content

Commit b0a7fbd

Browse files
eddyberikdesjardins
authored andcommitted
[experiment] ty/layout: compute both niche-filling and tagged layouts for enums.
1 parent c2dbebd commit b0a7fbd

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/librustc_middle/ty/layout.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
876876
.iter_enumerated()
877877
.all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i.as_u32()));
878878

879+
let mut niche_filling_layout = None;
880+
879881
// Niche-filling enum optimization.
880882
if !def.repr.inhibit_enum_layout_opt() && no_explicit_discriminants {
881883
let mut dataful_variant = None;
@@ -972,7 +974,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
972974
let largest_niche =
973975
Niche::from_scalar(dl, offset, niche_scalar.clone());
974976

975-
return Ok(tcx.intern_layout(Layout {
977+
niche_filling_layout = Some(Layout {
976978
variants: Variants::Multiple {
977979
tag: niche_scalar,
978980
tag_encoding: TagEncoding::Niche {
@@ -991,7 +993,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
991993
largest_niche,
992994
size,
993995
align,
994-
}));
996+
});
995997
}
996998
}
997999
}
@@ -1214,7 +1216,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12141216

12151217
let largest_niche = Niche::from_scalar(dl, Size::ZERO, tag.clone());
12161218

1217-
tcx.intern_layout(Layout {
1219+
let tagged_layout = Layout {
12181220
variants: Variants::Multiple {
12191221
tag,
12201222
tag_encoding: TagEncoding::Direct,
@@ -1229,7 +1231,9 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12291231
abi,
12301232
align,
12311233
size,
1232-
})
1234+
};
1235+
1236+
tcx.intern_layout(niche_filling_layout.unwrap_or(tagged_layout))
12331237
}
12341238

12351239
// Types with no meaningful known layout.

0 commit comments

Comments
 (0)