1
Fork 0

Simplify hoisting of struct-like patterns

This commit is contained in:
Zalathar 2024-08-02 20:28:56 +10:00
parent a5ed6fb646
commit c764bea0c3

View file

@ -838,31 +838,24 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
// of type `Box` is to use a `box` pattern via #[feature(box_patterns)]. // of type `Box` is to use a `box` pattern via #[feature(box_patterns)].
PatKind::Box { subpattern: hoist(&pat.fields[0]) } PatKind::Box { subpattern: hoist(&pat.fields[0]) }
} }
Struct | Variant(_) | UnionField => match pat.ty().kind() { Struct | Variant(_) | UnionField => {
ty::Tuple(..) => PatKind::StructLike { let enum_info = match *pat.ty().kind() {
enum_info: EnumInfo::NotEnum, ty::Adt(adt_def, _) if adt_def.is_enum() => EnumInfo::Enum {
subpatterns: subpatterns adt_def,
.enumerate() variant_index: RustcPatCtxt::variant_index_for_adt(pat.ctor(), adt_def),
.map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern }) },
.collect(), ty::Adt(..) | ty::Tuple(..) => EnumInfo::NotEnum,
}, _ => bug!("unexpected ctor for type {:?} {:?}", pat.ctor(), *pat.ty()),
&ty::Adt(adt_def, _) => { };
let variant_index = RustcPatCtxt::variant_index_for_adt(&pat.ctor(), adt_def);
let subpatterns = subpatterns
.enumerate()
.map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern })
.collect();
let enum_info = if adt_def.is_enum() { let subpatterns = pat
EnumInfo::Enum { adt_def, variant_index } .iter_fields()
} else { .enumerate()
EnumInfo::NotEnum .map(|(i, pat)| FieldPat { field: FieldIdx::new(i), pattern: hoist(pat) })
}; .collect::<Vec<_>>();
PatKind::StructLike { enum_info, subpatterns } PatKind::StructLike { enum_info, subpatterns }
} }
_ => bug!("unexpected ctor for type {:?} {:?}", pat.ctor(), *pat.ty()),
},
// Note: given the expansion of `&str` patterns done in `expand_pattern`, we should // Note: given the expansion of `&str` patterns done in `expand_pattern`, we should
// be careful to reconstruct the correct constant pattern here. However a string // be careful to reconstruct the correct constant pattern here. However a string
// literal pattern will never be reported as a non-exhaustiveness witness, so we // literal pattern will never be reported as a non-exhaustiveness witness, so we