Skip to content

Commit 19a1192

Browse files
committed
Add a helper for replacing the self type in trait refs
1 parent 6f77c97 commit 19a1192

File tree

5 files changed

+15
-29
lines changed

5 files changed

+15
-29
lines changed

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
539539
.subst_iter_copied(self.tcx, substs)
540540
{
541541
let pred = pred.kind().rebind(match pred.kind().skip_binder() {
542-
ty::PredicateKind::Trait(mut trait_pred) => {
542+
ty::PredicateKind::Trait(trait_pred) => {
543543
assert_eq!(trait_pred.trait_ref.self_ty(), opaque_ty);
544-
trait_pred.trait_ref.substs =
545-
self.tcx.mk_substs_trait(ty, &trait_pred.trait_ref.substs[1..]);
546-
ty::PredicateKind::Trait(trait_pred)
544+
ty::PredicateKind::Trait(trait_pred.with_self_type(self.tcx, ty))
547545
}
548546
ty::PredicateKind::Projection(mut proj_pred) => {
549547
assert_eq!(proj_pred.projection_ty.self_ty(), opaque_ty);

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ impl<'tcx> TraitPredicate<'tcx> {
852852
}
853853
}
854854

855+
pub fn with_self_type(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
856+
Self { trait_ref: self.trait_ref.with_self_type(tcx, self_ty), ..self }
857+
}
858+
855859
pub fn def_id(self) -> DefId {
856860
self.trait_ref.def_id
857861
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,10 @@ impl<'tcx> TraitRef<'tcx> {
811811
TraitRef { def_id, substs }
812812
}
813813

814+
pub fn with_self_type(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
815+
tcx.mk_trait_ref(self.def_id, self_ty, &self.substs[1..])
816+
}
817+
814818
/// Returns a `TraitRef` of the form `P0: Foo<P1..Pn>` where `Pi`
815819
/// are the parameters defined on trait.
816820
pub fn identity(tcx: TyCtxt<'tcx>, def_id: DefId) -> Binder<'tcx, TraitRef<'tcx>> {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -998,13 +998,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
998998
if trait_predicate.skip_binder().self_ty().is_never()
999999
&& self.fallback_has_occurred
10001000
{
1001-
let predicate = trait_predicate.map_bound(|mut trait_pred| {
1002-
trait_pred.trait_ref = self.tcx.mk_trait_ref(
1003-
trait_pred.trait_ref.def_id,
1004-
self.tcx.mk_unit(),
1005-
&trait_pred.trait_ref.substs[1..],
1006-
);
1007-
trait_pred
1001+
let predicate = trait_predicate.map_bound(|trait_pred| {
1002+
trait_pred.with_self_type(self.tcx, self.tcx.mk_unit())
10081003
});
10091004
let unit_obligation = obligation.with(tcx, predicate);
10101005
if self.predicate_may_hold(&unit_obligation) {
@@ -2026,14 +2021,8 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20262021
param_env: ty::ParamEnv<'tcx>,
20272022
trait_ref_and_ty: ty::Binder<'tcx, (ty::TraitPredicate<'tcx>, Ty<'tcx>)>,
20282023
) -> PredicateObligation<'tcx> {
2029-
let trait_pred = trait_ref_and_ty.map_bound_ref(|(tr, new_self_ty)| ty::TraitPredicate {
2030-
trait_ref: self.tcx.mk_trait_ref(
2031-
tr.trait_ref.def_id,
2032-
*new_self_ty,
2033-
&tr.trait_ref.substs[1..],
2034-
),
2035-
..*tr
2036-
});
2024+
let trait_pred = trait_ref_and_ty
2025+
.map_bound(|(tr, new_self_ty)| tr.with_self_type(self.tcx, new_self_ty));
20372026

20382027
Obligation::new(self.tcx, ObligationCause::dummy(), param_env, trait_pred)
20392028
}

compiler/rustc_trait_selection/src/traits/relationships.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ pub(crate) fn update<'tcx, T>(
1818
{
1919
let new_self_ty = infcx.tcx.types.unit;
2020

21-
let trait_ref = infcx.tcx.mk_trait_ref(
22-
tpred.trait_ref.def_id,
23-
new_self_ty, &tpred.trait_ref.substs[1..],
24-
);
25-
2621
// Then construct a new obligation with Self = () added
2722
// to the ParamEnv, and see if it holds.
2823
let o = obligation.with(infcx.tcx,
@@ -31,11 +26,7 @@ pub(crate) fn update<'tcx, T>(
3126
.kind()
3227
.rebind(
3328
// (*) binder moved here
34-
ty::PredicateKind::Trait(ty::TraitPredicate {
35-
trait_ref,
36-
constness: tpred.constness,
37-
polarity: tpred.polarity,
38-
})
29+
ty::PredicateKind::Trait(tpred.with_self_type(infcx.tcx, new_self_ty))
3930
),
4031
);
4132
// Don't report overflow errors. Otherwise equivalent to may_hold.

0 commit comments

Comments
 (0)