Skip to content

Commit 50cc7ff

Browse files
committed
optimize candidate assembly
1 parent 9a96f37 commit 50cc7ff

4 files changed

Lines changed: 88 additions & 23 deletions

File tree

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use rustc_type_ir::solve::{
1414
RerunNonErased, RerunReason, RerunResultExt, SizedTraitKind, StalledOnCoroutines,
1515
};
1616
use rustc_type_ir::{
17-
self as ty, AliasTy, CallerBoundsIterator, Const, Interner, MayBeErased, Region, TypeFlags,
18-
TypeFoldable, TypeFolder, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable,
19-
TypeVisitableExt, TypeVisitor, TypingMode, Unnormalized, Upcast, elaborate,
17+
self as ty, AliasTy, Const, Interner, MayBeErased, Region, TypeFlags, TypeFoldable, TypeFolder,
18+
TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
19+
TypingMode, Unnormalized, Upcast, elaborate,
2020
};
2121
use tracing::{debug, instrument};
2222

@@ -243,6 +243,11 @@ where
243243
assumption: I::Clause,
244244
) -> Result<(), NoSolution>;
245245

246+
fn non_fast_rejected_caller_bounds(
247+
ecx: &mut EvalCtxt<'_, D>,
248+
goal: Goal<I, Self>,
249+
) -> impl Iterator<Item = I::Clause>;
250+
246251
/// Relate the goal and assumption.
247252
fn match_assumption(
248253
ecx: &mut EvalCtxt<'_, D>,
@@ -754,19 +759,12 @@ where
754759
candidates: &mut Vec<Candidate<I>>,
755760
failed_candidate_info: &mut FailedCandidateInfo,
756761
) -> Result<(), RerunNonErased> {
757-
let goal_lang_item = self.cx().as_trait_lang_item(goal.predicate.trait_def_id(self.cx()));
758-
759-
for assumption in goal.param_env.caller_bounds().all_clauses(self.cx()).iter() {
760-
match G::fast_reject_assumption(self, goal, goal_lang_item, assumption) {
761-
Ok(()) => {
762-
match G::probe_and_consider_param_env_candidate(self, goal, assumption)? {
763-
Ok(candidate) => candidates.push(candidate),
764-
Err(head_usages) => {
765-
failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
766-
}
767-
}
762+
for assumption in G::non_fast_rejected_caller_bounds(self, goal).collect::<Vec<_>>() {
763+
match G::probe_and_consider_param_env_candidate(self, goal, assumption)? {
764+
Ok(candidate) => candidates.push(candidate),
765+
Err(head_usages) => {
766+
failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
768767
}
769-
Err(NoSolution) => {}
770768
}
771769
}
772770

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_type_ir::solve::{
99
AliasBoundKind, NoSolutionOrRerunNonErased, QueryResultOrRerunNonErased, RerunNonErased,
1010
SizedTraitKind,
1111
};
12-
use rustc_type_ir::{self as ty, Interner, Unnormalized, elaborate};
12+
use rustc_type_ir::{self as ty, CallerBoundsIterator, Interner, Unnormalized, elaborate};
1313
use tracing::instrument;
1414

1515
use super::assembly::{Candidate, structural_traits};
@@ -38,7 +38,17 @@ where
3838
fn trait_def_id(self, _: I) -> I::TraitId {
3939
self.def_id()
4040
}
41-
41+
fn non_fast_rejected_caller_bounds(
42+
ecx: &mut EvalCtxt<'_, D>,
43+
goal: Goal<I, Self>,
44+
) -> impl Iterator<Item = I::Clause> {
45+
goal.param_env.caller_bounds().non_trait_clauses().iter().filter(move |&assumption| {
46+
match Self::fast_reject_assumption(ecx, goal, None, assumption) {
47+
Ok(()) => true,
48+
Err(NoSolution) => false,
49+
}
50+
})
51+
}
4252
fn fast_reject_assumption(
4353
ecx: &mut EvalCtxt<'_, D>,
4454
goal: Goal<I, Self>,

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rustc_type_ir::solve::{
88
RerunNonErased, RerunReason, RerunResultExt,
99
};
1010
use rustc_type_ir::{
11-
self as ty, Const, FieldInfo, Interner, NormalizesTo, PredicateKind, Region, Unnormalized,
12-
Upcast as _,
11+
self as ty, CallerBoundsIterator, Const, FieldInfo, Interner, NormalizesTo, PredicateKind,
12+
Region, Unnormalized, Upcast as _,
1313
};
1414
use tracing::instrument;
1515

@@ -180,6 +180,18 @@ where
180180
self.trait_def_id(cx)
181181
}
182182

183+
fn non_fast_rejected_caller_bounds(
184+
ecx: &mut EvalCtxt<'_, D>,
185+
goal: Goal<I, Self>,
186+
) -> impl Iterator<Item = I::Clause> {
187+
goal.param_env.caller_bounds().non_trait_clauses().iter().filter(move |&assumption| {
188+
match Self::fast_reject_assumption(ecx, goal, None, assumption) {
189+
Ok(()) => true,
190+
Err(NoSolution) => false,
191+
}
192+
})
193+
}
194+
183195
fn fast_reject_assumption(
184196
ecx: &mut EvalCtxt<'_, D>,
185197
goal: Goal<I, Self>,

‎compiler/rustc_next_trait_solver/src/solve/trait_goals.rs‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use rustc_type_ir::solve::{
1010
QueryResultOrRerunNonErased, RerunNonErased, RerunReason, RerunResultExt, SizedTraitKind,
1111
};
1212
use rustc_type_ir::{
13-
self as ty, ClausePolarity, ExistentialPredicate, FieldInfo, IncludeSizedImplicitBounds,
14-
Interner, MayBeErased, Movability, Region, TraitClause, TraitRef, TypeVisitableExt as _,
15-
TypingMode, Unnormalized, Upcast as _, elaborate,
13+
self as ty, CallerBoundsIterator, ClausePolarity, ExistentialPredicate, FieldInfo,
14+
IncludeSizedImplicitBounds, Interner, MayBeErased, Movability, Region, TraitClause, TraitRef,
15+
TypeVisitableExt as _, TypingMode, Unnormalized, Upcast as _, elaborate,
1616
};
1717
use tracing::{debug, instrument, trace, warn};
1818

@@ -133,6 +133,51 @@ where
133133
.enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
134134
}
135135

136+
fn non_fast_rejected_caller_bounds(
137+
ecx: &mut EvalCtxt<'_, D>,
138+
goal: Goal<I, Self>,
139+
) -> impl Iterator<Item = I::Clause> {
140+
fn trait_def_id_matches<I: Interner>(
141+
cx: I,
142+
clause_def_id: I::TraitId,
143+
goal_def_id: I::TraitId,
144+
polarity: ClausePolarity,
145+
) -> bool {
146+
clause_def_id == goal_def_id
147+
// PERF(sized-hierarchy): Sizedness supertraits aren't elaborated to improve perf, so
148+
// check for a `MetaSized` supertrait being matched against a `Sized` assumption.
149+
//
150+
// `PointeeSized` bounds are syntactic sugar for a lack of bounds so don't need this.
151+
|| (polarity == ClausePolarity::Positive
152+
&& cx.is_trait_lang_item(clause_def_id, SolverTraitLangItem::Sized)
153+
&& cx.is_trait_lang_item(goal_def_id, SolverTraitLangItem::MetaSized))
154+
}
155+
156+
goal.param_env
157+
.caller_bounds()
158+
.trait_clauses(ecx.cx())
159+
.with_polarity(goal.predicate.polarity)
160+
// .for_trait(goal.predicate.def_id())
161+
// TODO: allow filtering before interning
162+
.interned()
163+
.iter()
164+
.filter(move |&trait_clause| {
165+
let trait_clause = trait_clause
166+
.as_trait_clause()
167+
.expect("caller_bounds only returns trait clauses");
168+
169+
trait_def_id_matches(
170+
ecx.cx(),
171+
trait_clause.def_id(),
172+
goal.predicate.def_id(),
173+
goal.predicate.polarity,
174+
) && DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
175+
goal.predicate.trait_ref.args,
176+
trait_clause.skip_binder().trait_ref.args,
177+
)
178+
})
179+
}
180+
136181
fn fast_reject_assumption(
137182
ecx: &mut EvalCtxt<'_, D>,
138183
goal: Goal<I, Self>,
@@ -170,7 +215,7 @@ where
170215
trait_clause.skip_binder().trait_ref.args,
171216
)
172217
{
173-
return Ok(());
218+
Ok(())
174219
} else {
175220
Err(NoSolution)
176221
}

0 commit comments

Comments
 (0)