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 9321f7efa9
commit d7d08ae95c
3 changed files with 7 additions and 6 deletions

View file

@ -189,7 +189,7 @@ fn layout_of_simd_ty(
};
Ok(Arc::new(Layout {
variants: Variants::Single { index: struct_variant_idx() },
variants: Variants::Single { index: Some(struct_variant_idx()) },
fields,
backend_repr: BackendRepr::Vector { element: e_abi, count: e_len },
largest_niche: e_ly.largest_niche,
@ -305,7 +305,7 @@ pub fn layout_of_ty_query(
let largest_niche = if count != 0 { element.largest_niche } else { None };
Layout {
variants: Variants::Single { index: struct_variant_idx() },
variants: Variants::Single { index: Some(struct_variant_idx()) },
fields: FieldsShape::Array { stride: element.size, count },
backend_repr,
largest_niche,
@ -318,7 +318,7 @@ pub fn layout_of_ty_query(
TyKind::Slice(element) => {
let element = db.layout_of_ty(element.clone(), trait_env)?;
Layout {
variants: Variants::Single { index: struct_variant_idx() },
variants: Variants::Single { index: Some(struct_variant_idx()) },
fields: FieldsShape::Array { stride: element.size, count: 0 },
backend_repr: BackendRepr::Memory { sized: false },
largest_niche: None,
@ -329,7 +329,7 @@ pub fn layout_of_ty_query(
}
}
TyKind::Str => Layout {
variants: Variants::Single { index: struct_variant_idx() },
variants: Variants::Single { index: Some(struct_variant_idx()) },
fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 },
backend_repr: BackendRepr::Memory { sized: false },
largest_niche: None,

View file

@ -1639,7 +1639,8 @@ impl Evaluator<'_> {
};
match &layout.variants {
Variants::Single { index } => {
let r = self.const_eval_discriminant(self.db.enum_data(e).variants[index.0].0)?;
let r = self
.const_eval_discriminant(self.db.enum_data(e).variants[index.unwrap().0].0)?;
Ok(r)
}
Variants::Multiple { tag, tag_encoding, variants, .. } => {

View file

@ -335,7 +335,7 @@ pub(crate) fn detect_variant_from_bytes<'a>(
) -> Option<(EnumVariantId, &'a Layout)> {
let (var_id, var_layout) = match &layout.variants {
hir_def::layout::Variants::Single { index } => {
(db.enum_data(e).variants[index.0].0, layout)
(db.enum_data(e).variants[index.unwrap().0].0, layout)
}
hir_def::layout::Variants::Multiple { tag, tag_encoding, variants, .. } => {
let size = tag.size(target_data_layout).bytes_usize();