1
Fork 0

add helper methods for accessing struct tail

This commit is contained in:
Lukas Markeffsky 2023-07-05 20:44:24 +02:00
parent e3de14e463
commit 7aa5f39d3b
9 changed files with 31 additions and 34 deletions

View file

@ -2028,6 +2028,22 @@ impl VariantDef {
&self.fields[FieldIdx::from_u32(0)]
}
/// Returns the last field in this variant, if present.
#[inline]
pub fn tail_opt(&self) -> Option<&FieldDef> {
self.fields.raw.last()
}
/// Returns the last field in this variant.
///
/// # Panics
///
/// Panics, if the variant has no fields.
#[inline]
pub fn tail(&self) -> &FieldDef {
self.tail_opt().expect("expected unsized ADT to have a tail field")
}
}
impl PartialEq for VariantDef {

View file

@ -230,7 +230,7 @@ impl<'tcx> TyCtxt<'tcx> {
if !def.is_struct() {
break;
}
match def.non_enum_variant().fields.raw.last() {
match def.non_enum_variant().tail_opt() {
Some(field) => {
f();
ty = field.ty(self, substs);
@ -304,7 +304,7 @@ impl<'tcx> TyCtxt<'tcx> {
(&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs))
if a_def == b_def && a_def.is_struct() =>
{
if let Some(f) = a_def.non_enum_variant().fields.raw.last() {
if let Some(f) = a_def.non_enum_variant().tail_opt() {
a = f.ty(self, a_substs);
b = f.ty(self, b_substs);
} else {