Split out hoisting/printing of box
patterns
This commit is contained in:
parent
7f48851416
commit
a5ed6fb646
2 changed files with 10 additions and 7 deletions
|
@ -833,6 +833,11 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
|
||||||
let kind = match pat.ctor() {
|
let kind = match pat.ctor() {
|
||||||
Bool(b) => PatKind::Constant { value: mir::Const::from_bool(cx.tcx, *b) },
|
Bool(b) => PatKind::Constant { value: mir::Const::from_bool(cx.tcx, *b) },
|
||||||
IntRange(range) => return self.hoist_pat_range(range, *pat.ty()),
|
IntRange(range) => return self.hoist_pat_range(range, *pat.ty()),
|
||||||
|
Struct if pat.ty().is_box() => {
|
||||||
|
// Outside of the `alloc` crate, the only way to create a struct pattern
|
||||||
|
// of type `Box` is to use a `box` pattern via #[feature(box_patterns)].
|
||||||
|
PatKind::Box { subpattern: hoist(&pat.fields[0]) }
|
||||||
|
}
|
||||||
Struct | Variant(_) | UnionField => match pat.ty().kind() {
|
Struct | Variant(_) | UnionField => match pat.ty().kind() {
|
||||||
ty::Tuple(..) => PatKind::StructLike {
|
ty::Tuple(..) => PatKind::StructLike {
|
||||||
enum_info: EnumInfo::NotEnum,
|
enum_info: EnumInfo::NotEnum,
|
||||||
|
@ -841,12 +846,6 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
|
||||||
.map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern })
|
.map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern })
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
},
|
||||||
ty::Adt(adt_def, _) if adt_def.is_box() => {
|
|
||||||
// Without `box_patterns`, the only legal pattern of type `Box` is `_` (outside
|
|
||||||
// of `std`). So this branch is only reachable when the feature is enabled and
|
|
||||||
// the pattern is a box pattern.
|
|
||||||
PatKind::Deref { subpattern: subpatterns.next().unwrap() }
|
|
||||||
}
|
|
||||||
&ty::Adt(adt_def, _) => {
|
&ty::Adt(adt_def, _) => {
|
||||||
let variant_index = RustcPatCtxt::variant_index_for_adt(&pat.ctor(), adt_def);
|
let variant_index = RustcPatCtxt::variant_index_for_adt(&pat.ctor(), adt_def);
|
||||||
let subpatterns = subpatterns
|
let subpatterns = subpatterns
|
||||||
|
|
|
@ -38,6 +38,10 @@ pub(crate) enum PatKind<'tcx> {
|
||||||
subpatterns: Vec<FieldPat<'tcx>>,
|
subpatterns: Vec<FieldPat<'tcx>>,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Box {
|
||||||
|
subpattern: Box<Pat<'tcx>>,
|
||||||
|
},
|
||||||
|
|
||||||
Deref {
|
Deref {
|
||||||
subpattern: Box<Pat<'tcx>>,
|
subpattern: Box<Pat<'tcx>>,
|
||||||
},
|
},
|
||||||
|
@ -64,6 +68,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
|
||||||
match self.kind {
|
match self.kind {
|
||||||
PatKind::Wild => write!(f, "_"),
|
PatKind::Wild => write!(f, "_"),
|
||||||
PatKind::Never => write!(f, "!"),
|
PatKind::Never => write!(f, "!"),
|
||||||
|
PatKind::Box { ref subpattern } => write!(f, "box {subpattern}"),
|
||||||
PatKind::StructLike { ref enum_info, ref subpatterns } => {
|
PatKind::StructLike { ref enum_info, ref subpatterns } => {
|
||||||
write_struct_like(f, self.ty, enum_info, subpatterns)
|
write_struct_like(f, self.ty, enum_info, subpatterns)
|
||||||
}
|
}
|
||||||
|
@ -184,7 +189,6 @@ fn write_ref_like<'tcx>(
|
||||||
subpattern: &Pat<'tcx>,
|
subpattern: &Pat<'tcx>,
|
||||||
) -> fmt::Result {
|
) -> fmt::Result {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(def, _) if def.is_box() => write!(f, "box ")?,
|
|
||||||
ty::Ref(_, _, mutbl) => {
|
ty::Ref(_, _, mutbl) => {
|
||||||
write!(f, "&{}", mutbl.prefix_str())?;
|
write!(f, "&{}", mutbl.prefix_str())?;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue