Skip to content

Commit 2fcc029

Browse files
committed
Clean up code
1 parent 911f309 commit 2fcc029

File tree

2 files changed

+14
-57
lines changed

2 files changed

+14
-57
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
227227
trace!(?predicates);
228228
}
229229
hir::GenericParamKind::Const { .. } => {
230+
let param_def_id = param.def_id.to_def_id();
230231
let ct_ty = tcx
231-
.type_of(param.def_id.to_def_id())
232+
.type_of(param_def_id)
232233
.no_bound_vars()
233234
.expect("const parameters cannot be generic");
234-
let ct = icx.lowerer().lower_const_param(param.hir_id);
235+
let ct = icx.lowerer().lower_const_param(param_def_id, param.hir_id);
235236
predicates
236237
.insert((ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), param.span));
237238
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+11-55
Original file line numberDiff line numberDiff line change
@@ -1974,23 +1974,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
19741974
///
19751975
/// Early-bound const parameters get lowered to [`ty::ConstKind::Param`]
19761976
/// and late-bound ones to [`ty::ConstKind::Bound`].
1977-
pub(crate) fn lower_const_param(&self, hir_id: HirId) -> Const<'tcx> {
1977+
pub(crate) fn lower_const_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {
19781978
let tcx = self.tcx();
1979-
match tcx.named_bound_var(hir_id) {
1980-
Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
1979+
1980+
match tcx.named_bound_var(path_hir_id) {
1981+
Some(rbv::ResolvedArg::EarlyBound(_)) => {
19811982
// Find the name and index of the const parameter by indexing the generics of
19821983
// the parent item and construct a `ParamConst`.
1983-
let item_def_id = tcx.local_parent(def_id);
1984+
let item_def_id = tcx.parent(param_def_id);
19841985
let generics = tcx.generics_of(item_def_id);
1985-
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
1986-
let name = tcx.item_name(def_id.to_def_id());
1986+
let index = generics.param_def_id_to_index[&param_def_id];
1987+
let name = tcx.item_name(param_def_id);
19871988
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
19881989
}
19891990
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
19901991
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
19911992
}
19921993
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
1993-
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", hir_id),
1994+
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
19941995
}
19951996
}
19961997

@@ -2019,10 +2020,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20192020
fn lower_const_arg_path(&self, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Const<'tcx> {
20202021
let tcx = self.tcx();
20212022

2022-
// TODO: handle path args properly
20232023
match qpath {
20242024
hir::QPath::Resolved(_, &hir::Path { res: Res::Def(DefKind::ConstParam, did), .. }) => {
2025-
self.lower_const_arg_param(did, hir_id)
2025+
self.lower_const_param(did, hir_id)
20262026
}
20272027
hir::QPath::Resolved(
20282028
_,
@@ -2032,31 +2032,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20322032
qpath.span(),
20332033
"fn's cannot be used as const args",
20342034
),
2035-
// hir::QPath::Resolved(_, path @ &hir::Path { res: Res::Def(_, did), .. }) => {
2036-
// let (item_segment, _) = path.segments.split_last().unwrap();
2037-
// let args = self.lower_generic_args_of_path_segment(path.span, did, item_segment);
2038-
// ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2039-
// }
2040-
// // TODO: type-relative paths
2041-
// _ => ty::Const::new_error_with_message(
2042-
// tcx,
2043-
// qpath.span(),
2044-
// "Const::lower_const_arg_path: invalid qpath",
2045-
// ),
20462035
hir::QPath::Resolved(maybe_qself, path) => {
20472036
debug!(?maybe_qself, ?path);
20482037
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
20492038
self.lower_const_path_resolved(opt_self_ty, path, hir_id)
20502039
}
2051-
2052-
// TODO: type-relative paths
2053-
// hir::QPath::TypeRelative(qself, segment) => {
2054-
// debug!(?qself, ?segment);
2055-
// let ty = self.lower_ty(qself);
2056-
// self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
2057-
// .map(|(ty, _, _)| ty)
2058-
// .unwrap_or_else(|guar| Ty::new_error(tcx, guar))
2059-
// }
20602040
_ => ty::Const::new_error_with_message(
20612041
tcx,
20622042
qpath.span(),
@@ -2080,7 +2060,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20802060
path.segments.iter(),
20812061
GenericsArgsErrExtend::Param(def_id),
20822062
);
2083-
self.lower_const_arg_param(def_id, hir_id)
2063+
self.lower_const_param(def_id, hir_id)
20842064
}
20852065
Res::Def(DefKind::Const | DefKind::Ctor(_, CtorKind::Const), did) => {
20862066
assert_eq!(opt_self_ty, None);
@@ -2095,37 +2075,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20952075
);
20962076
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
20972077
}
2098-
// TODO: DefKind::AssocConst?
20992078
_ => Const::new_error(
21002079
tcx,
21012080
tcx.dcx().span_delayed_bug(span, "invalid Res for const path"),
21022081
),
21032082
}
21042083
}
21052084

2106-
/// Lower a const param to a [`Const`]. This is only meant as a helper for [`Self::lower_const_arg_path`].
2107-
/// FIXME: dedup with lower_const_param
2108-
fn lower_const_arg_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {
2109-
let tcx = self.tcx();
2110-
2111-
match tcx.named_bound_var(path_hir_id) {
2112-
Some(rbv::ResolvedArg::EarlyBound(_)) => {
2113-
// Find the name and index of the const parameter by indexing the generics of
2114-
// the parent item and construct a `ParamConst`.
2115-
let item_def_id = tcx.parent(param_def_id);
2116-
let generics = tcx.generics_of(item_def_id);
2117-
let index = generics.param_def_id_to_index[&param_def_id];
2118-
let name = tcx.item_name(param_def_id);
2119-
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
2120-
}
2121-
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
2122-
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
2123-
}
2124-
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
2125-
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
2126-
}
2127-
}
2128-
21292085
fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
21302086
let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
21312087
match idx {
@@ -2327,7 +2283,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23272283
.type_of(def_id)
23282284
.no_bound_vars()
23292285
.expect("const parameter types cannot be generic");
2330-
let ct = self.lower_const_param(expr.hir_id);
2286+
let ct = self.lower_const_param(def_id, expr.hir_id);
23312287
(ct, ty)
23322288
}
23332289

0 commit comments

Comments
 (0)