Skip to content

Commit 3f89c38

Browse files
committed
Inline const_param_def_id at its only use site
1 parent fa5a15c commit 3f89c38

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

src/librustc/ty/sty.rs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,26 +2401,6 @@ pub struct Const<'tcx> {
24012401
#[cfg(target_arch = "x86_64")]
24022402
static_assert_size!(Const<'_>, 48);
24032403

2404-
/// Returns the `DefId` of the constant parameter that the provided expression is a path to.
2405-
fn const_param_def_id(expr: &hir::Expr<'_>) -> Option<DefId> {
2406-
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2407-
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
2408-
let expr = match &expr.kind {
2409-
hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
2410-
block.expr.as_ref().unwrap()
2411-
}
2412-
_ => expr,
2413-
};
2414-
2415-
match &expr.kind {
2416-
hir::ExprKind::Path(hir::QPath::Resolved(_, path)) => match path.res {
2417-
hir::def::Res::Def(hir::def::DefKind::ConstParam, did) => Some(did),
2418-
_ => None,
2419-
},
2420-
_ => None,
2421-
}
2422-
}
2423-
24242404
impl<'tcx> Const<'tcx> {
24252405
/// Literals and const generic parameters are eagerly converted to a constant, everything else
24262406
/// becomes `Unevaluated`.
@@ -2456,20 +2436,36 @@ impl<'tcx> Const<'tcx> {
24562436
}
24572437
}
24582438

2459-
let kind = if let Some(def_id) = const_param_def_id(expr) {
2460-
// Find the name and index of the const parameter by indexing the generics of the
2461-
// parent item and construct a `ParamConst`.
2462-
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
2463-
let item_id = tcx.hir().get_parent_node(hir_id);
2464-
let item_def_id = tcx.hir().local_def_id(item_id);
2465-
let generics = tcx.generics_of(item_def_id);
2466-
let index = generics.param_def_id_to_index[&tcx.hir().local_def_id(hir_id)];
2467-
let name = tcx.hir().name(hir_id);
2468-
ty::ConstKind::Param(ty::ParamConst::new(index, name))
2469-
} else {
2470-
ty::ConstKind::Unevaluated(def_id, InternalSubsts::identity_for_item(tcx, def_id), None)
2439+
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2440+
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
2441+
let expr = match &expr.kind {
2442+
hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
2443+
block.expr.as_ref().unwrap()
2444+
}
2445+
_ => expr,
24712446
};
2472-
tcx.mk_const(ty::Const { val: kind, ty })
2447+
2448+
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
2449+
let val = match expr.kind {
2450+
ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
2451+
// Find the name and index of the const parameter by indexing the generics of
2452+
// the parent item and construct a `ParamConst`.
2453+
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
2454+
let item_id = tcx.hir().get_parent_node(hir_id);
2455+
let item_def_id = tcx.hir().local_def_id(item_id);
2456+
let generics = tcx.generics_of(item_def_id);
2457+
let index = generics.param_def_id_to_index[&tcx.hir().local_def_id(hir_id)];
2458+
let name = tcx.hir().name(hir_id);
2459+
ty::ConstKind::Param(ty::ParamConst::new(index, name))
2460+
}
2461+
_ => ty::ConstKind::Unevaluated(
2462+
def_id,
2463+
InternalSubsts::identity_for_item(tcx, def_id),
2464+
None,
2465+
),
2466+
};
2467+
2468+
tcx.mk_const(ty::Const { val, ty })
24732469
}
24742470

24752471
#[inline]

0 commit comments

Comments
 (0)