get rid of to_poly_trait_predicate

This commit is contained in:
Oli Scherer 2022-11-18 14:10:36 +00:00 committed by Santiago Pastorino
parent 65a6e22668
commit 08afabddac
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
12 changed files with 40 additions and 49 deletions

View File

@ -792,8 +792,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
self.param_env, self.param_env,
ty::Binder::dummy( ty::Binder::dummy(
self.tcx.at(self.cause.span).mk_trait_ref(hir::LangItem::PointerSized, [a]), self.tcx.at(self.cause.span).mk_trait_ref(hir::LangItem::PointerSized, [a]),
) ),
.to_poly_trait_predicate(),
)); ));
Ok(InferOk { Ok(InferOk {

View File

@ -1096,8 +1096,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty::Binder::dummy(self.tcx.mk_trait_ref( ty::Binder::dummy(self.tcx.mk_trait_ref(
into_def_id, into_def_id,
[expr_ty, expected_ty] [expr_ty, expected_ty]
)) )),
.to_poly_trait_predicate(),
)) ))
{ {
let sugg = if expr.precedence().order() >= PREC_POSTFIX { let sugg = if expr.precedence().order() >= PREC_POSTFIX {

View File

@ -1430,7 +1430,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
trait_ref: ty::TraitRef<'tcx>, trait_ref: ty::TraitRef<'tcx>,
) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> { ) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
let cause = traits::ObligationCause::misc(self.span, self.body_id); 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); let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, predicate);
traits::SelectionContext::new(self).select(&obligation) traits::SelectionContext::new(self).select(&obligation)
} }

View File

@ -1153,6 +1153,25 @@ impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, PredicateKind<'tc
} }
} }
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> { impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyTraitPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
self.map_bound(PredicateKind::Trait).to_predicate(tcx) self.map_bound(PredicateKind::Trait).to_predicate(tcx)

View File

@ -853,23 +853,6 @@ impl<'tcx> PolyTraitRef<'tcx> {
pub fn def_id(&self) -> DefId { pub fn def_id(&self) -> DefId {
self.skip_binder().def_id 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<'_> { impl rustc_errors::IntoDiagnosticArg for PolyTraitRef<'_> {

View File

@ -10,7 +10,7 @@ use crate::traits::project::ProjectAndUnifyResult;
use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::visit::TypeVisitable; 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}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
@ -88,19 +88,22 @@ impl<'tcx> AutoTraitFinder<'tcx> {
let trait_ref = tcx.mk_trait_ref(trait_did, [ty]); 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 infcx = tcx.infer_ctxt().build();
let mut selcx = SelectionContext::new(&infcx); let mut selcx = SelectionContext::new(&infcx);
for f in [ for polarity in [true, false] {
PolyTraitRef::to_poly_trait_predicate,
PolyTraitRef::to_poly_trait_predicate_negative_polarity,
] {
let result = selcx.select(&Obligation::new( let result = selcx.select(&Obligation::new(
tcx, tcx,
ObligationCause::dummy(), ObligationCause::dummy(),
orig_env, 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 { if let Ok(Some(ImplSource::UserDefined(_))) = result {
debug!( debug!(

View File

@ -39,8 +39,7 @@ pub fn codegen_select_candidate<'tcx>(
let mut selcx = SelectionContext::new(&infcx); let mut selcx = SelectionContext::new(&infcx);
let obligation_cause = ObligationCause::dummy(); let obligation_cause = ObligationCause::dummy();
let obligation = let obligation = Obligation::new(tcx, obligation_cause, param_env, trait_ref);
Obligation::new(tcx, obligation_cause, param_env, trait_ref.to_poly_trait_predicate());
let selection = match selcx.select(&obligation) { let selection = match selcx.select(&obligation) {
Ok(Some(selection)) => selection, Ok(Some(selection)) => selection,

View File

@ -2111,7 +2111,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
) )
}; };
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); let mut selcx = SelectionContext::new(&self);
match selcx.select_from_obligation(&obligation) { match selcx.select_from_obligation(&obligation) {
Ok(None) => { Ok(None) => {

View File

@ -477,7 +477,7 @@ fn subst_and_check_impossible_predicates<'tcx>(
// associated items. // associated items.
if let Some(trait_def_id) = tcx.trait_of_item(key.0) { 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); 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()); predicates.retain(|predicate| !predicate.needs_subst());

View File

@ -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)); obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id));
// FIXME(named-returns): Binders // FIXME(named-returns): Binders
let trait_predicate = let trait_predicate =
ty::Binder::dummy(ty::TraitRef { def_id: trait_def_id, substs: trait_substs }) ty::Binder::dummy(ty::TraitRef { def_id: trait_def_id, substs: trait_substs });
.to_poly_trait_predicate();
let _ = selcx.infcx().commit_if_ok(|_| { let _ = selcx.infcx().commit_if_ok(|_| {
match selcx.select(&obligation.with(tcx, trait_predicate)) { match selcx.select(&obligation.with(tcx, trait_predicate)) {
@ -1527,7 +1526,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
// If we are resolving `<T as TraitRef<...>>::Item == Type`, // If we are resolving `<T as TraitRef<...>>::Item == Type`,
// start out by selecting the predicate `T as TraitRef<...>`: // start out by selecting the predicate `T as TraitRef<...>`:
let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx())); 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 _ = selcx.infcx().commit_if_ok(|_| {
let impl_source = match selcx.select(&trait_obligation) { let impl_source = match selcx.select(&trait_obligation) {
Ok(Some(impl_source)) => impl_source, Ok(Some(impl_source)) => impl_source,

View File

@ -640,12 +640,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
); );
let tr = let tr =
ty::Binder::dummy(self.tcx().at(cause.span).mk_trait_ref(LangItem::Sized, [output_ty])); ty::Binder::dummy(self.tcx().at(cause.span).mk_trait_ref(LangItem::Sized, [output_ty]));
nested.push(Obligation::new( nested.push(Obligation::new(self.infcx.tcx, cause, obligation.param_env, tr));
self.infcx.tcx,
cause,
obligation.param_env,
tr.to_poly_trait_predicate(),
));
Ok(ImplSourceFnPointerData { fn_ty: self_ty, nested }) Ok(ImplSourceFnPointerData { fn_ty: self_ty, nested })
} }

View File

@ -67,12 +67,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.instantiate(cx.tcx, impl_substs) .instantiate(cx.tcx, impl_substs)
.predicates .predicates
.into_iter() .into_iter()
.chain(Some( .chain(Some(ty::Binder::dummy(impl_trait_ref).to_predicate(infcx.tcx)));
ty::Binder::dummy(impl_trait_ref)
.to_poly_trait_predicate()
.map_bound(ty::PredicateKind::Trait)
.to_predicate(infcx.tcx),
));
for predicate in predicates { for predicate in predicates {
debug!("testing predicate {:?}", predicate); debug!("testing predicate {:?}", predicate);
let obligation = traits::Obligation::new( let obligation = traits::Obligation::new(