From 08afabddac12adc114a8f963f1a10c47f3b3b2d9 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 18 Nov 2022 14:10:36 +0000 Subject: [PATCH] get rid of to_poly_trait_predicate --- compiler/rustc_hir_typeck/src/coercion.rs | 3 +-- .../src/fn_ctxt/suggestions.rs | 3 +-- compiler/rustc_hir_typeck/src/method/probe.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 19 +++++++++++++++++++ compiler/rustc_middle/src/ty/sty.rs | 17 ----------------- .../src/traits/auto_trait.rs | 19 +++++++++++-------- .../src/traits/codegen.rs | 3 +-- .../src/traits/error_reporting/mod.rs | 2 +- .../rustc_trait_selection/src/traits/mod.rs | 2 +- .../src/traits/project.rs | 5 ++--- .../src/traits/select/confirmation.rs | 7 +------ src/librustdoc/clean/blanket_impl.rs | 7 +------ 12 files changed, 40 insertions(+), 49 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 82784bb8a66..c314694ead4 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -792,8 +792,7 @@ fn coerce_dyn_star( self.param_env, ty::Binder::dummy( self.tcx.at(self.cause.span).mk_trait_ref(hir::LangItem::PointerSized, [a]), - ) - .to_poly_trait_predicate(), + ), )); Ok(InferOk { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index b5aa8cd6e7c..625cd184223 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1096,8 +1096,7 @@ pub(crate) fn suggest_into( ty::Binder::dummy(self.tcx.mk_trait_ref( into_def_id, [expr_ty, expected_ty] - )) - .to_poly_trait_predicate(), + )), )) { let sugg = if expr.precedence().order() >= PREC_POSTFIX { diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 44c3edf06a8..120ef2dac19 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -1430,7 +1430,7 @@ fn select_trait_candidate( trait_ref: ty::TraitRef<'tcx>, ) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> { let cause = traits::ObligationCause::misc(self.span, self.body_id); - let predicate = ty::Binder::dummy(trait_ref).to_poly_trait_predicate(); + let predicate = ty::Binder::dummy(trait_ref); let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, predicate); traits::SelectionContext::new(self).select(&obligation) } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 9d778ff2fb6..8e03cba15ef 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1153,6 +1153,25 @@ fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { } } +impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> { + #[inline(always)] + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + let pred: PolyTraitPredicate<'tcx> = self.to_predicate(tcx); + pred.to_predicate(tcx) + } +} + +impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> { + #[inline(always)] + fn to_predicate(self, _: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> { + self.map_bound(|trait_ref| TraitPredicate { + trait_ref, + constness: ty::BoundConstness::NotConst, + polarity: ty::ImplPolarity::Positive, + }) + } +} + impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyTraitPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { self.map_bound(PredicateKind::Trait).to_predicate(tcx) diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index e7a751fa0af..a53b275fb02 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -853,23 +853,6 @@ pub fn self_ty(&self) -> Binder<'tcx, Ty<'tcx>> { pub fn def_id(&self) -> DefId { self.skip_binder().def_id } - - pub fn to_poly_trait_predicate(&self) -> ty::PolyTraitPredicate<'tcx> { - self.map_bound(|trait_ref| ty::TraitPredicate { - trait_ref, - constness: ty::BoundConstness::NotConst, - 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, - }) - } } impl rustc_errors::IntoDiagnosticArg for PolyTraitRef<'_> { diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 78af187cd8f..66f80e92eb7 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -10,7 +10,7 @@ use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::visit::TypeVisitable; -use rustc_middle::ty::{PolyTraitRef, Region, RegionVid}; +use rustc_middle::ty::{ImplPolarity, Region, RegionVid}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; @@ -88,19 +88,22 @@ pub fn find_auto_trait_generics( let trait_ref = tcx.mk_trait_ref(trait_did, [ty]); - let trait_pred = ty::Binder::dummy(trait_ref); - let infcx = tcx.infer_ctxt().build(); let mut selcx = SelectionContext::new(&infcx); - for f in [ - PolyTraitRef::to_poly_trait_predicate, - PolyTraitRef::to_poly_trait_predicate_negative_polarity, - ] { + for polarity in [true, false] { let result = selcx.select(&Obligation::new( tcx, ObligationCause::dummy(), orig_env, - f(&trait_pred), + ty::Binder::dummy(ty::TraitPredicate { + trait_ref, + constness: ty::BoundConstness::NotConst, + polarity: if polarity { + ImplPolarity::Positive + } else { + ImplPolarity::Negative + }, + }), )); if let Ok(Some(ImplSource::UserDefined(_))) = result { debug!( diff --git a/compiler/rustc_trait_selection/src/traits/codegen.rs b/compiler/rustc_trait_selection/src/traits/codegen.rs index ca4299f7db3..61743d78e9e 100644 --- a/compiler/rustc_trait_selection/src/traits/codegen.rs +++ b/compiler/rustc_trait_selection/src/traits/codegen.rs @@ -39,8 +39,7 @@ pub fn codegen_select_candidate<'tcx>( let mut selcx = SelectionContext::new(&infcx); let obligation_cause = ObligationCause::dummy(); - let obligation = - Obligation::new(tcx, obligation_cause, param_env, trait_ref.to_poly_trait_predicate()); + let obligation = Obligation::new(tcx, obligation_cause, param_env, trait_ref); let selection = match selcx.select(&obligation) { Ok(Some(selection)) => selection, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 5cb0988919d..2d46960d97b 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2111,7 +2111,7 @@ fn maybe_report_ambiguity( ) }; - let obligation = obligation.with(self.tcx, trait_ref.to_poly_trait_predicate()); + let obligation = obligation.with(self.tcx, trait_ref); let mut selcx = SelectionContext::new(&self); match selcx.select_from_obligation(&obligation) { Ok(None) => { diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 548ca1c1d7f..4ab43c4e0b0 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -477,7 +477,7 @@ fn subst_and_check_impossible_predicates<'tcx>( // associated items. if let Some(trait_def_id) = tcx.trait_of_item(key.0) { let trait_ref = ty::TraitRef::from_method(tcx, trait_def_id, key.1); - predicates.push(ty::Binder::dummy(trait_ref).to_poly_trait_predicate().to_predicate(tcx)); + predicates.push(ty::Binder::dummy(trait_ref).to_predicate(tcx)); } predicates.retain(|predicate| !predicate.needs_subst()); diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index f17d702d421..428eea0adc7 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -1328,8 +1328,7 @@ fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>( obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id)); // FIXME(named-returns): Binders let trait_predicate = - ty::Binder::dummy(ty::TraitRef { def_id: trait_def_id, substs: trait_substs }) - .to_poly_trait_predicate(); + ty::Binder::dummy(ty::TraitRef { def_id: trait_def_id, substs: trait_substs }); let _ = selcx.infcx().commit_if_ok(|_| { match selcx.select(&obligation.with(tcx, trait_predicate)) { @@ -1527,7 +1526,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // If we are resolving `>::Item == Type`, // start out by selecting the predicate `T as TraitRef<...>`: let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx())); - let trait_obligation = obligation.with(selcx.tcx(), poly_trait_ref.to_poly_trait_predicate()); + let trait_obligation = obligation.with(selcx.tcx(), poly_trait_ref); let _ = selcx.infcx().commit_if_ok(|_| { let impl_source = match selcx.select(&trait_obligation) { Ok(Some(impl_source)) => impl_source, diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index c0edbebed54..0d739eda75b 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -640,12 +640,7 @@ fn confirm_fn_pointer_candidate( ); let tr = ty::Binder::dummy(self.tcx().at(cause.span).mk_trait_ref(LangItem::Sized, [output_ty])); - nested.push(Obligation::new( - self.infcx.tcx, - cause, - obligation.param_env, - tr.to_poly_trait_predicate(), - )); + nested.push(Obligation::new(self.infcx.tcx, cause, obligation.param_env, tr)); Ok(ImplSourceFnPointerData { fn_ty: self_ty, nested }) } diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index cb0b8d4a9bc..a1145b90d65 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -67,12 +67,7 @@ pub(crate) fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec { .instantiate(cx.tcx, impl_substs) .predicates .into_iter() - .chain(Some( - ty::Binder::dummy(impl_trait_ref) - .to_poly_trait_predicate() - .map_bound(ty::PredicateKind::Trait) - .to_predicate(infcx.tcx), - )); + .chain(Some(ty::Binder::dummy(impl_trait_ref).to_predicate(infcx.tcx))); for predicate in predicates { debug!("testing predicate {:?}", predicate); let obligation = traits::Obligation::new(