Use DeepRejectCtxt to quickly reject ParamEnv candidates

This commit is contained in:
Bryanskiy 2024-08-04 19:52:15 +03:00
parent 717aec0f8e
commit c51953f4d8
16 changed files with 287 additions and 167 deletions

View File

@ -90,7 +90,8 @@ fn check_def_id(
} }
} }
if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) { if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer)
{
self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id); self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id);
} else { } else {
bug!("unexpected self type: {:?}", self_ty); bug!("unexpected self type: {:?}", self_ty);
@ -129,7 +130,7 @@ fn check_primitive_impl(
} }
} }
if let Some(simp) = simplify_type(self.tcx, ty, TreatParams::AsCandidateKey) { if let Some(simp) = simplify_type(self.tcx, ty, TreatParams::InstantiateWithInfer) {
self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id); self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id);
} else { } else {
bug!("unexpected primitive type: {:?}", ty); bug!("unexpected primitive type: {:?}", ty);

View File

@ -714,7 +714,7 @@ fn assemble_probe(&mut self, self_ty: &Canonical<'tcx, QueryResponse<'tcx, Ty<'t
} }
fn assemble_inherent_candidates_for_incoherent_ty(&mut self, self_ty: Ty<'tcx>) { fn assemble_inherent_candidates_for_incoherent_ty(&mut self, self_ty: Ty<'tcx>) {
let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) else { let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
bug!("unexpected incoherent type: {:?}", self_ty) bug!("unexpected incoherent type: {:?}", self_ty)
}; };
for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter().flatten() { for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter().flatten() {

View File

@ -2234,8 +2234,7 @@ fn suggest_associated_call_syntax(
let target_ty = self let target_ty = self
.autoderef(sugg_span, rcvr_ty) .autoderef(sugg_span, rcvr_ty)
.find(|(rcvr_ty, _)| { .find(|(rcvr_ty, _)| {
DeepRejectCtxt::new(self.tcx, TreatParams::ForLookup) DeepRejectCtxt::relate_rigid_infer(self.tcx).types_may_unify(*rcvr_ty, impl_ty)
.types_may_unify(*rcvr_ty, impl_ty)
}) })
.map_or(impl_ty, |(ty, _)| ty) .map_or(impl_ty, |(ty, _)| ty)
.peel_refs(); .peel_refs();
@ -2497,7 +2496,7 @@ fn report_failed_method_call_on_numerical_infer_var(
.into_iter() .into_iter()
.any(|info| self.associated_value(info.def_id, item_name).is_some()); .any(|info| self.associated_value(info.def_id, item_name).is_some());
let found_assoc = |ty: Ty<'tcx>| { let found_assoc = |ty: Ty<'tcx>| {
simplify_type(tcx, ty, TreatParams::AsCandidateKey) simplify_type(tcx, ty, TreatParams::InstantiateWithInfer)
.and_then(|simp| { .and_then(|simp| {
tcx.incoherent_impls(simp) tcx.incoherent_impls(simp)
.into_iter() .into_iter()
@ -3962,7 +3961,7 @@ enum Introducer {
// cases where a positive bound implies a negative impl. // cases where a positive bound implies a negative impl.
(candidates, Vec::new()) (candidates, Vec::new())
} else if let Some(simp_rcvr_ty) = } else if let Some(simp_rcvr_ty) =
simplify_type(self.tcx, rcvr_ty, TreatParams::ForLookup) simplify_type(self.tcx, rcvr_ty, TreatParams::AsRigid)
{ {
let mut potential_candidates = Vec::new(); let mut potential_candidates = Vec::new();
let mut explicitly_negative = Vec::new(); let mut explicitly_negative = Vec::new();
@ -3980,7 +3979,7 @@ enum Introducer {
.any(|header| { .any(|header| {
let imp = header.trait_ref.instantiate_identity(); let imp = header.trait_ref.instantiate_identity();
let imp_simp = let imp_simp =
simplify_type(self.tcx, imp.self_ty(), TreatParams::ForLookup); simplify_type(self.tcx, imp.self_ty(), TreatParams::AsRigid);
imp_simp.is_some_and(|s| s == simp_rcvr_ty) imp_simp.is_some_and(|s| s == simp_rcvr_ty)
}) })
{ {

View File

@ -2017,7 +2017,7 @@ fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
let simplified_self_ty = fast_reject::simplify_type( let simplified_self_ty = fast_reject::simplify_type(
self.tcx, self.tcx,
trait_ref.self_ty(), trait_ref.self_ty(),
TreatParams::AsCandidateKey, TreatParams::InstantiateWithInfer,
); );
trait_impls trait_impls
.entry(trait_ref.def_id) .entry(trait_ref.def_id)

View File

@ -428,7 +428,7 @@ fn for_each_relevant_impl(
let simp = ty::fast_reject::simplify_type( let simp = ty::fast_reject::simplify_type(
tcx, tcx,
self_ty, self_ty,
ty::fast_reject::TreatParams::ForLookup, ty::fast_reject::TreatParams::AsRigid,
) )
.unwrap(); .unwrap();
consider_impls_for_simplified_type(simp); consider_impls_for_simplified_type(simp);

View File

@ -3,6 +3,14 @@
use super::TyCtxt; use super::TyCtxt;
pub type DeepRejectCtxt<'tcx> = rustc_type_ir::fast_reject::DeepRejectCtxt<TyCtxt<'tcx>>; pub type DeepRejectCtxt<
'tcx,
const INSTANTIATE_LHS_WITH_INFER: bool,
const INSTANTIATE_RHS_WITH_INFER: bool,
> = rustc_type_ir::fast_reject::DeepRejectCtxt<
TyCtxt<'tcx>,
INSTANTIATE_LHS_WITH_INFER,
INSTANTIATE_RHS_WITH_INFER,
>;
pub type SimplifiedType = rustc_type_ir::fast_reject::SimplifiedType<DefId>; pub type SimplifiedType = rustc_type_ir::fast_reject::SimplifiedType<DefId>;

View File

@ -168,9 +168,9 @@ pub fn for_each_relevant_impl(
// whose outer level is not a parameter or projection. Especially for things like // whose outer level is not a parameter or projection. Especially for things like
// `T: Clone` this is incredibly useful as we would otherwise look at all the impls // `T: Clone` this is incredibly useful as we would otherwise look at all the impls
// of `Clone` for `Option<T>`, `Vec<T>`, `ConcreteType` and so on. // of `Clone` for `Option<T>`, `Vec<T>`, `ConcreteType` and so on.
// Note that we're using `TreatParams::ForLookup` to query `non_blanket_impls` while using // Note that we're using `TreatParams::AsRigid` to query `non_blanket_impls` while using
// `TreatParams::AsCandidateKey` while actually adding them. // `TreatParams::InstantiateWithInfer` while actually adding them.
if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::ForLookup) { if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsRigid) {
if let Some(impls) = impls.non_blanket_impls.get(&simp) { if let Some(impls) = impls.non_blanket_impls.get(&simp) {
for &impl_def_id in impls { for &impl_def_id in impls {
f(impl_def_id); f(impl_def_id);
@ -190,7 +190,9 @@ pub fn non_blanket_impls_for_ty(
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
) -> impl Iterator<Item = DefId> + 'tcx { ) -> impl Iterator<Item = DefId> + 'tcx {
let impls = self.trait_impls_of(trait_def_id); let impls = self.trait_impls_of(trait_def_id);
if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsCandidateKey) { if let Some(simp) =
fast_reject::simplify_type(self, self_ty, TreatParams::InstantiateWithInfer)
{
if let Some(impls) = impls.non_blanket_impls.get(&simp) { if let Some(impls) = impls.non_blanket_impls.get(&simp) {
return impls.iter().copied(); return impls.iter().copied();
} }
@ -239,7 +241,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity(); let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity();
if let Some(simplified_self_ty) = if let Some(simplified_self_ty) =
fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::AsCandidateKey) fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::InstantiateWithInfer)
{ {
impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id); impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id);
} else { } else {

View File

@ -3,7 +3,7 @@
mod opaque_types; mod opaque_types;
mod weak_types; mod weak_types;
use rustc_type_ir::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_type_ir::fast_reject::DeepRejectCtxt;
use rustc_type_ir::inherent::*; use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem; use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::{self as ty, Interner, NormalizesTo, Upcast as _}; use rustc_type_ir::{self as ty, Interner, NormalizesTo, Upcast as _};
@ -106,6 +106,12 @@ fn probe_and_match_goal_against_assumption(
if let Some(projection_pred) = assumption.as_projection_clause() { if let Some(projection_pred) = assumption.as_projection_clause() {
if projection_pred.projection_def_id() == goal.predicate.def_id() { if projection_pred.projection_def_id() == goal.predicate.def_id() {
let cx = ecx.cx(); let cx = ecx.cx();
if !DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
goal.predicate.alias.args,
projection_pred.skip_binder().projection_term.args,
) {
return Err(NoSolution);
}
ecx.probe_trait_candidate(source).enter(|ecx| { ecx.probe_trait_candidate(source).enter(|ecx| {
let assumption_projection_pred = let assumption_projection_pred =
ecx.instantiate_binder_with_infer(projection_pred); ecx.instantiate_binder_with_infer(projection_pred);
@ -144,7 +150,7 @@ fn consider_impl_candidate(
let goal_trait_ref = goal.predicate.alias.trait_ref(cx); let goal_trait_ref = goal.predicate.alias.trait_ref(cx);
let impl_trait_ref = cx.impl_trait_ref(impl_def_id); let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::ForLookup).args_may_unify( if !DeepRejectCtxt::relate_rigid_infer(ecx.cx()).args_may_unify(
goal.predicate.alias.trait_ref(cx).args, goal.predicate.alias.trait_ref(cx).args,
impl_trait_ref.skip_binder().args, impl_trait_ref.skip_binder().args,
) { ) {

View File

@ -2,7 +2,7 @@
use rustc_ast_ir::Movability; use rustc_ast_ir::Movability;
use rustc_type_ir::data_structures::IndexSet; use rustc_type_ir::data_structures::IndexSet;
use rustc_type_ir::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_type_ir::fast_reject::DeepRejectCtxt;
use rustc_type_ir::inherent::*; use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem; use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::visit::TypeVisitableExt as _; use rustc_type_ir::visit::TypeVisitableExt as _;
@ -47,7 +47,7 @@ fn consider_impl_candidate(
let cx = ecx.cx(); let cx = ecx.cx();
let impl_trait_ref = cx.impl_trait_ref(impl_def_id); let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::ForLookup) if !DeepRejectCtxt::relate_rigid_infer(ecx.cx())
.args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args) .args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args)
{ {
return Err(NoSolution); return Err(NoSolution);
@ -124,6 +124,13 @@ fn probe_and_match_goal_against_assumption(
if trait_clause.def_id() == goal.predicate.def_id() if trait_clause.def_id() == goal.predicate.def_id()
&& trait_clause.polarity() == goal.predicate.polarity && trait_clause.polarity() == goal.predicate.polarity
{ {
if !DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
goal.predicate.trait_ref.args,
trait_clause.skip_binder().trait_ref.args,
) {
return Err(NoSolution);
}
ecx.probe_trait_candidate(source).enter(|ecx| { ecx.probe_trait_candidate(source).enter(|ecx| {
let assumption_trait_pred = ecx.instantiate_binder_with_infer(trait_clause); let assumption_trait_pred = ecx.instantiate_binder_with_infer(trait_clause);
ecx.eq( ecx.eq(

View File

@ -4,7 +4,7 @@
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_middle::traits::{ObligationCause, ObligationCauseCode}; use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::print::{FmtPrinter, Printer}; use rustc_middle::ty::print::{FmtPrinter, Printer};
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty}; use rustc_middle::ty::{self, suggest_constraining_type_param, Ty};
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
@ -316,7 +316,7 @@ fn foo(&self, x: T) -> T { x }
{ {
let mut has_matching_impl = false; let mut has_matching_impl = false;
tcx.for_each_relevant_impl(def_id, values.found, |did| { tcx.for_each_relevant_impl(def_id, values.found, |did| {
if DeepRejectCtxt::new(tcx, TreatParams::ForLookup) if DeepRejectCtxt::relate_rigid_infer(tcx)
.types_may_unify(values.found, tcx.type_of(did).skip_binder()) .types_may_unify(values.found, tcx.type_of(did).skip_binder())
{ {
has_matching_impl = true; has_matching_impl = true;
@ -337,7 +337,7 @@ fn foo(&self, x: T) -> T { x }
{ {
let mut has_matching_impl = false; let mut has_matching_impl = false;
tcx.for_each_relevant_impl(def_id, values.expected, |did| { tcx.for_each_relevant_impl(def_id, values.expected, |did| {
if DeepRejectCtxt::new(tcx, TreatParams::ForLookup) if DeepRejectCtxt::relate_rigid_infer(tcx)
.types_may_unify(values.expected, tcx.type_of(did).skip_binder()) .types_may_unify(values.expected, tcx.type_of(did).skip_binder())
{ {
has_matching_impl = true; has_matching_impl = true;
@ -357,7 +357,7 @@ fn foo(&self, x: T) -> T { x }
{ {
let mut has_matching_impl = false; let mut has_matching_impl = false;
tcx.for_each_relevant_impl(def_id, values.found, |did| { tcx.for_each_relevant_impl(def_id, values.found, |did| {
if DeepRejectCtxt::new(tcx, TreatParams::ForLookup) if DeepRejectCtxt::relate_rigid_infer(tcx)
.types_may_unify(values.found, tcx.type_of(did).skip_binder()) .types_may_unify(values.found, tcx.type_of(did).skip_binder())
{ {
has_matching_impl = true; has_matching_impl = true;

View File

@ -15,7 +15,7 @@
use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::query::NoSolution;
use rustc_middle::traits::solve::{CandidateSource, Certainty, Goal}; use rustc_middle::traits::solve::{CandidateSource, Certainty, Goal};
use rustc_middle::traits::specialization_graph::OverlapMode; use rustc_middle::traits::specialization_graph::OverlapMode;
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor}; use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
pub use rustc_next_trait_solver::coherence::*; pub use rustc_next_trait_solver::coherence::*;
@ -94,7 +94,7 @@ pub fn overlapping_impls(
// Before doing expensive operations like entering an inference context, do // Before doing expensive operations like entering an inference context, do
// a quick check via fast_reject to tell if the impl headers could possibly // a quick check via fast_reject to tell if the impl headers could possibly
// unify. // unify.
let drcx = DeepRejectCtxt::new(tcx, TreatParams::AsCandidateKey); let drcx = DeepRejectCtxt::relate_infer_infer(tcx);
let impl1_ref = tcx.impl_trait_ref(impl1_def_id); let impl1_ref = tcx.impl_trait_ref(impl1_def_id);
let impl2_ref = tcx.impl_trait_ref(impl2_def_id); let impl2_ref = tcx.impl_trait_ref(impl2_def_id);
let may_overlap = match (impl1_ref, impl2_ref) { let may_overlap = match (impl1_ref, impl2_ref) {

View File

@ -13,6 +13,7 @@
use rustc_middle::traits::select::OverflowError; use rustc_middle::traits::select::OverflowError;
pub use rustc_middle::traits::Reveal; pub use rustc_middle::traits::Reveal;
use rustc_middle::traits::{BuiltinImplSource, ImplSource, ImplSourceUserDefinedData}; use rustc_middle::traits::{BuiltinImplSource, ImplSource, ImplSourceUserDefinedData};
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::visit::{MaxUniverse, TypeVisitable, TypeVisitableExt}; use rustc_middle::ty::visit::{MaxUniverse, TypeVisitable, TypeVisitableExt};
use rustc_middle::ty::{self, Term, Ty, TyCtxt, Upcast}; use rustc_middle::ty::{self, Term, Ty, TyCtxt, Upcast};
@ -885,6 +886,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>(
potentially_unnormalized_candidates: bool, potentially_unnormalized_candidates: bool,
) { ) {
let infcx = selcx.infcx; let infcx = selcx.infcx;
let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
for predicate in env_predicates { for predicate in env_predicates {
let bound_predicate = predicate.kind(); let bound_predicate = predicate.kind();
if let ty::ClauseKind::Projection(data) = predicate.kind().skip_binder() { if let ty::ClauseKind::Projection(data) = predicate.kind().skip_binder() {
@ -893,6 +895,12 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>(
continue; continue;
} }
if !drcx
.args_may_unify(obligation.predicate.args, data.skip_binder().projection_term.args)
{
continue;
}
let is_match = infcx.probe(|_| { let is_match = infcx.probe(|_| {
selcx.match_projection_projections( selcx.match_projection_projections(
obligation, obligation,

View File

@ -13,7 +13,7 @@
use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_infer::traits::{Obligation, ObligationCause, PolyTraitObligation, SelectionError}; use rustc_infer::traits::{Obligation, ObligationCause, PolyTraitObligation, SelectionError};
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::{self, ToPolyTraitRef, Ty, TypeVisitableExt}; use rustc_middle::ty::{self, ToPolyTraitRef, Ty, TypeVisitableExt};
use rustc_middle::{bug, span_bug}; use rustc_middle::{bug, span_bug};
@ -247,11 +247,17 @@ fn assemble_candidates_from_caller_bounds<'o>(
.filter(|p| p.def_id() == stack.obligation.predicate.def_id()) .filter(|p| p.def_id() == stack.obligation.predicate.def_id())
.filter(|p| p.polarity() == stack.obligation.predicate.polarity()); .filter(|p| p.polarity() == stack.obligation.predicate.polarity());
let drcx = DeepRejectCtxt::relate_rigid_rigid(self.tcx());
let obligation_args = stack.obligation.predicate.skip_binder().trait_ref.args;
// Keep only those bounds which may apply, and propagate overflow if it occurs. // Keep only those bounds which may apply, and propagate overflow if it occurs.
for bound in bounds { for bound in bounds {
let bound_trait_ref = bound.map_bound(|t| t.trait_ref);
if !drcx.args_may_unify(obligation_args, bound_trait_ref.skip_binder().args) {
continue;
}
// FIXME(oli-obk): it is suspicious that we are dropping the constness and // FIXME(oli-obk): it is suspicious that we are dropping the constness and
// polarity here. // polarity here.
let wc = self.where_clause_may_apply(stack, bound.map_bound(|t| t.trait_ref))?; let wc = self.where_clause_may_apply(stack, bound_trait_ref)?;
if wc.may_apply() { if wc.may_apply() {
candidates.vec.push(ParamCandidate(bound)); candidates.vec.push(ParamCandidate(bound));
} }
@ -580,7 +586,7 @@ fn assemble_candidates_from_impls(
return; return;
} }
let drcx = DeepRejectCtxt::new(self.tcx(), TreatParams::ForLookup); let drcx = DeepRejectCtxt::relate_rigid_infer(self.tcx());
let obligation_args = obligation.predicate.skip_binder().trait_ref.args; let obligation_args = obligation.predicate.skip_binder().trait_ref.args;
self.tcx().for_each_relevant_impl( self.tcx().for_each_relevant_impl(
obligation.predicate.def_id(), obligation.predicate.def_id(),

View File

@ -40,7 +40,7 @@ impl<'tcx> Children {
fn insert_blindly(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId) { fn insert_blindly(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId) {
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder(); let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder();
if let Some(st) = if let Some(st) =
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey) fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::InstantiateWithInfer)
{ {
debug!("insert_blindly: impl_def_id={:?} st={:?}", impl_def_id, st); debug!("insert_blindly: impl_def_id={:?} st={:?}", impl_def_id, st);
self.non_blanket_impls.entry(st).or_default().push(impl_def_id) self.non_blanket_impls.entry(st).or_default().push(impl_def_id)
@ -57,7 +57,7 @@ fn remove_existing(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId) {
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder(); let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder();
let vec: &mut Vec<DefId>; let vec: &mut Vec<DefId>;
if let Some(st) = if let Some(st) =
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey) fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::InstantiateWithInfer)
{ {
debug!("remove_existing: impl_def_id={:?} st={:?}", impl_def_id, st); debug!("remove_existing: impl_def_id={:?} st={:?}", impl_def_id, st);
vec = self.non_blanket_impls.get_mut(&st).unwrap(); vec = self.non_blanket_impls.get_mut(&st).unwrap();
@ -278,7 +278,7 @@ fn insert(
let mut parent = trait_def_id; let mut parent = trait_def_id;
let mut last_lint = None; let mut last_lint = None;
let simplified = let simplified =
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey); fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::InstantiateWithInfer);
// Descend the specialization tree, where `parent` is the current parent node. // Descend the specialization tree, where `parent` is the current parent node.
loop { loop {

View File

@ -74,13 +74,13 @@ fn to_stable_hash_key(&self, hcx: &HCX) -> Fingerprint {
pub enum TreatParams { pub enum TreatParams {
/// Treat parameters as infer vars. This is the correct mode for caching /// Treat parameters as infer vars. This is the correct mode for caching
/// an impl's type for lookup. /// an impl's type for lookup.
AsCandidateKey, InstantiateWithInfer,
/// Treat parameters as placeholders in the given environment. This is the /// Treat parameters as placeholders in the given environment. This is the
/// correct mode for *lookup*, as during candidate selection. /// correct mode for *lookup*, as during candidate selection.
/// ///
/// This also treats projections with inference variables as infer vars /// This also treats projections with inference variables as infer vars
/// since they could be further normalized. /// since they could be further normalized.
ForLookup, AsRigid,
} }
/// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists. /// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
@ -140,18 +140,16 @@ pub fn simplify_type<I: Interner>(
} }
ty::Placeholder(..) => Some(SimplifiedType::Placeholder), ty::Placeholder(..) => Some(SimplifiedType::Placeholder),
ty::Param(_) => match treat_params { ty::Param(_) => match treat_params {
TreatParams::ForLookup => Some(SimplifiedType::Placeholder), TreatParams::AsRigid => Some(SimplifiedType::Placeholder),
TreatParams::AsCandidateKey => None, TreatParams::InstantiateWithInfer => None,
}, },
ty::Alias(..) => match treat_params { ty::Alias(..) => match treat_params {
// When treating `ty::Param` as a placeholder, projections also // When treating `ty::Param` as a placeholder, projections also
// don't unify with anything else as long as they are fully normalized. // don't unify with anything else as long as they are fully normalized.
// FIXME(-Znext-solver): Can remove this `if` and always simplify to `Placeholder` // FIXME(-Znext-solver): Can remove this `if` and always simplify to `Placeholder`
// when the new solver is enabled by default. // when the new solver is enabled by default.
TreatParams::ForLookup if !ty.has_non_region_infer() => { TreatParams::AsRigid if !ty.has_non_region_infer() => Some(SimplifiedType::Placeholder),
Some(SimplifiedType::Placeholder) TreatParams::AsRigid | TreatParams::InstantiateWithInfer => None,
}
TreatParams::ForLookup | TreatParams::AsCandidateKey => None,
}, },
ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id)), ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id)),
ty::Error(_) => Some(SimplifiedType::Error), ty::Error(_) => Some(SimplifiedType::Error),
@ -173,29 +171,49 @@ pub fn def(self) -> Option<DefId> {
} }
} }
/// Given generic arguments from an obligation and an impl, /// Given generic arguments, could they be unified after
/// could these two be unified after replacing parameters in the /// replacing parameters with inference variables or placeholders.
/// the impl with inference variables. /// This behavior is toggled using the const generics.
/// ///
/// For obligations, parameters won't be replaced by inference /// We use this to quickly reject impl/wc candidates without needing
/// variables and only unify with themselves. We treat them /// to instantiate generic arguments/having to enter a probe.
/// the same way we treat placeholders.
/// ///
/// We also use this function during coherence. For coherence the /// We also use this function during coherence. For coherence the
/// impls only have to overlap for some value, so we treat parameters /// impls only have to overlap for some value, so we treat parameters
/// on both sides like inference variables. This behavior is toggled /// on both sides like inference variables.
/// using the `treat_obligation_params` field.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct DeepRejectCtxt<I: Interner> { pub struct DeepRejectCtxt<
treat_obligation_params: TreatParams, I: Interner,
const INSTANTIATE_LHS_WITH_INFER: bool,
const INSTANTIATE_RHS_WITH_INFER: bool,
> {
_interner: PhantomData<I>, _interner: PhantomData<I>,
} }
impl<I: Interner> DeepRejectCtxt<I> { impl<I: Interner> DeepRejectCtxt<I, false, false> {
pub fn new(_interner: I, treat_obligation_params: TreatParams) -> Self { /// Treat parameters in both the lhs and the rhs as rigid.
DeepRejectCtxt { treat_obligation_params, _interner: PhantomData } pub fn relate_rigid_rigid(_interner: I) -> DeepRejectCtxt<I, false, false> {
DeepRejectCtxt { _interner: PhantomData }
} }
}
impl<I: Interner> DeepRejectCtxt<I, true, true> {
/// Treat parameters in both the lhs and the rhs as infer vars.
pub fn relate_infer_infer(_interner: I) -> DeepRejectCtxt<I, true, true> {
DeepRejectCtxt { _interner: PhantomData }
}
}
impl<I: Interner> DeepRejectCtxt<I, false, true> {
/// Treat parameters in the lhs as rigid, and in rhs as infer vars.
pub fn relate_rigid_infer(_interner: I) -> DeepRejectCtxt<I, false, true> {
DeepRejectCtxt { _interner: PhantomData }
}
}
impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_WITH_INFER: bool>
DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER, INSTANTIATE_RHS_WITH_INFER>
{
pub fn args_may_unify( pub fn args_may_unify(
self, self,
obligation_args: I::GenericArgs, obligation_args: I::GenericArgs,
@ -216,11 +234,18 @@ pub fn args_may_unify(
}) })
} }
pub fn types_may_unify(self, obligation_ty: I::Ty, impl_ty: I::Ty) -> bool { pub fn types_may_unify(self, lhs: I::Ty, rhs: I::Ty) -> bool {
match impl_ty.kind() { match rhs.kind() {
// Start by checking whether the type in the impl may unify with // Start by checking whether the `rhs` type may unify with
// pretty much everything. Just return `true` in that case. // pretty much everything. Just return `true` in that case.
ty::Param(_) | ty::Error(_) | ty::Alias(..) => return true, ty::Param(_) => {
if INSTANTIATE_RHS_WITH_INFER {
return true;
}
}
ty::Error(_) | ty::Alias(..) | ty::Bound(..) => return true,
ty::Infer(var) => return self.var_and_ty_may_unify(var, lhs),
// These types only unify with inference variables or their own // These types only unify with inference variables or their own
// variant. // variant.
ty::Bool ty::Bool
@ -238,108 +263,48 @@ pub fn types_may_unify(self, obligation_ty: I::Ty, impl_ty: I::Ty) -> bool {
| ty::Ref(..) | ty::Ref(..)
| ty::Never | ty::Never
| ty::Tuple(..) | ty::Tuple(..)
| ty::FnDef(..)
| ty::FnPtr(..) | ty::FnPtr(..)
| ty::Foreign(..) => debug_assert!(impl_ty.is_known_rigid()),
ty::FnDef(..)
| ty::Closure(..) | ty::Closure(..)
| ty::CoroutineClosure(..) | ty::CoroutineClosure(..)
| ty::Coroutine(..) | ty::Coroutine(..)
| ty::CoroutineWitness(..) | ty::CoroutineWitness(..)
| ty::Placeholder(..) | ty::Foreign(_)
| ty::Bound(..) | ty::Placeholder(_) => {}
| ty::Infer(_) => panic!("unexpected impl_ty: {impl_ty:?}"), };
}
let k = impl_ty.kind(); // For purely rigid types, use structural equivalence.
match obligation_ty.kind() { match lhs.kind() {
// Purely rigid types, use structural equivalence. ty::Ref(_, lhs_ty, lhs_mutbl) => match rhs.kind() {
ty::Bool ty::Ref(_, rhs_ty, rhs_mutbl) => {
| ty::Char lhs_mutbl == rhs_mutbl && self.types_may_unify(lhs_ty, rhs_ty)
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Str
| ty::Never
| ty::Foreign(_) => obligation_ty == impl_ty,
ty::Ref(_, obl_ty, obl_mutbl) => match k {
ty::Ref(_, impl_ty, impl_mutbl) => {
obl_mutbl == impl_mutbl && self.types_may_unify(obl_ty, impl_ty)
} }
_ => false, _ => false,
}, },
ty::Adt(obl_def, obl_args) => match k {
ty::Adt(impl_def, impl_args) => { ty::Adt(lhs_def, lhs_args) => match rhs.kind() {
obl_def == impl_def && self.args_may_unify(obl_args, impl_args) ty::Adt(rhs_def, rhs_args) => {
lhs_def == rhs_def && self.args_may_unify(lhs_args, rhs_args)
} }
_ => false, _ => false,
}, },
ty::Pat(obl_ty, _) => {
// FIXME(pattern_types): take pattern into account // Depending on the value of const generics, we either treat generic parameters
matches!(k, ty::Pat(impl_ty, _) if self.types_may_unify(obl_ty, impl_ty)) // like placeholders or like inference variables.
ty::Param(lhs) => {
INSTANTIATE_LHS_WITH_INFER
|| match rhs.kind() {
ty::Param(rhs) => lhs == rhs,
_ => false,
}
} }
ty::Slice(obl_ty) => {
matches!(k, ty::Slice(impl_ty) if self.types_may_unify(obl_ty, impl_ty)) // Placeholder types don't unify with anything on their own.
ty::Placeholder(lhs) => {
matches!(rhs.kind(), ty::Placeholder(rhs) if lhs == rhs)
} }
ty::Array(obl_ty, obl_len) => match k {
ty::Array(impl_ty, impl_len) => {
self.types_may_unify(obl_ty, impl_ty)
&& self.consts_may_unify(obl_len, impl_len)
}
_ => false,
},
ty::Tuple(obl) => match k {
ty::Tuple(imp) => {
obl.len() == imp.len()
&& iter::zip(obl.iter(), imp.iter())
.all(|(obl, imp)| self.types_may_unify(obl, imp))
}
_ => false,
},
ty::RawPtr(obl_ty, obl_mutbl) => match k {
ty::RawPtr(imp_ty, imp_mutbl) => {
obl_mutbl == imp_mutbl && self.types_may_unify(obl_ty, imp_ty)
}
_ => false,
},
ty::Dynamic(obl_preds, ..) => {
// Ideally we would walk the existential predicates here or at least
// compare their length. But considering that the relevant `Relate` impl
// actually sorts and deduplicates these, that doesn't work.
matches!(k, ty::Dynamic(impl_preds, ..) if
obl_preds.principal_def_id() == impl_preds.principal_def_id()
)
}
ty::FnPtr(obl_sig_tys, obl_hdr) => match k {
ty::FnPtr(impl_sig_tys, impl_hdr) => {
let obl_sig_tys = obl_sig_tys.skip_binder().inputs_and_output;
let impl_sig_tys = impl_sig_tys.skip_binder().inputs_and_output;
obl_hdr == impl_hdr ty::Infer(var) => self.var_and_ty_may_unify(var, rhs),
&& obl_sig_tys.len() == impl_sig_tys.len()
&& iter::zip(obl_sig_tys.iter(), impl_sig_tys.iter())
.all(|(obl, imp)| self.types_may_unify(obl, imp))
}
_ => false,
},
// Impls cannot contain these types as these cannot be named directly.
ty::FnDef(..) | ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..) => false,
// Placeholder types don't unify with anything on their own
ty::Placeholder(..) | ty::Bound(..) => false,
// Depending on the value of `treat_obligation_params`, we either
// treat generic parameters like placeholders or like inference variables.
ty::Param(_) => match self.treat_obligation_params {
TreatParams::ForLookup => false,
TreatParams::AsCandidateKey => true,
},
ty::Infer(ty::IntVar(_)) => impl_ty.is_integral(),
ty::Infer(ty::FloatVar(_)) => impl_ty.is_floating_point(),
ty::Infer(_) => true,
// As we're walking the whole type, it may encounter projections // As we're walking the whole type, it may encounter projections
// inside of binders and what not, so we're just going to assume that // inside of binders and what not, so we're just going to assume that
@ -348,49 +313,167 @@ pub fn types_may_unify(self, obligation_ty: I::Ty, impl_ty: I::Ty) -> bool {
// Looking forward to lazy normalization this is the safer strategy anyways. // Looking forward to lazy normalization this is the safer strategy anyways.
ty::Alias(..) => true, ty::Alias(..) => true,
ty::Error(_) => true, ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Str
| ty::Bool
| ty::Char
| ty::Never
| ty::Foreign(_) => lhs == rhs,
ty::CoroutineWitness(..) => { ty::Tuple(lhs) => match rhs.kind() {
panic!("unexpected obligation type: {:?}", obligation_ty) ty::Tuple(rhs) => {
lhs.len() == rhs.len()
&& iter::zip(lhs.iter(), rhs.iter())
.all(|(lhs, rhs)| self.types_may_unify(lhs, rhs))
}
_ => false,
},
ty::Array(lhs_ty, lhs_len) => match rhs.kind() {
ty::Array(rhs_ty, rhs_len) => {
self.types_may_unify(lhs_ty, rhs_ty) && self.consts_may_unify(lhs_len, rhs_len)
}
_ => false,
},
ty::RawPtr(lhs_ty, lhs_mutbl) => match rhs.kind() {
ty::RawPtr(rhs_ty, rhs_mutbl) => {
lhs_mutbl == rhs_mutbl && self.types_may_unify(lhs_ty, rhs_ty)
}
_ => false,
},
ty::Slice(lhs_ty) => {
matches!(rhs.kind(), ty::Slice(rhs_ty) if self.types_may_unify(lhs_ty, rhs_ty))
} }
ty::Dynamic(lhs_preds, ..) => {
// Ideally we would walk the existential predicates here or at least
// compare their length. But considering that the relevant `Relate` impl
// actually sorts and deduplicates these, that doesn't work.
matches!(rhs.kind(), ty::Dynamic(rhs_preds, ..) if
lhs_preds.principal_def_id() == rhs_preds.principal_def_id()
)
}
ty::FnPtr(lhs_sig_tys, lhs_hdr) => match rhs.kind() {
ty::FnPtr(rhs_sig_tys, rhs_hdr) => {
let lhs_sig_tys = lhs_sig_tys.skip_binder().inputs_and_output;
let rhs_sig_tys = rhs_sig_tys.skip_binder().inputs_and_output;
lhs_hdr == rhs_hdr
&& lhs_sig_tys.len() == rhs_sig_tys.len()
&& iter::zip(lhs_sig_tys.iter(), rhs_sig_tys.iter())
.all(|(lhs, rhs)| self.types_may_unify(lhs, rhs))
}
_ => false,
},
ty::Bound(..) => true,
ty::FnDef(lhs_def_id, lhs_args) => match rhs.kind() {
ty::FnDef(rhs_def_id, rhs_args) => {
lhs_def_id == rhs_def_id && self.args_may_unify(lhs_args, rhs_args)
}
_ => false,
},
ty::Closure(lhs_def_id, lhs_args) => match rhs.kind() {
ty::Closure(rhs_def_id, rhs_args) => {
lhs_def_id == rhs_def_id && self.args_may_unify(lhs_args, rhs_args)
}
_ => false,
},
ty::CoroutineClosure(lhs_def_id, lhs_args) => match rhs.kind() {
ty::CoroutineClosure(rhs_def_id, rhs_args) => {
lhs_def_id == rhs_def_id && self.args_may_unify(lhs_args, rhs_args)
}
_ => false,
},
ty::Coroutine(lhs_def_id, lhs_args) => match rhs.kind() {
ty::Coroutine(rhs_def_id, rhs_args) => {
lhs_def_id == rhs_def_id && self.args_may_unify(lhs_args, rhs_args)
}
_ => false,
},
ty::CoroutineWitness(lhs_def_id, lhs_args) => match rhs.kind() {
ty::CoroutineWitness(rhs_def_id, rhs_args) => {
lhs_def_id == rhs_def_id && self.args_may_unify(lhs_args, rhs_args)
}
_ => false,
},
ty::Pat(lhs_ty, _) => {
// FIXME(pattern_types): take pattern into account
matches!(rhs.kind(), ty::Pat(rhs_ty, _) if self.types_may_unify(lhs_ty, rhs_ty))
}
ty::Error(..) => true,
} }
} }
pub fn consts_may_unify(self, obligation_ct: I::Const, impl_ct: I::Const) -> bool { pub fn consts_may_unify(self, lhs: I::Const, rhs: I::Const) -> bool {
let impl_val = match impl_ct.kind() { match rhs.kind() {
ty::ConstKind::Param(_) => {
if INSTANTIATE_RHS_WITH_INFER {
return true;
}
}
ty::ConstKind::Expr(_) ty::ConstKind::Expr(_)
| ty::ConstKind::Param(_)
| ty::ConstKind::Unevaluated(_) | ty::ConstKind::Unevaluated(_)
| ty::ConstKind::Error(_) => { | ty::ConstKind::Error(_)
| ty::ConstKind::Infer(_)
| ty::ConstKind::Bound(..) => {
return true; return true;
} }
ty::ConstKind::Value(_, impl_val) => impl_val,
ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => { ty::ConstKind::Value(..) | ty::ConstKind::Placeholder(_) => {}
panic!("unexpected impl arg: {:?}", impl_ct)
}
}; };
match obligation_ct.kind() { match lhs.kind() {
ty::ConstKind::Param(_) => match self.treat_obligation_params { ty::ConstKind::Value(_, lhs_val) => match rhs.kind() {
TreatParams::ForLookup => false, ty::ConstKind::Value(_, rhs_val) => lhs_val == rhs_val,
TreatParams::AsCandidateKey => true, _ => false,
}, },
ty::ConstKind::Param(lhs) => {
INSTANTIATE_LHS_WITH_INFER
|| match rhs.kind() {
ty::ConstKind::Param(rhs) => lhs == rhs,
_ => false,
}
}
// Placeholder consts don't unify with anything on their own // Placeholder consts don't unify with anything on their own
ty::ConstKind::Placeholder(_) => false, ty::ConstKind::Placeholder(lhs) => {
matches!(rhs.kind(), ty::ConstKind::Placeholder(rhs) if lhs == rhs)
}
// As we don't necessarily eagerly evaluate constants, // As we don't necessarily eagerly evaluate constants,
// they might unify with any value. // they might unify with any value.
ty::ConstKind::Expr(_) | ty::ConstKind::Unevaluated(_) | ty::ConstKind::Error(_) => { ty::ConstKind::Expr(_) | ty::ConstKind::Unevaluated(_) | ty::ConstKind::Error(_) => {
true true
} }
ty::ConstKind::Value(_, obl_val) => obl_val == impl_val,
ty::ConstKind::Infer(_) => true, ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) => true,
}
}
ty::ConstKind::Bound(..) => { fn var_and_ty_may_unify(self, var: ty::InferTy, ty: I::Ty) -> bool {
panic!("unexpected obl const: {:?}", obligation_ct) if !ty.is_known_rigid() {
} return true;
}
match var {
ty::IntVar(_) => ty.is_integral(),
ty::FloatVar(_) => ty.is_floating_point(),
_ => true,
} }
} }
} }

View File

@ -29,7 +29,7 @@
use regex::Regex; use regex::Regex;
use rustc_data_structures::flock; use rustc_data_structures::flock;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
use rustc_span::Symbol; use rustc_span::Symbol;
@ -826,7 +826,7 @@ fn visit_item(&mut self, it: &Item) {
// Be aware of `tests/rustdoc/type-alias/deeply-nested-112515.rs` which might regress. // Be aware of `tests/rustdoc/type-alias/deeply-nested-112515.rs` which might regress.
let Some(impl_did) = impl_item_id.as_def_id() else { continue }; let Some(impl_did) = impl_item_id.as_def_id() else { continue };
let for_ty = self.cx.tcx().type_of(impl_did).skip_binder(); let for_ty = self.cx.tcx().type_of(impl_did).skip_binder();
let reject_cx = DeepRejectCtxt::new(self.cx.tcx(), TreatParams::AsCandidateKey); let reject_cx = DeepRejectCtxt::relate_infer_infer(self.cx.tcx());
if !reject_cx.types_may_unify(aliased_ty, for_ty) { if !reject_cx.types_may_unify(aliased_ty, for_ty) {
continue; continue;
} }