Skip to content

Commit a7b89ef

Browse files
committed
perf experiment: revert changes to unelaborated fast path
1 parent e1ad8d3 commit a7b89ef

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
217217
}
218218

219219
selcx.infcx.probe(|_| {
220-
let bound =
221-
util::unelaborated_sizedness_candidate(selcx.infcx, obligation, bound);
220+
if util::unelaborated_sizedness_candidate(selcx.infcx, obligation, bound)
221+
.is_some()
222+
{
223+
candidates.vec.push(ProjectionCandidate(idx));
224+
return;
225+
}
222226

223227
// We checked the polarity already
224228
match selcx.match_normalize_trait_ref(
@@ -258,26 +262,32 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
258262
) -> Result<(), SelectionError<'tcx>> {
259263
debug!(?stack.obligation);
260264

265+
if let Some(bound) = stack
266+
.obligation
267+
.param_env
268+
.caller_bounds()
269+
.iter()
270+
.filter_map(|p| p.as_trait_clause())
271+
.find_map(|p| util::unelaborated_sizedness_candidate(self.infcx, stack.obligation, p))
272+
{
273+
candidates.vec.push(ParamCandidate(bound));
274+
return Ok(());
275+
}
276+
261277
let bounds = stack
262278
.obligation
263279
.param_env
264280
.caller_bounds()
265281
.iter()
266282
.filter_map(|p| p.as_trait_clause())
267-
// Micro-optimization: filter out predicates with different polarities.
283+
// Micro-optimization: filter out predicates relating to different traits.
284+
.filter(|p| p.def_id() == stack.obligation.predicate.def_id())
268285
.filter(|p| p.polarity() == stack.obligation.predicate.polarity());
269286

270287
let drcx = DeepRejectCtxt::relate_rigid_rigid(self.tcx());
271288
let obligation_args = stack.obligation.predicate.skip_binder().trait_ref.args;
272289
// Keep only those bounds which may apply, and propagate overflow if it occurs.
273290
for bound in bounds {
274-
let bound = util::unelaborated_sizedness_candidate(self.infcx, stack.obligation, bound);
275-
276-
// Micro-optimization: filter out predicates relating to different traits.
277-
if bound.def_id() != stack.obligation.predicate.def_id() {
278-
continue;
279-
}
280-
281291
let bound_trait_ref = bound.map_bound(|t| t.trait_ref);
282292
if !drcx.args_may_unify(obligation_args, bound_trait_ref.skip_binder().args) {
283293
continue;

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
168168
let candidate_predicate = candidate_predicate
169169
.as_trait_clause()
170170
.expect("projection candidate is not a trait predicate");
171-
let candidate_predicate =
172-
util::unelaborated_sizedness_candidate(self.infcx, obligation, candidate_predicate);
171+
if util::unelaborated_sizedness_candidate(self.infcx, obligation, candidate_predicate)
172+
.is_some()
173+
{
174+
return Ok(PredicateObligations::new());
175+
}
173176

174177
let candidate = candidate_predicate.map_bound(|t| t.trait_ref);
175178

@@ -228,13 +231,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
228231
) -> PredicateObligations<'tcx> {
229232
debug!(?obligation, ?param, "confirm_param_candidate");
230233

231-
let param = util::unelaborated_sizedness_candidate(
232-
self.infcx,
233-
obligation,
234-
param.upcast(self.infcx.tcx),
235-
)
236-
.map_bound(|p| p.trait_ref);
237-
238234
// During evaluation, we already checked that this
239235
// where-clause trait-ref could be unified with the obligation
240236
// trait-ref. Repeat that unification now without any

compiler/rustc_trait_selection/src/traits/util.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,12 @@ pub(crate) fn unelaborated_sizedness_candidate<'tcx>(
539539
infcx: &InferCtxt<'tcx>,
540540
obligation: &PolyTraitObligation<'tcx>,
541541
candidate: PolyTraitPredicate<'tcx>,
542-
) -> PolyTraitPredicate<'tcx> {
542+
) -> Option<PolyTraitPredicate<'tcx>> {
543543
use crate::infer::InferCtxtExt;
544544
if !infcx.tcx.is_lang_item(obligation.predicate.def_id(), LangItem::MetaSized)
545545
|| !infcx.tcx.is_lang_item(candidate.def_id(), LangItem::Sized)
546546
{
547-
return candidate;
547+
return None;
548548
}
549549

550550
let drcx = DeepRejectCtxt::relate_rigid_rigid(infcx.tcx);
@@ -564,15 +564,15 @@ pub(crate) fn unelaborated_sizedness_candidate<'tcx>(
564564
};
565565

566566
if matches {
567-
candidate.map_bound(|c| TraitPredicate {
567+
Some(candidate.map_bound(|c| TraitPredicate {
568568
trait_ref: TraitRef::new_from_args(
569569
infcx.tcx,
570570
obligation.predicate.def_id(),
571571
c.trait_ref.args,
572572
),
573573
polarity: c.polarity,
574-
})
574+
}))
575575
} else {
576-
candidate
576+
None
577577
}
578578
}

tests/ui/where-clauses/ignore-err-clauses.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::ops::Add;
22

33
fn dbl<T>(x: T) -> <T as Add>::Output
4-
//~^ ERROR type annotations needed
54
where
65
T: Copy + Add,
76
UUU: Copy,
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
error[E0412]: cannot find type `UUU` in this scope
2-
--> $DIR/ignore-err-clauses.rs:7:5
2+
--> $DIR/ignore-err-clauses.rs:6:5
33
|
44
LL | UUU: Copy,
55
| ^^^ not found in this scope
66

7-
error[E0282]: type annotations needed
8-
--> $DIR/ignore-err-clauses.rs:3:14
9-
|
10-
LL | fn dbl<T>(x: T) -> <T as Add>::Output
11-
| ^ cannot infer type for type parameter `T`
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

15-
Some errors have detailed explanations: E0282, E0412.
16-
For more information about an error, try `rustc --explain E0282`.
9+
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)