1
Fork 0

Auto merge of #122747 - Urgau:non-local-defs_perfect_impl, r=lcnr

Implement T-types suggested logic for perfect non-local impl detection

This implement [T-types suggested logic](https://github.com/rust-lang/rust/issues/121621#issuecomment-1976826895) for perfect non-local impl detection:

> for each impl, instantiate all local types with inference vars and then assemble candidates for that goal, if there are more than 1 (non-private impls), it does not leak

This extension to the current logic is meant to address issues reported in https://github.com/rust-lang/rust/issues/121621.

This PR also re-enables the lint `non_local_definitions` to warn-by-default.

Implementation was discussed in this [zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/144729-t-types/topic/Implementing.20new.20non-local.20impl.20defs.20logic).

Fixes https://github.com/rust-lang/rust/issues/121621
Fixes https://github.com/rust-lang/rust/issues/121746

r? `@lcnr` *(feel free to re-roll)*
This commit is contained in:
bors 2024-04-05 20:09:57 +00:00
commit 9d79cd5f79
7 changed files with 440 additions and 173 deletions

View file

@ -7,18 +7,21 @@ use rustc_span::{Span, DUMMY_SP};
use crate::traits::ObligationCtxt;
pub enum Ambiguity {
#[derive(Debug)]
pub enum CandidateSource {
DefId(DefId),
ParamEnv(Span),
}
pub fn recompute_applicable_impls<'tcx>(
pub fn compute_applicable_impls_for_diagnostics<'tcx>(
infcx: &InferCtxt<'tcx>,
obligation: &PolyTraitObligation<'tcx>,
) -> Vec<Ambiguity> {
) -> Vec<CandidateSource> {
let tcx = infcx.tcx;
let param_env = obligation.param_env;
let predicate_polarity = obligation.predicate.skip_binder().polarity;
let impl_may_apply = |impl_def_id| {
let ocx = ObligationCtxt::new(infcx);
infcx.enter_forall(obligation.predicate, |placeholder_obligation| {
@ -40,6 +43,15 @@ pub fn recompute_applicable_impls<'tcx>(
return false;
}
let impl_trait_header = tcx.impl_trait_header(impl_def_id).unwrap();
let impl_polarity = impl_trait_header.polarity;
match (impl_polarity, predicate_polarity) {
(ty::ImplPolarity::Positive, ty::PredicatePolarity::Positive)
| (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => {}
_ => return false,
}
let impl_predicates = tcx.predicates_of(impl_def_id).instantiate(tcx, impl_args);
ocx.register_obligations(impl_predicates.predicates.iter().map(|&predicate| {
Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)
@ -86,7 +98,7 @@ pub fn recompute_applicable_impls<'tcx>(
obligation.predicate.skip_binder().trait_ref.self_ty(),
|impl_def_id| {
if infcx.probe(|_| impl_may_apply(impl_def_id)) {
ambiguities.push(Ambiguity::DefId(impl_def_id))
ambiguities.push(CandidateSource::DefId(impl_def_id))
}
},
);
@ -101,9 +113,9 @@ pub fn recompute_applicable_impls<'tcx>(
if kind.rebind(trait_pred.trait_ref)
== ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_pred.def_id()))
{
ambiguities.push(Ambiguity::ParamEnv(tcx.def_span(trait_pred.def_id())))
ambiguities.push(CandidateSource::ParamEnv(tcx.def_span(trait_pred.def_id())))
} else {
ambiguities.push(Ambiguity::ParamEnv(span))
ambiguities.push(CandidateSource::ParamEnv(span))
}
}
}

View file

@ -1,6 +1,6 @@
// ignore-tidy-filelength :(
mod ambiguity;
pub mod ambiguity;
mod infer_ctxt_ext;
pub mod on_unimplemented;
pub mod suggestions;

View file

@ -10,7 +10,7 @@ use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::InferCtxtExt as _;
use crate::infer::{self, InferCtxt};
use crate::traits::error_reporting::infer_ctxt_ext::InferCtxtExt;
use crate::traits::error_reporting::{ambiguity, ambiguity::Ambiguity::*};
use crate::traits::error_reporting::{ambiguity, ambiguity::CandidateSource::*};
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::specialize::to_pretty_impl_header;
use crate::traits::NormalizeExt;
@ -2386,7 +2386,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
)
};
let mut ambiguities = ambiguity::recompute_applicable_impls(
let mut ambiguities = ambiguity::compute_applicable_impls_for_diagnostics(
self.infcx,
&obligation.with(self.tcx, trait_ref),
);
@ -2702,7 +2702,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
fn annotate_source_of_ambiguity(
&self,
err: &mut Diag<'_>,
ambiguities: &[ambiguity::Ambiguity],
ambiguities: &[ambiguity::CandidateSource],
predicate: ty::Predicate<'tcx>,
) {
let mut spans = vec![];
@ -2711,7 +2711,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let mut has_param_env = false;
for ambiguity in ambiguities {
match ambiguity {
ambiguity::Ambiguity::DefId(impl_def_id) => {
ambiguity::CandidateSource::DefId(impl_def_id) => {
match self.tcx.span_of_impl(*impl_def_id) {
Ok(span) => spans.push(span),
Err(name) => {
@ -2722,7 +2722,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
}
ambiguity::Ambiguity::ParamEnv(span) => {
ambiguity::CandidateSource::ParamEnv(span) => {
has_param_env = true;
spans.push(*span);
}