Skip to content

Commit ad88732

Browse files
Fix perf issue for auto trait selection
1 parent 3fe3b89 commit ad88732

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

compiler/rustc_middle/src/ty/sty.rs

+9
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,15 @@ impl<'tcx> PolyTraitRef<'tcx> {
999999
polarity: ty::ImplPolarity::Positive,
10001000
})
10011001
}
1002+
1003+
/// Same as [`PolyTraitRef::to_poly_trait_predicate`] but sets a negative polarity instead.
1004+
pub fn to_poly_trait_predicate_negative_polarity(&self) -> ty::PolyTraitPredicate<'tcx> {
1005+
self.map_bound(|trait_ref| ty::TraitPredicate {
1006+
trait_ref,
1007+
constness: ty::BoundConstness::NotConst,
1008+
polarity: ty::ImplPolarity::Negative,
1009+
})
1010+
}
10021011
}
10031012

10041013
/// An existential reference to a trait, where `Self` is erased.

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+18
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ impl<'tcx> AutoTraitFinder<'tcx> {
9494
trait_pred.to_poly_trait_predicate(),
9595
));
9696

97+
match result {
98+
Ok(Some(ImplSource::UserDefined(_))) => {
99+
debug!(
100+
"find_auto_trait_generics({:?}): \
101+
manual impl found, bailing out",
102+
trait_ref
103+
);
104+
return true;
105+
}
106+
_ => {}
107+
}
108+
109+
let result = selcx.select(&Obligation::new(
110+
ObligationCause::dummy(),
111+
orig_env,
112+
trait_pred.to_poly_trait_predicate_negative_polarity(),
113+
));
114+
97115
match result {
98116
Ok(Some(ImplSource::UserDefined(_))) => {
99117
debug!(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12721272
// the master cache. Since coherence executes pretty quickly,
12731273
// it's not worth going to more trouble to increase the
12741274
// hit-rate, I don't think.
1275-
if self.intercrate || self.allow_negative_impls {
1275+
if self.intercrate {
12761276
return false;
12771277
}
12781278

@@ -1289,7 +1289,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12891289
// mode, so don't do any caching. In particular, we might
12901290
// re-use the same `InferCtxt` with both an intercrate
12911291
// and non-intercrate `SelectionContext`
1292-
if self.intercrate || self.allow_negative_impls {
1292+
if self.intercrate {
12931293
return None;
12941294
}
12951295
let tcx = self.tcx();

0 commit comments

Comments
 (0)