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

Compute the correct layout for variants of uninhabited enums #69767

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
24 changes: 15 additions & 9 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,20 +779,26 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
.filter_map(|(i, v)| if absent(v) { None } else { Some(i) });
(present_variants.next(), present_variants.next())
};
let present_first = match present_first {
present_first @ Some(_) => present_first,
// Uninhabited because it has no variants, or only absent ones.
None if def.is_enum() => return tcx.layout_raw(param_env.and(tcx.types.never)),
// if it's a struct, still compute a layout so that we can still compute the
// field offsets
None => Some(VariantIdx::new(0)),
let (present_first, allow_enum_layout_opt) = match present_first {
present_first @ Some(_) => (present_first, !def.repr.inhibit_enum_layout_opt()),
// Do not run layout optimizations because it has no variants,
// or only absent ones, which will make the niche logic ICE.
None if def.is_enum() => {
if variants.is_empty() {
return tcx.layout_raw(param_env.and(tcx.types.never));
} else {
(None, false)
}
}
// If it's a struct, still compute a valid layout.
None => (Some(VariantIdx::new(0)), !def.repr.inhibit_enum_layout_opt()),
};

let is_struct = !def.is_enum() ||
// Only one variant is present.
(present_second.is_none() &&
// Representation optimizations are allowed.
!def.repr.inhibit_enum_layout_opt());
allow_enum_layout_opt);
if is_struct {
// Struct, or univariant enum equivalent to a struct.
// (Typechecking will reject discriminant-sizing attrs.)
Expand Down Expand Up @@ -883,7 +889,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
.all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i.as_u32()));

// Niche-filling enum optimization.
if !def.repr.inhibit_enum_layout_opt() && no_explicit_discriminants {
if allow_enum_layout_opt && no_explicit_discriminants {
let mut dataful_variant = None;
let mut niche_variants = VariantIdx::MAX..=VariantIdx::new(0);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
let base = match op.try_as_mplace(self) {
Ok(mplace) => {
// The easy case
// We can reuse the mplace field compuation logic for indirect operands
let field = self.mplace_field(mplace, field)?;
return Ok(field.into());
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/ui/consts/issue-69191.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// build-pass
//
// (this is deliberately *not* check-pass; I have confirmed that the bug in
// question does not replicate when one uses `cargo check` alone.)

pub enum Void {}

enum UninhabitedUnivariant {
_Variant(Void),
}

#[repr(C)]
enum UninhabitedUnivariantC {
_Variant(Void),
}

#[repr(i32)]
enum UninhabitedUnivariant32 {
_Variant(Void),
}

fn main() {
let _seed: UninhabitedUnivariant = None.unwrap();
match _seed {
UninhabitedUnivariant::_Variant(_x) => {}
}

let _seed: UninhabitedUnivariantC = None.unwrap();
match _seed {
UninhabitedUnivariantC::_Variant(_x) => {}
}

let _seed: UninhabitedUnivariant32 = None.unwrap();
match _seed {
UninhabitedUnivariant32::_Variant(_x) => {}
}
}