Skip to content

Commit a5ed6fb

Browse files
committed
Split out hoisting/printing of box patterns
1 parent 7f48851 commit a5ed6fb

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,11 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
833833
let kind = match pat.ctor() {
834834
Bool(b) => PatKind::Constant { value: mir::Const::from_bool(cx.tcx, *b) },
835835
IntRange(range) => return self.hoist_pat_range(range, *pat.ty()),
836+
Struct if pat.ty().is_box() => {
837+
// Outside of the `alloc` crate, the only way to create a struct pattern
838+
// of type `Box` is to use a `box` pattern via #[feature(box_patterns)].
839+
PatKind::Box { subpattern: hoist(&pat.fields[0]) }
840+
}
836841
Struct | Variant(_) | UnionField => match pat.ty().kind() {
837842
ty::Tuple(..) => PatKind::StructLike {
838843
enum_info: EnumInfo::NotEnum,
@@ -841,12 +846,6 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
841846
.map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern })
842847
.collect(),
843848
},
844-
ty::Adt(adt_def, _) if adt_def.is_box() => {
845-
// Without `box_patterns`, the only legal pattern of type `Box` is `_` (outside
846-
// of `std`). So this branch is only reachable when the feature is enabled and
847-
// the pattern is a box pattern.
848-
PatKind::Deref { subpattern: subpatterns.next().unwrap() }
849-
}
850849
&ty::Adt(adt_def, _) => {
851850
let variant_index = RustcPatCtxt::variant_index_for_adt(&pat.ctor(), adt_def);
852851
let subpatterns = subpatterns

compiler/rustc_pattern_analysis/src/rustc/print.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ pub(crate) enum PatKind<'tcx> {
3838
subpatterns: Vec<FieldPat<'tcx>>,
3939
},
4040

41+
Box {
42+
subpattern: Box<Pat<'tcx>>,
43+
},
44+
4145
Deref {
4246
subpattern: Box<Pat<'tcx>>,
4347
},
@@ -64,6 +68,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
6468
match self.kind {
6569
PatKind::Wild => write!(f, "_"),
6670
PatKind::Never => write!(f, "!"),
71+
PatKind::Box { ref subpattern } => write!(f, "box {subpattern}"),
6772
PatKind::StructLike { ref enum_info, ref subpatterns } => {
6873
write_struct_like(f, self.ty, enum_info, subpatterns)
6974
}
@@ -184,7 +189,6 @@ fn write_ref_like<'tcx>(
184189
subpattern: &Pat<'tcx>,
185190
) -> fmt::Result {
186191
match ty.kind() {
187-
ty::Adt(def, _) if def.is_box() => write!(f, "box ")?,
188192
ty::Ref(_, _, mutbl) => {
189193
write!(f, "&{}", mutbl.prefix_str())?;
190194
}

0 commit comments

Comments
 (0)