From ad887322541b1c255b210cf87fae675072d1027f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 26 Mar 2022 14:58:19 +0100 Subject: [PATCH 1/2] Fix perf issue for auto trait selection --- compiler/rustc_middle/src/ty/sty.rs | 9 +++++++++ .../src/traits/auto_trait.rs | 18 ++++++++++++++++++ .../src/traits/select/mod.rs | 4 ++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 93ece753728..dc16460ca43 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -999,6 +999,15 @@ pub fn to_poly_trait_predicate(&self) -> ty::PolyTraitPredicate<'tcx> { polarity: ty::ImplPolarity::Positive, }) } + + /// Same as [`PolyTraitRef::to_poly_trait_predicate`] but sets a negative polarity instead. + pub fn to_poly_trait_predicate_negative_polarity(&self) -> ty::PolyTraitPredicate<'tcx> { + self.map_bound(|trait_ref| ty::TraitPredicate { + trait_ref, + constness: ty::BoundConstness::NotConst, + polarity: ty::ImplPolarity::Negative, + }) + } } /// An existential reference to a trait, where `Self` is erased. diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index c9398d746d7..6f6f21aa788 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -94,6 +94,24 @@ pub fn find_auto_trait_generics( trait_pred.to_poly_trait_predicate(), )); + match result { + Ok(Some(ImplSource::UserDefined(_))) => { + debug!( + "find_auto_trait_generics({:?}): \ + manual impl found, bailing out", + trait_ref + ); + return true; + } + _ => {} + } + + let result = selcx.select(&Obligation::new( + ObligationCause::dummy(), + orig_env, + trait_pred.to_poly_trait_predicate_negative_polarity(), + )); + match result { Ok(Some(ImplSource::UserDefined(_))) => { debug!( diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 6d232d86d8a..4704d841012 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -1272,7 +1272,7 @@ fn can_use_global_caches(&self, param_env: ty::ParamEnv<'tcx>) -> bool { // the master cache. Since coherence executes pretty quickly, // it's not worth going to more trouble to increase the // hit-rate, I don't think. - if self.intercrate || self.allow_negative_impls { + if self.intercrate { return false; } @@ -1289,7 +1289,7 @@ fn check_candidate_cache( // mode, so don't do any caching. In particular, we might // re-use the same `InferCtxt` with both an intercrate // and non-intercrate `SelectionContext` - if self.intercrate || self.allow_negative_impls { + if self.intercrate { return None; } let tcx = self.tcx(); From bd51f174ed6b6e1e668207cb5999e46189dcfaa3 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 27 Mar 2022 03:01:34 +0200 Subject: [PATCH 2/2] Remove SelectionContext::allow_negative_impls field --- .../src/traits/auto_trait.rs | 4 ++-- .../src/traits/select/mod.rs | 24 ------------------- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 6f6f21aa788..ee9983ee8b8 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -87,7 +87,7 @@ pub fn find_auto_trait_generics( let trait_pred = ty::Binder::dummy(trait_ref); let bail_out = tcx.infer_ctxt().enter(|infcx| { - let mut selcx = SelectionContext::with_negative(&infcx, true); + let mut selcx = SelectionContext::new(&infcx); let result = selcx.select(&Obligation::new( ObligationCause::dummy(), orig_env, @@ -295,7 +295,7 @@ fn evaluate_predicates( fresh_preds.insert(self.clean_pred(infcx, predicate)); } - let mut select = SelectionContext::with_negative(&infcx, true); + let mut select = SelectionContext::new(&infcx); let mut already_visited = FxHashSet::default(); let mut predicates = VecDeque::new(); diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 4704d841012..72d156067a1 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -119,11 +119,6 @@ pub struct SelectionContext<'cx, 'tcx> { intercrate_ambiguity_causes: Option>, - /// Controls whether or not to filter out negative impls when selecting. - /// This is used in librustdoc to distinguish between the lack of an impl - /// and a negative impl - allow_negative_impls: bool, - /// The mode that trait queries run in, which informs our error handling /// policy. In essence, canonicalized queries need their errors propagated /// rather than immediately reported because we do not have accurate spans. @@ -215,7 +210,6 @@ pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>) -> SelectionContext<'cx, 'tcx> { freshener: infcx.freshener_keep_static(), intercrate: false, intercrate_ambiguity_causes: None, - allow_negative_impls: false, query_mode: TraitQueryMode::Standard, } } @@ -226,22 +220,6 @@ pub fn intercrate(infcx: &'cx InferCtxt<'cx, 'tcx>) -> SelectionContext<'cx, 'tc freshener: infcx.freshener_keep_static(), intercrate: true, intercrate_ambiguity_causes: None, - allow_negative_impls: false, - query_mode: TraitQueryMode::Standard, - } - } - - pub fn with_negative( - infcx: &'cx InferCtxt<'cx, 'tcx>, - allow_negative_impls: bool, - ) -> SelectionContext<'cx, 'tcx> { - debug!(?allow_negative_impls, "with_negative"); - SelectionContext { - infcx, - freshener: infcx.freshener_keep_static(), - intercrate: false, - intercrate_ambiguity_causes: None, - allow_negative_impls, query_mode: TraitQueryMode::Standard, } } @@ -256,7 +234,6 @@ pub fn with_query_mode( freshener: infcx.freshener_keep_static(), intercrate: false, intercrate_ambiguity_causes: None, - allow_negative_impls: false, query_mode, } } @@ -1192,7 +1169,6 @@ fn filter_impls( if let ImplCandidate(def_id) = candidate { if ty::ImplPolarity::Reservation == tcx.impl_polarity(def_id) || obligation.polarity() == tcx.impl_polarity(def_id) - || self.allow_negative_impls { result.push(candidate); }