Skip to content

Commit 7458605

Browse files
committed
deduplication of assoc const binding RHS lowering
1 parent f58bd5c commit 7458605

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -541,19 +541,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
541541
hir::AssocItemConstraintKind::Equality { term } => {
542542
let term = match term {
543543
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
544-
hir::Term::Const(ct) => {
545-
let ty = projection_term.map_bound(|alias| {
546-
tcx.type_of(alias.def_id).instantiate(tcx, alias.args)
547-
});
548-
let ty = check_assoc_const_binding_type(
549-
self,
544+
hir::Term::Const(ct) => self
545+
.lower_assoc_const_binding_rhs(
546+
ct,
550547
constraint.ident,
551-
ty,
552548
constraint.hir_id,
553-
);
554-
555-
self.lower_const_arg(ct, ty).into()
556-
}
549+
projection_term,
550+
)
551+
.into(),
557552
};
558553

559554
// Find any late-bound regions declared in `ty` that are not
@@ -853,6 +848,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
853848
let shifted_output = tcx.shift_bound_var_indices(num_bound_vars, output);
854849
Ok(ty::EarlyBinder::bind(shifted_output).instantiate(tcx, args))
855850
}
851+
852+
/// Derives the expected type of an associated const,
853+
/// validates it, and lowers the RHS const arg against that type
854+
pub(crate) fn lower_assoc_const_binding_rhs(
855+
&self,
856+
ct: &'tcx hir::ConstArg<'tcx>,
857+
assoc_ident: Ident,
858+
constraint_hir_id: hir::HirId,
859+
projection_term: ty::Binder<'tcx, ty::AliasTerm<'tcx>>,
860+
) -> ty::Const<'tcx> {
861+
let tcx = self.tcx();
862+
let ty = projection_term
863+
.map_bound(|alias| tcx.type_of(alias.def_id).instantiate(tcx, alias.args));
864+
let ty = check_assoc_const_binding_type(self, assoc_ident, ty, constraint_hir_id);
865+
self.lower_const_arg(ct, ty)
866+
}
856867
}
857868

858869
/// Detect and reject early-bound & escaping late-bound generic params in the type of assoc const bindings.

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,19 +1308,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13081308
)
13091309
});
13101310

1311-
// FIXME(mgca): code duplication with other places we lower
1312-
// the rhs' of associated const bindings
1313-
let ty = projection_term.map_bound(|alias| {
1314-
tcx.type_of(alias.def_id).instantiate(tcx, alias.args)
1315-
});
1316-
let ty = bounds::check_assoc_const_binding_type(
1317-
self,
1311+
self.lower_assoc_const_binding_rhs(
1312+
ct,
13181313
constraint.ident,
1319-
ty,
13201314
constraint.hir_id,
1321-
);
1322-
1323-
self.lower_const_arg(ct, ty).into()
1315+
projection_term,
1316+
)
1317+
.into()
13241318
}
13251319
};
13261320
if term.references_error() {

0 commit comments

Comments
 (0)