Skip to content

Commit 5577ffa

Browse files
committed
Separate pattern lowering from pattern type lowering
1 parent f186a6e commit 5577ffa

File tree

1 file changed

+29
-27
lines changed
  • compiler/rustc_hir_analysis/src/hir_ty_lowering

1 file changed

+29
-27
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,33 +2689,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26892689
hir::TyKind::Pat(ty, pat) => {
26902690
let ty_span = ty.span;
26912691
let ty = self.lower_ty(ty);
2692-
let pat_ty = match pat.kind {
2693-
hir::TyPatKind::Range(start, end) => {
2694-
let (ty, start, end) = match ty.kind() {
2695-
// Keep this list of types in sync with the list of types that
2696-
// the `RangePattern` trait is implemented for.
2697-
ty::Int(_) | ty::Uint(_) | ty::Char => {
2698-
let start = self.lower_const_arg(start, FeedConstTy::No);
2699-
let end = self.lower_const_arg(end, FeedConstTy::No);
2700-
(ty, start, end)
2701-
}
2702-
_ => {
2703-
let guar = self.dcx().span_delayed_bug(
2704-
ty_span,
2705-
"invalid base type for range pattern",
2706-
);
2707-
let errc = ty::Const::new_error(tcx, guar);
2708-
(Ty::new_error(tcx, guar), errc, errc)
2709-
}
2710-
};
2711-
2712-
let pat = tcx.mk_pat(ty::PatternKind::Range { start, end });
2713-
Ty::new_pat(tcx, ty, pat)
2714-
}
2715-
hir::TyPatKind::NotNull => {
2716-
Ty::new_pat(tcx, ty, tcx.mk_pat(ty::PatternKind::NotNull))
2717-
}
2718-
hir::TyPatKind::Err(e) => Ty::new_error(tcx, e),
2692+
let pat_ty = match self.lower_pat_ty_pat(ty, ty_span, pat) {
2693+
Ok(kind) => Ty::new_pat(tcx, ty, tcx.mk_pat(kind)),
2694+
Err(guar) => Ty::new_error(tcx, guar),
27192695
};
27202696
self.record_ty(pat.hir_id, ty, pat.span);
27212697
pat_ty
@@ -2727,6 +2703,32 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27272703
result_ty
27282704
}
27292705

2706+
fn lower_pat_ty_pat(
2707+
&self,
2708+
ty: Ty<'tcx>,
2709+
ty_span: Span,
2710+
pat: &hir::TyPat<'tcx>,
2711+
) -> Result<ty::PatternKind<'tcx>, ErrorGuaranteed> {
2712+
match pat.kind {
2713+
hir::TyPatKind::Range(start, end) => {
2714+
match ty.kind() {
2715+
// Keep this list of types in sync with the list of types that
2716+
// the `RangePattern` trait is implemented for.
2717+
ty::Int(_) | ty::Uint(_) | ty::Char => {
2718+
let start = self.lower_const_arg(start, FeedConstTy::No);
2719+
let end = self.lower_const_arg(end, FeedConstTy::No);
2720+
Ok(ty::PatternKind::Range { start, end })
2721+
}
2722+
_ => Err(self
2723+
.dcx()
2724+
.span_delayed_bug(ty_span, "invalid base type for range pattern")),
2725+
}
2726+
}
2727+
hir::TyPatKind::NotNull => Ok(ty::PatternKind::NotNull),
2728+
hir::TyPatKind::Err(e) => Err(e),
2729+
}
2730+
}
2731+
27302732
/// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
27312733
#[instrument(level = "debug", skip(self), ret)]
27322734
fn lower_opaque_ty(&self, def_id: LocalDefId, in_trait: bool) -> Ty<'tcx> {

0 commit comments

Comments
 (0)