Skip to content

Commit 2303972

Browse files
Auto merge of #142935 - compiler-errors:two-self-tys, r=<try>
[perf] Don't normalize self ty potentially 2 times This potentially does not do anything perf-wise... This is a preparation for a more involved test for doing less work when computing the `proven_via`.
2 parents 706f244 + 2650a28 commit 2303972

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -364,22 +364,7 @@ where
364364
goal: Goal<I, G>,
365365
assemble_from: AssembleCandidatesFrom,
366366
) -> Vec<Candidate<I>> {
367-
let Ok(normalized_self_ty) =
368-
self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
369-
else {
370-
return vec![];
371-
};
372-
373-
if normalized_self_ty.is_ty_var() {
374-
debug!("self type has been normalized to infer");
375-
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
376-
}
377-
378-
let goal: Goal<I, G> =
379-
goal.with(self.cx(), goal.predicate.with_self_ty(self.cx(), normalized_self_ty));
380-
// Vars that show up in the rest of the goal substs may have been constrained by
381-
// normalizing the self type as well, since type variables are not uniquified.
382-
let goal = self.resolve_vars_if_possible(goal);
367+
assert!(!goal.predicate.self_ty().is_ty_var());
383368

384369
let mut candidates = vec![];
385370

@@ -885,6 +870,30 @@ where
885870
}
886871
}
887872

873+
pub(super) fn normalize_goal_self_ty<G: GoalKind<D>>(
874+
&mut self,
875+
goal: Goal<I, G>,
876+
) -> Result<Option<Goal<I, G>>, NoSolution> {
877+
let cx = self.cx();
878+
879+
let Ok(normalized_self_ty) =
880+
self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
881+
else {
882+
return Err(NoSolution);
883+
};
884+
885+
if normalized_self_ty.is_ty_var() {
886+
debug!("self type has been normalized to infer");
887+
return Ok(None);
888+
}
889+
890+
let goal: Goal<I, G> = goal.with(cx, goal.predicate.with_self_ty(cx, normalized_self_ty));
891+
// Vars that show up in the rest of the goal substs may have been constrained by
892+
// normalizing the self type as well, since type variables are not uniquified.
893+
let goal = self.resolve_vars_if_possible(goal);
894+
Ok(Some(goal))
895+
}
896+
888897
/// Assemble and merge candidates for goals which are related to an underlying trait
889898
/// goal. Right now, this is normalizes-to and host effect goals.
890899
///
@@ -918,11 +927,20 @@ where
918927
#[instrument(level = "debug", skip(self, inject_normalize_to_rigid_candidate), ret)]
919928
pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
920929
&mut self,
921-
proven_via: Option<TraitGoalProvenVia>,
922930
goal: Goal<I, G>,
923931
inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
924932
) -> QueryResult<I> {
925-
let Some(proven_via) = proven_via else {
933+
let Some(goal) = self.normalize_goal_self_ty(goal)? else {
934+
return self.forced_ambiguity(MaybeCause::Ambiguity).map(|c| c.result);
935+
};
936+
937+
let cx = self.cx();
938+
let trait_ref = goal.predicate.trait_ref(cx);
939+
let (_, Some(proven_via)) = self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
940+
let trait_goal: Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
941+
ecx.compute_trait_goal_for_normalized_self_ty(trait_goal)
942+
})?
943+
else {
926944
// We don't care about overflow. If proving the trait goal overflowed, then
927945
// it's enough to report an overflow error for that, we don't also have to
928946
// overflow during normalization.

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_type_ir::fast_reject::DeepRejectCtxt;
55
use rustc_type_ir::inherent::*;
66
use rustc_type_ir::lang_items::TraitSolverLangItem;
77
use rustc_type_ir::solve::SizedTraitKind;
8-
use rustc_type_ir::solve::inspect::ProbeKind;
98
use rustc_type_ir::{self as ty, Interner, elaborate};
109
use tracing::instrument;
1110

@@ -394,11 +393,6 @@ where
394393
&mut self,
395394
goal: Goal<I, ty::HostEffectPredicate<I>>,
396395
) -> QueryResult<I> {
397-
let (_, proven_via) = self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
398-
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
399-
goal.with(ecx.cx(), goal.predicate.trait_ref);
400-
ecx.compute_trait_goal(trait_goal)
401-
})?;
402-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
396+
self.assemble_and_merge_candidates(goal, |_ecx| Err(NoSolution))
403397
}
404398
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ where
591591
if let Some(kind) = kind.no_bound_vars() {
592592
match kind {
593593
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
594-
self.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
594+
self.compute_trait_goal(Goal { param_env, predicate })
595595
}
596596
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
597597
self.compute_host_effect_goal(Goal { param_env, predicate })

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,16 @@ where
3232
debug_assert!(self.term_is_fully_unconstrained(goal));
3333
let cx = self.cx();
3434
match goal.predicate.alias.kind(cx) {
35-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
36-
let trait_ref = goal.predicate.alias.trait_ref(cx);
37-
let (_, proven_via) =
38-
self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
39-
let trait_goal: Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
40-
ecx.compute_trait_goal(trait_goal)
41-
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
35+
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => self
36+
.assemble_and_merge_candidates(goal, |ecx| {
4337
ecx.probe(|&result| ProbeKind::RigidAlias { result }).enter(|this| {
4438
this.structurally_instantiate_normalizes_to_term(
4539
goal,
4640
goal.predicate.alias,
4741
);
4842
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
4943
})
50-
})
51-
}
44+
}),
5245
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5346
self.normalize_inherent_associated_term(goal)
5447
}

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,17 @@ where
14331433
pub(super) fn compute_trait_goal(
14341434
&mut self,
14351435
goal: Goal<I, TraitPredicate<I>>,
1436+
) -> QueryResult<I> {
1437+
let Some(goal) = self.normalize_goal_self_ty(goal)? else {
1438+
return self.forced_ambiguity(MaybeCause::Ambiguity).map(|c| c.result);
1439+
};
1440+
self.compute_trait_goal_for_normalized_self_ty(goal).map(|(r, _via)| r)
1441+
}
1442+
1443+
#[instrument(level = "trace", skip(self))]
1444+
pub(super) fn compute_trait_goal_for_normalized_self_ty(
1445+
&mut self,
1446+
goal: Goal<I, TraitPredicate<I>>,
14361447
) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
14371448
let candidates = self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
14381449
self.merge_trait_candidates(candidates)

0 commit comments

Comments
 (0)