Skip to content

Commit

Permalink
[experiment] ty/layout: compute both niche-filling and tagged layouts…
Browse files Browse the repository at this point in the history
… for enums.
  • Loading branch information
eddyb authored and erikdesjardins committed Jul 17, 2020
1 parent c2dbebd commit b0a7fbd
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/librustc_middle/ty/layout.rs
Expand Up @@ -876,6 +876,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
.iter_enumerated()
.all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i.as_u32()));

let mut niche_filling_layout = None;

// Niche-filling enum optimization.
if !def.repr.inhibit_enum_layout_opt() && no_explicit_discriminants {
let mut dataful_variant = None;
Expand Down Expand Up @@ -972,7 +974,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
let largest_niche =
Niche::from_scalar(dl, offset, niche_scalar.clone());

return Ok(tcx.intern_layout(Layout {
niche_filling_layout = Some(Layout {
variants: Variants::Multiple {
tag: niche_scalar,
tag_encoding: TagEncoding::Niche {
Expand All @@ -991,7 +993,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
largest_niche,
size,
align,
}));
});
}
}
}
Expand Down Expand Up @@ -1214,7 +1216,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {

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

tcx.intern_layout(Layout {
let tagged_layout = Layout {
variants: Variants::Multiple {
tag,
tag_encoding: TagEncoding::Direct,
Expand All @@ -1229,7 +1231,9 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
abi,
align,
size,
})
};

tcx.intern_layout(niche_filling_layout.unwrap_or(tagged_layout))
}

// Types with no meaningful known layout.
Expand Down

0 comments on commit b0a7fbd

Please sign in to comment.