Simplify hoisting of struct-like patterns
This commit is contained in:
parent
a5ed6fb646
commit
c764bea0c3
1 changed files with 16 additions and 23 deletions
|
@ -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(adt_def, _) => {
|
ty::Adt(..) | ty::Tuple(..) => EnumInfo::NotEnum,
|
||||||
let variant_index = RustcPatCtxt::variant_index_for_adt(&pat.ctor(), adt_def);
|
_ => bug!("unexpected ctor for type {:?} {:?}", pat.ctor(), *pat.ty()),
|
||||||
let subpatterns = subpatterns
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern })
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let enum_info = if adt_def.is_enum() {
|
|
||||||
EnumInfo::Enum { adt_def, variant_index }
|
|
||||||
} else {
|
|
||||||
EnumInfo::NotEnum
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let subpatterns = pat
|
||||||
|
.iter_fields()
|
||||||
|
.enumerate()
|
||||||
|
.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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue