Skip to content

Commit 7307dc0

Browse files
authored
Rollup merge of rust-lang#144694 - compiler-errors:with-self-ty, r=SparrowLii
Distinguish prepending and replacing self ty in predicates There are two kinds of functions called `with_self_ty`: 1. Prepends the `Self` type onto an `ExistentialPredicate` which lacks it in its internal representation. 2. Replaces the `Self` type of an existing predicate, either for diagnostics purposes or in the new trait solver when normalizing that self type. This PR distinguishes these two because I often want to only grep for one of them. Namely, let's call it `with_replaced_self_ty` when all we're doing is replacing the self type.
2 parents 07b7dc9 + 2ae048e commit 7307dc0

File tree

15 files changed

+53
-42
lines changed

15 files changed

+53
-42
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ fn suggestion_signature<'tcx>(
493493
let args = ty::GenericArgs::identity_for_item(tcx, assoc.def_id).rebase_onto(
494494
tcx,
495495
assoc.container_id(tcx),
496-
impl_trait_ref.with_self_ty(tcx, tcx.types.self_param).args,
496+
impl_trait_ref.with_replaced_self_ty(tcx, tcx.types.self_param).args,
497497
);
498498

499499
match assoc.kind {

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
888888
ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
889889
// `<Foo as Iterator>::Item = String`.
890890
let projection_term = pred.projection_term;
891-
let quiet_projection_term =
892-
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
891+
let quiet_projection_term = projection_term
892+
.with_replaced_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
893893

894894
let term = pred.term;
895895
let obligation = format!("{projection_term} = {term}");

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,11 +1800,13 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18001800
.kind()
18011801
.map_bound(|clause| match clause {
18021802
ty::ClauseKind::Trait(trait_pred) => Some(ty::ClauseKind::Trait(
1803-
trait_pred.with_self_ty(fcx.tcx, ty),
1803+
trait_pred.with_replaced_self_ty(fcx.tcx, ty),
18041804
)),
1805-
ty::ClauseKind::Projection(proj_pred) => Some(
1806-
ty::ClauseKind::Projection(proj_pred.with_self_ty(fcx.tcx, ty)),
1807-
),
1805+
ty::ClauseKind::Projection(proj_pred) => {
1806+
Some(ty::ClauseKind::Projection(
1807+
proj_pred.with_replaced_self_ty(fcx.tcx, ty),
1808+
))
1809+
}
18081810
_ => None,
18091811
})
18101812
.transpose()?;

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10531053
let pred = bound_predicate.rebind(pred);
10541054
// `<Foo as Iterator>::Item = String`.
10551055
let projection_term = pred.skip_binder().projection_term;
1056-
let quiet_projection_term =
1057-
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
1056+
let quiet_projection_term = projection_term
1057+
.with_replaced_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
10581058

10591059
let term = pred.skip_binder().term;
10601060

@@ -2157,7 +2157,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21572157
self.tcx,
21582158
self.fresh_args_for_item(sugg_span, impl_did),
21592159
)
2160-
.with_self_ty(self.tcx, rcvr_ty),
2160+
.with_replaced_self_ty(self.tcx, rcvr_ty),
21612161
idx,
21622162
sugg_span,
21632163
item,
@@ -2196,7 +2196,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21962196
trait_did,
21972197
self.fresh_args_for_item(sugg_span, trait_did),
21982198
)
2199-
.with_self_ty(self.tcx, rcvr_ty),
2199+
.with_replaced_self_ty(self.tcx, rcvr_ty),
22002200
idx,
22012201
sugg_span,
22022202
item,

compiler/rustc_hir_typeck/src/typeck_root_ctxt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl<'tcx> TypeckRootCtxt<'tcx> {
167167
obligation.predicate.kind().rebind(
168168
// (*) binder moved here
169169
ty::PredicateKind::Clause(ty::ClauseKind::Trait(
170-
tpred.with_self_ty(self.tcx, new_self_ty),
170+
tpred.with_replaced_self_ty(self.tcx, new_self_ty),
171171
)),
172172
),
173173
);

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ where
5050

5151
fn trait_ref(self, cx: I) -> ty::TraitRef<I>;
5252

53-
fn with_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
53+
fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
5454

5555
fn trait_def_id(self, cx: I) -> I::DefId;
5656

@@ -376,8 +376,8 @@ where
376376
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
377377
}
378378

379-
let goal: Goal<I, G> =
380-
goal.with(self.cx(), goal.predicate.with_self_ty(self.cx(), normalized_self_ty));
379+
let goal: Goal<I, G> = goal
380+
.with(self.cx(), goal.predicate.with_replaced_self_ty(self.cx(), normalized_self_ty));
381381
// Vars that show up in the rest of the goal substs may have been constrained by
382382
// normalizing the self type as well, since type variables are not uniquified.
383383
let goal = self.resolve_vars_if_possible(goal);

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ where
2929
self.trait_ref
3030
}
3131

32-
fn with_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
33-
self.with_self_ty(cx, self_ty)
32+
fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
33+
self.with_replaced_self_ty(cx, self_ty)
3434
}
3535

3636
fn trait_def_id(self, _: I) -> I::DefId {

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ where
9999
self.alias.trait_ref(cx)
100100
}
101101

102-
fn with_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
103-
self.with_self_ty(cx, self_ty)
102+
fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
103+
self.with_replaced_self_ty(cx, self_ty)
104104
}
105105

106106
fn trait_def_id(self, cx: I) -> I::DefId {

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ where
3333
self.trait_ref
3434
}
3535

36-
fn with_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
37-
self.with_self_ty(cx, self_ty)
36+
fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
37+
self.with_replaced_self_ty(cx, self_ty)
3838
}
3939

4040
fn trait_def_id(self, _: I) -> I::DefId {
@@ -1263,7 +1263,9 @@ where
12631263
let goals =
12641264
ecx.enter_forall(constituent_tys(ecx, goal.predicate.self_ty())?, |ecx, tys| {
12651265
tys.into_iter()
1266-
.map(|ty| goal.with(ecx.cx(), goal.predicate.with_self_ty(ecx.cx(), ty)))
1266+
.map(|ty| {
1267+
goal.with(ecx.cx(), goal.predicate.with_replaced_self_ty(ecx.cx(), ty))
1268+
})
12671269
.collect::<Vec<_>>()
12681270
});
12691271
ecx.add_goals(GoalSource::ImplWhereBound, goals);

compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
581581
let trait_args = trait_ref
582582
.instantiate_identity()
583583
// Replace the explicit self type with `Self` for better suggestion rendering
584-
.with_self_ty(self.tcx, Ty::new_param(self.tcx, 0, kw::SelfUpper))
584+
.with_replaced_self_ty(self.tcx, Ty::new_param(self.tcx, 0, kw::SelfUpper))
585585
.args;
586586
let trait_item_args = ty::GenericArgs::identity_for_item(self.tcx, impl_item_def_id)
587587
.rebase_onto(self.tcx, impl_def_id, trait_args);

0 commit comments

Comments
 (0)