Auto merge of #109202 - compiler-errors:new-solver-fast-reject-faster-2, r=lcnr

Don't pass `TreatProjections` separately to `fast_reject`

Don't pass `TreatProjections` separately to `fast_reject`, and instead use the original approach of switching on two variants of `TreatParams` (undoes this: https://github.com/rust-lang/rust/pull/108830#pullrequestreview-1330371417).

Fixes the regression introduced in https://github.com/rust-lang/rust/pull/108830#issuecomment-1468116419
This commit is contained in:
bors 2023-03-23 23:53:56 +00:00
commit cf073ec2cb
7 changed files with 53 additions and 80 deletions

View File

@ -11,7 +11,7 @@
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams, TreatProjections}; use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams};
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt}; use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
@ -97,12 +97,7 @@ fn check_def_id(&mut self, impl_def_id: LocalDefId, self_ty: Ty<'tcx>, ty_def_id
} }
} }
if let Some(simp) = simplify_type( if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) {
self.tcx,
self_ty,
TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
) {
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);
@ -162,12 +157,7 @@ fn check_primitive_impl(&mut self, impl_def_id: LocalDefId, ty: Ty<'tcx>) {
} }
} }
if let Some(simp) = simplify_type( if let Some(simp) = simplify_type(self.tcx, ty, TreatParams::AsCandidateKey) {
self.tcx,
ty,
TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
) {
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

@ -16,7 +16,6 @@
use rustc_infer::infer::DefineOpaqueTypes; use rustc_infer::infer::DefineOpaqueTypes;
use rustc_infer::infer::{self, InferOk, TyCtxtInferExt}; use rustc_infer::infer::{self, InferOk, TyCtxtInferExt};
use rustc_middle::middle::stability; use rustc_middle::middle::stability;
use rustc_middle::ty::fast_reject::TreatProjections;
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams}; use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
use rustc_middle::ty::AssocItem; use rustc_middle::ty::AssocItem;
use rustc_middle::ty::GenericParamDefKind; use rustc_middle::ty::GenericParamDefKind;
@ -701,7 +700,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, TreatProjections::AsCandidateKey) else { let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) else {
bug!("unexpected incoherent type: {:?}", self_ty) bug!("unexpected incoherent type: {:?}", self_ty)
}; };
for &impl_def_id in self.tcx.incoherent_impls(simp) { for &impl_def_id in self.tcx.incoherent_impls(simp) {

View File

@ -25,7 +25,6 @@
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::traits::util::supertraits; use rustc_middle::traits::util::supertraits;
use rustc_middle::ty::fast_reject::DeepRejectCtxt; use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::fast_reject::TreatProjections;
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams}; use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths}; use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths};
use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeVisitableExt};
@ -1524,7 +1523,7 @@ fn suggest_constraining_numerical_ty(
.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, TreatProjections::AsCandidateKey) simplify_type(tcx, ty, TreatParams::AsCandidateKey)
.and_then(|simp| { .and_then(|simp| {
tcx.incoherent_impls(simp) tcx.incoherent_impls(simp)
.iter() .iter()
@ -2653,12 +2652,9 @@ enum Introducer {
// FIXME: Even though negative bounds are not implemented, we could maybe handle // FIXME: Even though negative bounds are not implemented, we could maybe handle
// 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) = simplify_type( } else if let Some(simp_rcvr_ty) =
self.tcx, simplify_type(self.tcx, rcvr_ty, TreatParams::ForLookup)
rcvr_ty, {
TreatParams::ForLookup,
TreatProjections::ForLookup,
) {
let mut potential_candidates = Vec::new(); let mut potential_candidates = Vec::new();
let mut explicitly_negative = Vec::new(); let mut explicitly_negative = Vec::new();
for candidate in candidates { for candidate in candidates {
@ -2671,12 +2667,8 @@ enum Introducer {
}) })
.any(|imp_did| { .any(|imp_did| {
let imp = self.tcx.impl_trait_ref(imp_did).unwrap().subst_identity(); let imp = self.tcx.impl_trait_ref(imp_did).unwrap().subst_identity();
let imp_simp = simplify_type( let imp_simp =
self.tcx, simplify_type(self.tcx, imp.self_ty(), TreatParams::ForLookup);
imp.self_ty(),
TreatParams::ForLookup,
TreatProjections::ForLookup,
);
imp_simp.map_or(false, |s| s == simp_rcvr_ty) imp_simp.map_or(false, |s| s == simp_rcvr_ty)
}) })
{ {

View File

@ -27,7 +27,7 @@
use rustc_middle::query::LocalCrate; use rustc_middle::query::LocalCrate;
use rustc_middle::traits::specialization_graph; use rustc_middle::traits::specialization_graph;
use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams, TreatProjections}; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
use rustc_middle::util::common::to_readable_str; use rustc_middle::util::common::to_readable_str;
@ -1881,7 +1881,6 @@ fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
self.tcx, self.tcx,
trait_ref.self_ty(), trait_ref.self_ty(),
TreatParams::AsCandidateKey, TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
); );
fx_hash_map fx_hash_map

View File

@ -56,7 +56,15 @@ pub enum TreatParams {
AsCandidateKey, AsCandidateKey,
/// 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
/// since they could be further normalized.
ForLookup, ForLookup,
/// Treat parameters as placeholders in the given environment. This is the
/// correct mode for *lookup*, as during candidate selection.
///
/// N.B. during deep rejection, this acts identically to `ForLookup`.
NextSolverLookup,
} }
/// During fast-rejection, we have the choice of treating projection types /// During fast-rejection, we have the choice of treating projection types
@ -64,13 +72,6 @@ pub enum TreatParams {
/// to be normalized/rigid. /// to be normalized/rigid.
#[derive(PartialEq, Eq, Debug, Clone, Copy)] #[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum TreatProjections { pub enum TreatProjections {
/// In candidates, we may be able to normalize the projection
/// after instantiating the candidate and equating it with a goal.
///
/// We must assume that the `impl<T> Trait<T> for <T as Id>::This`
/// can apply to all self types so we don't return a simplified type
/// for `<T as Id>::This`.
AsCandidateKey,
/// In the old solver we don't try to normalize projections /// In the old solver we don't try to normalize projections
/// when looking up impls and only access them by using the /// when looking up impls and only access them by using the
/// current self type. This means that if the self type is /// current self type. This means that if the self type is
@ -107,7 +108,6 @@ pub fn simplify_type<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
treat_params: TreatParams, treat_params: TreatParams,
treat_projections: TreatProjections,
) -> Option<SimplifiedType> { ) -> Option<SimplifiedType> {
match *ty.kind() { match *ty.kind() {
ty::Bool => Some(BoolSimplifiedType), ty::Bool => Some(BoolSimplifiedType),
@ -136,13 +136,20 @@ pub fn simplify_type<'tcx>(
ty::FnPtr(f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())), ty::FnPtr(f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())),
ty::Placeholder(..) => Some(PlaceholderSimplifiedType), ty::Placeholder(..) => Some(PlaceholderSimplifiedType),
ty::Param(_) => match treat_params { ty::Param(_) => match treat_params {
TreatParams::ForLookup => Some(PlaceholderSimplifiedType), TreatParams::ForLookup | TreatParams::NextSolverLookup => {
Some(PlaceholderSimplifiedType)
}
TreatParams::AsCandidateKey => None, TreatParams::AsCandidateKey => None,
}, },
ty::Alias(..) => match treat_projections { ty::Alias(..) => match treat_params {
TreatProjections::ForLookup if !ty.needs_infer() => Some(PlaceholderSimplifiedType), // When treating `ty::Param` as a placeholder, projections also
TreatProjections::NextSolverLookup => Some(PlaceholderSimplifiedType), // don't unify with anything else as long as they are fully normalized.
TreatProjections::AsCandidateKey | TreatProjections::ForLookup => None, //
// We will have to be careful with lazy normalization here.
// FIXME(lazy_normalization): This is probably not right...
TreatParams::ForLookup if !ty.has_non_region_infer() => Some(PlaceholderSimplifiedType),
TreatParams::NextSolverLookup => Some(PlaceholderSimplifiedType),
TreatParams::ForLookup | TreatParams::AsCandidateKey => None,
}, },
ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)), ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None, ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None,
@ -310,7 +317,7 @@ pub fn types_may_unify<'tcx>(self, obligation_ty: Ty<'tcx>, impl_ty: Ty<'tcx>) -
// Depending on the value of `treat_obligation_params`, we either // Depending on the value of `treat_obligation_params`, we either
// treat generic parameters like placeholders or like inference variables. // treat generic parameters like placeholders or like inference variables.
ty::Param(_) => match self.treat_obligation_params { ty::Param(_) => match self.treat_obligation_params {
TreatParams::ForLookup => false, TreatParams::ForLookup | TreatParams::NextSolverLookup => false,
TreatParams::AsCandidateKey => true, TreatParams::AsCandidateKey => true,
}, },
@ -348,7 +355,7 @@ pub fn consts_may_unify(self, obligation_ct: ty::Const<'_>, impl_ct: ty::Const<'
let k = impl_ct.kind(); let k = impl_ct.kind();
match obligation_ct.kind() { match obligation_ct.kind() {
ty::ConstKind::Param(_) => match self.treat_obligation_params { ty::ConstKind::Param(_) => match self.treat_obligation_params {
TreatParams::ForLookup => false, TreatParams::ForLookup | TreatParams::NextSolverLookup => false,
TreatParams::AsCandidateKey => true, TreatParams::AsCandidateKey => true,
}, },

View File

@ -153,12 +153,7 @@ 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( if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsCandidateKey) {
self,
self_ty,
TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
) {
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();
} }
@ -191,13 +186,17 @@ pub fn find_map_relevant_impl<T>(
} }
} }
// Note that we're using `TreatParams::ForLookup` to query `non_blanket_impls` while using
// `TreatParams::AsCandidateKey` while actually adding them.
let treat_params = match treat_projections {
TreatProjections::NextSolverLookup => TreatParams::NextSolverLookup,
TreatProjections::ForLookup => TreatParams::ForLookup,
};
// This way, when searching for some impl for `T: Trait`, we do not look at any impls // This way, when searching for some impl for `T: Trait`, we do not look at any impls
// 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.
if let Some(simp) = if let Some(simp) = fast_reject::simplify_type(self, self_ty, treat_params) {
fast_reject::simplify_type(self, self_ty, TreatParams::ForLookup, treat_projections)
{
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 {
if let result @ Some(_) = f(impl_def_id) { if let result @ Some(_) = f(impl_def_id) {
@ -258,12 +257,9 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
continue; continue;
} }
if let Some(simplified_self_ty) = fast_reject::simplify_type( if let Some(simplified_self_ty) =
tcx, fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::AsCandidateKey)
impl_self_ty, {
TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
) {
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 {
impls.blanket_impls.push(impl_def_id); impls.blanket_impls.push(impl_def_id);

View File

@ -3,7 +3,7 @@
use crate::traits; use crate::traits;
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams, TreatProjections}; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
pub use rustc_middle::traits::specialization_graph::*; pub use rustc_middle::traits::specialization_graph::*;
@ -49,12 +49,9 @@ impl<'tcx> ChildrenExt<'tcx> for Children {
/// Insert an impl into this set of children without comparing to any existing impls. /// Insert an impl into this set of children without comparing to any existing impls.
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) = fast_reject::simplify_type( if let Some(st) =
tcx, fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey)
trait_ref.self_ty(), {
TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
) {
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)
} else { } else {
@ -69,12 +66,9 @@ fn insert_blindly(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId) {
fn remove_existing(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId) { 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) = fast_reject::simplify_type( if let Some(st) =
tcx, fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey)
trait_ref.self_ty(), {
TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
) {
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();
} else { } else {
@ -310,12 +304,8 @@ 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 = fast_reject::simplify_type( let simplified =
tcx, fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey);
trait_ref.self_ty(),
TreatParams::AsCandidateKey,
TreatProjections::AsCandidateKey,
);
// Descend the specialization tree, where `parent` is the current parent node. // Descend the specialization tree, where `parent` is the current parent node.
loop { loop {