Variants::Single: do not use invalid VariantIdx for uninhabited enums

This commit is contained in:
Ralf Jung 2024-12-01 13:12:43 +01:00
parent 37e74596c0
commit 21de42bf8d
47 changed files with 549 additions and 281 deletions

View file

@ -213,11 +213,11 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
|cx, enum_type_di_node| {
match enum_type_and_layout.variants {
Variants::Single { index: variant_index } => {
if enum_adt_def.variants().is_empty() {
let Some(variant_index) = variant_index else {
// Uninhabited enums have Variants::Single. We don't generate
// any members for them.
return smallvec![];
}
};
build_single_variant_union_fields(
cx,

View file

@ -35,14 +35,14 @@ fn uncached_llvm_type<'a, 'tcx>(
if !cx.sess().fewer_names() =>
{
let mut name = with_no_visible_paths!(with_no_trimmed_paths!(layout.ty.to_string()));
if let (&ty::Adt(def, _), &Variants::Single { index }) =
if let (&ty::Adt(def, _), &Variants::Single { index: Some(index) }) =
(layout.ty.kind(), &layout.variants)
{
if def.is_enum() && !def.variants().is_empty() {
if def.is_enum() {
write!(&mut name, "::{}", def.variant(index).name).unwrap();
}
}
if let (&ty::Coroutine(_, _), &Variants::Single { index }) =
if let (&ty::Coroutine(_, _), &Variants::Single { index: Some(index) }) =
(layout.ty.kind(), &layout.variants)
{
write!(&mut name, "::{}", ty::CoroutineArgs::variant_name(index)).unwrap();
@ -216,7 +216,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
// Check the cache.
let variant_index = match self.variants {
Variants::Single { index } => Some(index),
Variants::Single { index } => index,
_ => None,
};
if let Some(llty) = cx.type_lowering.borrow().get(&(self.ty, variant_index)) {