make no-variant types a dedicated Variants variant

This commit is contained in:
Ralf Jung 2024-12-01 17:33:01 +01:00
parent d7d08ae95c
commit c858ce132f
3 changed files with 10 additions and 9 deletions

View file

@ -189,7 +189,7 @@ fn layout_of_simd_ty(
};
Ok(Arc::new(Layout {
variants: Variants::Single { index: Some(struct_variant_idx()) },
variants: Variants::Single { index: 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: Some(struct_variant_idx()) },
variants: Variants::Single { index: 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: Some(struct_variant_idx()) },
variants: Variants::Single { index: 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: Some(struct_variant_idx()) },
variants: Variants::Single { index: struct_variant_idx() },
fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 },
backend_repr: BackendRepr::Memory { sized: false },
largest_niche: None,

View file

@ -813,7 +813,7 @@ impl Evaluator<'_> {
ProjectionElem::Field(Either::Left(f)) => {
let layout = self.layout(&prev_ty)?;
let variant_layout = match &layout.variants {
Variants::Single { .. } => &layout,
Variants::Single { .. } | Variants::Empty => &layout,
Variants::Multiple { variants, .. } => {
&variants[match f.parent {
hir_def::VariantId::EnumVariantId(it) => {
@ -1638,9 +1638,9 @@ impl Evaluator<'_> {
return Ok(0);
};
match &layout.variants {
Variants::Empty => unreachable!(),
Variants::Single { index } => {
let r = self
.const_eval_discriminant(self.db.enum_data(e).variants[index.unwrap().0].0)?;
let r = self.const_eval_discriminant(self.db.enum_data(e).variants[index.0].0)?;
Ok(r)
}
Variants::Multiple { tag, tag_encoding, variants, .. } => {
@ -1801,7 +1801,7 @@ impl Evaluator<'_> {
}
let layout = self.layout_adt(adt, subst)?;
Ok(match &layout.variants {
Variants::Single { .. } => (layout.size.bytes_usize(), layout, None),
Variants::Single { .. } | Variants::Empty => (layout.size.bytes_usize(), layout, None),
Variants::Multiple { variants, tag, tag_encoding, .. } => {
let enum_variant_id = match it {
VariantId::EnumVariantId(it) => it,

View file

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