From d7d08ae95c500d4bf6a3814c1f847642bd46b3dc Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 1 Dec 2024 13:12:43 +0100 Subject: [PATCH 1/2] Variants::Single: do not use invalid VariantIdx for uninhabited enums --- crates/hir-ty/src/layout.rs | 8 ++++---- crates/hir-ty/src/mir/eval.rs | 3 ++- crates/hir-ty/src/utils.rs | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/hir-ty/src/layout.rs b/crates/hir-ty/src/layout.rs index 0c1f63880c..08026f11c8 100644 --- a/crates/hir-ty/src/layout.rs +++ b/crates/hir-ty/src/layout.rs @@ -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, diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs index d7029651fc..9375853e91 100644 --- a/crates/hir-ty/src/mir/eval.rs +++ b/crates/hir-ty/src/mir/eval.rs @@ -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, .. } => { diff --git a/crates/hir-ty/src/utils.rs b/crates/hir-ty/src/utils.rs index 06719b09f7..43ed6a06f4 100644 --- a/crates/hir-ty/src/utils.rs +++ b/crates/hir-ty/src/utils.rs @@ -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(); From c858ce132fd25db02fb2fc2c03648a15e4a637da Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 1 Dec 2024 17:33:01 +0100 Subject: [PATCH 2/2] make no-variant types a dedicated Variants variant --- crates/hir-ty/src/layout.rs | 8 ++++---- crates/hir-ty/src/mir/eval.rs | 8 ++++---- crates/hir-ty/src/utils.rs | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/hir-ty/src/layout.rs b/crates/hir-ty/src/layout.rs index 08026f11c8..0c1f63880c 100644 --- a/crates/hir-ty/src/layout.rs +++ b/crates/hir-ty/src/layout.rs @@ -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, diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs index 9375853e91..e3072d6ee7 100644 --- a/crates/hir-ty/src/mir/eval.rs +++ b/crates/hir-ty/src/mir/eval.rs @@ -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, diff --git a/crates/hir-ty/src/utils.rs b/crates/hir-ty/src/utils.rs index 43ed6a06f4..42e7edaf0f 100644 --- a/crates/hir-ty/src/utils.rs +++ b/crates/hir-ty/src/utils.rs @@ -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();