Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[experiment] ty/layout: compute both niche-filling and tagged layouts for enums. #71045

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/librustc_middle/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,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 @@ -967,7 +969,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 {
discr: niche_scalar,
discr_kind: DiscriminantKind::Niche {
Expand All @@ -986,7 +988,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
largest_niche,
size,
align,
}));
});
}
}
}
Expand Down Expand Up @@ -1209,7 +1211,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 {
discr: tag,
discr_kind: DiscriminantKind::Tag,
Expand All @@ -1224,7 +1226,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