Skip to content

Commit a1a57d2

Browse files
committed
Clean up code
1 parent 56d3699 commit a1a57d2

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
223223
trace!(?predicates);
224224
}
225225
hir::GenericParamKind::Const { .. } => {
226+
let param_def_id = param.def_id.to_def_id();
226227
let ct_ty = tcx
227-
.type_of(param.def_id.to_def_id())
228+
.type_of(param_def_id)
228229
.no_bound_vars()
229230
.expect("const parameters cannot be generic");
230-
let ct = icx.lowerer().lower_const_param(param.hir_id);
231+
let ct = icx.lowerer().lower_const_param(param_def_id, param.hir_id);
231232
predicates
232233
.insert((ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), param.span));
233234
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,23 +2040,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20402040
///
20412041
/// Early-bound const parameters get lowered to [`ty::ConstKind::Param`]
20422042
/// and late-bound ones to [`ty::ConstKind::Bound`].
2043-
pub(crate) fn lower_const_param(&self, hir_id: HirId) -> Const<'tcx> {
2043+
pub(crate) fn lower_const_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {
20442044
let tcx = self.tcx();
2045-
match tcx.named_bound_var(hir_id) {
2046-
Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
2045+
2046+
match tcx.named_bound_var(path_hir_id) {
2047+
Some(rbv::ResolvedArg::EarlyBound(_)) => {
20472048
// Find the name and index of the const parameter by indexing the generics of
20482049
// the parent item and construct a `ParamConst`.
2049-
let item_def_id = tcx.local_parent(def_id);
2050+
let item_def_id = tcx.parent(param_def_id);
20502051
let generics = tcx.generics_of(item_def_id);
2051-
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
2052-
let name = tcx.item_name(def_id.to_def_id());
2052+
let index = generics.param_def_id_to_index[&param_def_id];
2053+
let name = tcx.item_name(param_def_id);
20532054
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
20542055
}
20552056
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
20562057
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
20572058
}
20582059
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
2059-
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", hir_id),
2060+
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
20602061
}
20612062
}
20622063

@@ -2085,10 +2086,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20852086
fn lower_const_arg_path(&self, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Const<'tcx> {
20862087
let tcx = self.tcx();
20872088

2088-
// TODO: handle path args properly
20892089
match qpath {
20902090
hir::QPath::Resolved(_, &hir::Path { res: Res::Def(DefKind::ConstParam, did), .. }) => {
2091-
self.lower_const_arg_param(did, hir_id)
2091+
self.lower_const_param(did, hir_id)
20922092
}
20932093
hir::QPath::Resolved(
20942094
_,
@@ -2098,31 +2098,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20982098
qpath.span(),
20992099
"fn's cannot be used as const args",
21002100
),
2101-
// hir::QPath::Resolved(_, path @ &hir::Path { res: Res::Def(_, did), .. }) => {
2102-
// let (item_segment, _) = path.segments.split_last().unwrap();
2103-
// let args = self.lower_generic_args_of_path_segment(path.span, did, item_segment);
2104-
// ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2105-
// }
2106-
// // TODO: type-relative paths
2107-
// _ => ty::Const::new_error_with_message(
2108-
// tcx,
2109-
// qpath.span(),
2110-
// "Const::lower_const_arg_path: invalid qpath",
2111-
// ),
21122101
hir::QPath::Resolved(maybe_qself, path) => {
21132102
debug!(?maybe_qself, ?path);
21142103
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
21152104
self.lower_const_path_resolved(opt_self_ty, path, hir_id)
21162105
}
2117-
2118-
// TODO: type-relative paths
2119-
// hir::QPath::TypeRelative(qself, segment) => {
2120-
// debug!(?qself, ?segment);
2121-
// let ty = self.lower_ty(qself);
2122-
// self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
2123-
// .map(|(ty, _, _)| ty)
2124-
// .unwrap_or_else(|guar| Ty::new_error(tcx, guar))
2125-
// }
21262106
_ => ty::Const::new_error_with_message(
21272107
tcx,
21282108
qpath.span(),
@@ -2146,7 +2126,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21462126
path.segments.iter(),
21472127
GenericsArgsErrExtend::Param(def_id),
21482128
);
2149-
self.lower_const_arg_param(def_id, hir_id)
2129+
self.lower_const_param(def_id, hir_id)
21502130
}
21512131
Res::Def(DefKind::Const | DefKind::Ctor(_, CtorKind::Const), did) => {
21522132
assert_eq!(opt_self_ty, None);
@@ -2161,37 +2141,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21612141
);
21622142
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
21632143
}
2164-
// TODO: DefKind::AssocConst?
21652144
_ => Const::new_error(
21662145
tcx,
21672146
tcx.dcx().span_delayed_bug(span, "invalid Res for const path"),
21682147
),
21692148
}
21702149
}
21712150

2172-
/// Lower a const param to a [`Const`]. This is only meant as a helper for [`Self::lower_const_arg_path`].
2173-
/// FIXME: dedup with lower_const_param
2174-
fn lower_const_arg_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {
2175-
let tcx = self.tcx();
2176-
2177-
match tcx.named_bound_var(path_hir_id) {
2178-
Some(rbv::ResolvedArg::EarlyBound(_)) => {
2179-
// Find the name and index of the const parameter by indexing the generics of
2180-
// the parent item and construct a `ParamConst`.
2181-
let item_def_id = tcx.parent(param_def_id);
2182-
let generics = tcx.generics_of(item_def_id);
2183-
let index = generics.param_def_id_to_index[&param_def_id];
2184-
let name = tcx.item_name(param_def_id);
2185-
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
2186-
}
2187-
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
2188-
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
2189-
}
2190-
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
2191-
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
2192-
}
2193-
}
2194-
21952151
fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
21962152
let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
21972153
match idx {
@@ -2387,7 +2343,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23872343
.type_of(def_id)
23882344
.no_bound_vars()
23892345
.expect("const parameter types cannot be generic");
2390-
let ct = self.lower_const_param(expr.hir_id);
2346+
let ct = self.lower_const_param(def_id, expr.hir_id);
23912347
(ct, ty)
23922348
}
23932349

0 commit comments

Comments
 (0)