Stop calling impl_polarity when impl_trait_ref was also called

This commit is contained in:
Oli Scherer 2024-02-10 22:27:03 +00:00
parent ab0e8b3145
commit b43fbe63e7
12 changed files with 96 additions and 77 deletions

View file

@ -39,15 +39,18 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
) -> Result<Candidate<'tcx>, NoSolution> {
let tcx = ecx.tcx();
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
let impl_trait_header = tcx.impl_trait_header(impl_def_id).unwrap();
let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::ForLookup };
if !drcx.args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args) {
if !drcx.args_may_unify(
goal.predicate.trait_ref.args,
impl_trait_header.skip_binder().trait_ref.args,
) {
return Err(NoSolution);
}
let impl_polarity = tcx.impl_polarity(impl_def_id);
// An upper bound of the certainty of this goal, used to lower the certainty
// of reservation impl to ambiguous during coherence.
let impl_polarity = impl_trait_header.skip_binder().polarity;
let maximal_certainty = match impl_polarity {
ty::ImplPolarity::Positive | ty::ImplPolarity::Negative => {
match impl_polarity == goal.predicate.polarity {
@ -63,7 +66,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
let impl_args = ecx.fresh_args_for_item(impl_def_id);
let impl_trait_ref = impl_trait_ref.instantiate(tcx, impl_args);
let impl_trait_ref = impl_trait_header.instantiate(tcx, impl_args).trait_ref;
ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
let where_clause_bounds = tcx

View file

@ -1980,13 +1980,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.tcx
.all_impls(trait_pred.def_id())
.filter_map(|def_id| {
if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative
let imp = self.tcx.impl_trait_header(def_id).unwrap().skip_binder();
if imp.polarity == ty::ImplPolarity::Negative
|| !self.tcx.is_user_visible_dep(def_id.krate)
{
return None;
}
let imp = self.tcx.impl_trait_ref(def_id).unwrap().skip_binder();
let imp = imp.trait_ref;
self.fuzzy_match_tys(trait_pred.skip_binder().self_ty(), imp.self_ty(), false).map(
|similarity| ImplCandidate { trait_ref: imp, similarity, impl_def_id: def_id },
@ -2165,12 +2165,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.tcx
.all_impls(def_id)
// Ignore automatically derived impls and `!Trait` impls.
.filter(|&def_id| {
self.tcx.impl_polarity(def_id) != ty::ImplPolarity::Negative
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
.map(ty::EarlyBinder::instantiate_identity)
.filter(|header| {
header.polarity != ty::ImplPolarity::Negative
|| self.tcx.is_automatically_derived(def_id)
})
.filter_map(|def_id| self.tcx.impl_trait_ref(def_id))
.map(ty::EarlyBinder::instantiate_identity)
.map(|header| header.trait_ref)
.filter(|trait_ref| {
let self_ty = trait_ref.self_ty();
// Avoid mentioning type parameters.

View file

@ -560,20 +560,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// Before we create the substitutions and everything, first
// consider a "quick reject". This avoids creating more types
// and so forth that we need to.
let impl_trait_ref = self.tcx().impl_trait_ref(impl_def_id).unwrap();
if !drcx.args_may_unify(obligation_args, impl_trait_ref.skip_binder().args) {
let impl_trait_header = self.tcx().impl_trait_header(impl_def_id).unwrap();
if !drcx
.args_may_unify(obligation_args, impl_trait_header.skip_binder().trait_ref.args)
{
return;
}
if self.reject_fn_ptr_impls(
impl_def_id,
obligation,
impl_trait_ref.skip_binder().self_ty(),
impl_trait_header.skip_binder().trait_ref.self_ty(),
) {
return;
}
self.infcx.probe(|_| {
if let Ok(_args) = self.match_impl(impl_def_id, impl_trait_ref, obligation) {
if let Ok(_args) = self.match_impl(impl_def_id, impl_trait_header, obligation) {
candidates.vec.push(ImplCandidate(impl_def_id));
}
});

View file

@ -2404,8 +2404,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
impl_def_id: DefId,
obligation: &PolyTraitObligation<'tcx>,
) -> Normalized<'tcx, GenericArgsRef<'tcx>> {
let impl_trait_ref = self.tcx().impl_trait_ref(impl_def_id).unwrap();
match self.match_impl(impl_def_id, impl_trait_ref, obligation) {
let impl_trait_header = self.tcx().impl_trait_header(impl_def_id).unwrap();
match self.match_impl(impl_def_id, impl_trait_header, obligation) {
Ok(args) => args,
Err(()) => {
// FIXME: A rematch may fail when a candidate cache hit occurs
@ -2438,7 +2438,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
fn match_impl(
&mut self,
impl_def_id: DefId,
impl_trait_ref: EarlyBinder<ty::TraitRef<'tcx>>,
impl_trait_header: EarlyBinder<ty::ImplTraitHeader<'tcx>>,
obligation: &PolyTraitObligation<'tcx>,
) -> Result<Normalized<'tcx, GenericArgsRef<'tcx>>, ()> {
let placeholder_obligation =
@ -2447,12 +2447,12 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
let impl_args = self.infcx.fresh_args_for_item(obligation.cause.span, impl_def_id);
let impl_trait_ref = impl_trait_ref.instantiate(self.tcx(), impl_args);
if impl_trait_ref.references_error() {
let impl_trait_header = impl_trait_header.instantiate(self.tcx(), impl_args);
if impl_trait_header.references_error() {
return Err(());
}
debug!(?impl_trait_ref);
debug!(?impl_trait_header);
let Normalized { value: impl_trait_ref, obligations: mut nested_obligations } =
ensure_sufficient_stack(|| {
@ -2461,7 +2461,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
obligation.param_env,
obligation.cause.clone(),
obligation.recursion_depth + 1,
impl_trait_ref,
impl_trait_header.trait_ref,
)
});
@ -2482,9 +2482,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
})?;
nested_obligations.extend(obligations);
if !self.is_intercrate()
&& self.tcx().impl_polarity(impl_def_id) == ty::ImplPolarity::Reservation
{
if !self.is_intercrate() && impl_trait_header.polarity == ty::ImplPolarity::Reservation {
debug!("reservation impls only apply in intercrate mode");
return Err(());
}

View file

@ -168,6 +168,8 @@ pub(super) fn specializes(tcx: TyCtxt<'_>, (impl1_def_id, impl2_def_id): (DefId,
}
}
let impl1_trait_header = tcx.impl_trait_header(impl1_def_id).unwrap().instantiate_identity();
// We determine whether there's a subset relationship by:
//
// - replacing bound vars with placeholders in impl1,
@ -181,21 +183,25 @@ pub(super) fn specializes(tcx: TyCtxt<'_>, (impl1_def_id, impl2_def_id): (DefId,
// See RFC 1210 for more details and justification.
// Currently we do not allow e.g., a negative impl to specialize a positive one
if tcx.impl_polarity(impl1_def_id) != tcx.impl_polarity(impl2_def_id) {
if impl1_trait_header.polarity != tcx.impl_polarity(impl2_def_id) {
return false;
}
// create a parameter environment corresponding to a (placeholder) instantiation of impl1
let penv = tcx.param_env(impl1_def_id);
let impl1_trait_ref = tcx.impl_trait_ref(impl1_def_id).unwrap().instantiate_identity();
// Create an infcx, taking the predicates of impl1 as assumptions:
let infcx = tcx.infer_ctxt().build();
// Attempt to prove that impl2 applies, given all of the above.
fulfill_implication(&infcx, penv, impl1_trait_ref, impl1_def_id, impl2_def_id, |_, _| {
ObligationCause::dummy()
})
fulfill_implication(
&infcx,
penv,
impl1_trait_header.trait_ref,
impl1_def_id,
impl2_def_id,
|_, _| ObligationCause::dummy(),
)
.is_ok()
}