Skip to content

Commit 6b0c0a0

Browse files
committed
Pull PatExpr lowering out into a dedicated function
1 parent 5e55679 commit 6b0c0a0

File tree

1 file changed

+40
-48
lines changed
  • compiler/rustc_hir_analysis/src/hir_ty_lowering

1 file changed

+40
-48
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+40-48
Original file line numberDiff line numberDiff line change
@@ -2441,54 +2441,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24412441
Ty::new_error(tcx, err)
24422442
}
24432443
hir::PatKind::Range(start, end, include_end) => {
2444-
let expr_to_const = |expr: &'tcx hir::PatExpr<'tcx>| -> ty::Const<'tcx> {
2445-
let (c, c_ty) = match expr.kind {
2446-
hir::PatExprKind::Lit { lit, negated } => {
2447-
let lit_input =
2448-
LitToConstInput { lit: &lit.node, ty, neg: negated };
2449-
let ct = tcx.lit_to_const(lit_input);
2450-
(ct, ty)
2451-
}
2452-
2453-
hir::PatExprKind::Path(hir::QPath::Resolved(
2454-
_,
2455-
path @ &hir::Path {
2456-
res: Res::Def(DefKind::ConstParam, def_id),
2457-
..
2458-
},
2459-
)) => {
2460-
match self.prohibit_generic_args(
2461-
path.segments.iter(),
2462-
GenericsArgsErrExtend::Param(def_id),
2463-
) {
2464-
Ok(()) => {
2465-
let ty = tcx
2466-
.type_of(def_id)
2467-
.no_bound_vars()
2468-
.expect("const parameter types cannot be generic");
2469-
let ct = self.lower_const_param(def_id, expr.hir_id);
2470-
(ct, ty)
2471-
}
2472-
Err(guar) => (
2473-
ty::Const::new_error(tcx, guar),
2474-
Ty::new_error(tcx, guar),
2475-
),
2476-
}
2477-
}
2478-
2479-
_ => {
2480-
let err = tcx
2481-
.dcx()
2482-
.emit_err(crate::errors::NonConstRange { span: expr.span });
2483-
(ty::Const::new_error(tcx, err), Ty::new_error(tcx, err))
2484-
}
2485-
};
2486-
self.record_ty(expr.hir_id, c_ty, expr.span);
2487-
c
2488-
};
2489-
2490-
let start = start.map(expr_to_const);
2491-
let end = end.map(expr_to_const);
2444+
let start = start.map(|expr| self.lower_pat_expr(expr, ty));
2445+
let end = end.map(|expr| self.lower_pat_expr(expr, ty));
24922446

24932447
let include_end = match include_end {
24942448
hir::RangeEnd::Included => true,
@@ -2515,6 +2469,44 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25152469
result_ty
25162470
}
25172471

2472+
fn lower_pat_expr(&self, expr: &'tcx hir::PatExpr<'tcx>, ty: Ty<'tcx>) -> ty::Const<'tcx> {
2473+
let tcx = self.tcx();
2474+
let (c, c_ty) = match expr.kind {
2475+
hir::PatExprKind::Lit { lit, negated } => {
2476+
let lit_input = LitToConstInput { lit: &lit.node, ty, neg: negated };
2477+
let ct = tcx.lit_to_const(lit_input);
2478+
(ct, ty)
2479+
}
2480+
2481+
hir::PatExprKind::Path(hir::QPath::Resolved(
2482+
_,
2483+
path @ &hir::Path { res: Res::Def(DefKind::ConstParam, def_id), .. },
2484+
)) => {
2485+
match self.prohibit_generic_args(
2486+
path.segments.iter(),
2487+
GenericsArgsErrExtend::Param(def_id),
2488+
) {
2489+
Ok(()) => {
2490+
let ty = tcx
2491+
.type_of(def_id)
2492+
.no_bound_vars()
2493+
.expect("const parameter types cannot be generic");
2494+
let ct = self.lower_const_param(def_id, expr.hir_id);
2495+
(ct, ty)
2496+
}
2497+
Err(guar) => (ty::Const::new_error(tcx, guar), Ty::new_error(tcx, guar)),
2498+
}
2499+
}
2500+
2501+
_ => {
2502+
let err = tcx.dcx().emit_err(crate::errors::NonConstRange { span: expr.span });
2503+
(ty::Const::new_error(tcx, err), Ty::new_error(tcx, err))
2504+
}
2505+
};
2506+
self.record_ty(expr.hir_id, c_ty, expr.span);
2507+
c
2508+
}
2509+
25182510
/// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
25192511
#[instrument(level = "debug", skip(self), ret)]
25202512
fn lower_opaque_ty(&self, def_id: LocalDefId, in_trait: bool) -> Ty<'tcx> {

0 commit comments

Comments
 (0)