diff --git a/compiler/rustc_infer/src/infer/relate/generalize.rs b/compiler/rustc_infer/src/infer/relate/generalize.rs index ee6e13250e709..12c577d3a50e2 100644 --- a/compiler/rustc_infer/src/infer/relate/generalize.rs +++ b/compiler/rustc_infer/src/infer/relate/generalize.rs @@ -5,10 +5,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_hir::def_id::DefId; use rustc_middle::bug; use rustc_middle::ty::error::TypeError; -use rustc_middle::ty::{ - self, InferConst, Term, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, - TypeVisitor, -}; +use rustc_middle::ty::{self, InferConst, Term, Ty, TyCtxt, TypeVisitableExt}; use rustc_span::Span; use tracing::{debug, instrument, warn}; @@ -362,45 +359,6 @@ impl<'tcx> InferCtxt<'tcx> { } } -/// Finds the max universe present -struct MaxUniverse { - max_universe: ty::UniverseIndex, -} - -impl MaxUniverse { - fn new() -> Self { - MaxUniverse { max_universe: ty::UniverseIndex::ROOT } - } - - fn max_universe(self) -> ty::UniverseIndex { - self.max_universe - } -} - -impl<'tcx> TypeVisitor> for MaxUniverse { - fn visit_ty(&mut self, t: Ty<'tcx>) { - if let ty::Placeholder(placeholder) = t.kind() { - self.max_universe = self.max_universe.max(placeholder.universe); - } - - t.super_visit_with(self) - } - - fn visit_const(&mut self, c: ty::Const<'tcx>) { - if let ty::ConstKind::Placeholder(placeholder) = c.kind() { - self.max_universe = self.max_universe.max(placeholder.universe); - } - - c.super_visit_with(self) - } - - fn visit_region(&mut self, r: ty::Region<'tcx>) { - if let ty::RePlaceholder(placeholder) = r.kind() { - self.max_universe = self.max_universe.max(placeholder.universe); - } - } -} - /// The "generalizer" is used when handling inference variables. /// /// The basic strategy for handling a constraint like `?A <: B` is to @@ -511,10 +469,9 @@ impl<'tcx> Generalizer<'_, 'tcx> { if is_nested_alias { return Err(e); } else { - let mut visitor = MaxUniverse::new(); - alias.visit_with(&mut visitor); + let alias_max_universe = ty::max_universe_of_placeholders(self.infcx, alias); let infer_replacement_is_complete = - self.for_universe.can_name(visitor.max_universe()) + self.for_universe.can_name(alias_max_universe) && !alias.has_escaping_bound_vars(); if !infer_replacement_is_complete { warn!("may incompletely handle alias type: {alias:?}"); diff --git a/compiler/rustc_next_trait_solver/src/normalize.rs b/compiler/rustc_next_trait_solver/src/normalize.rs index 761b141202d98..959e18071bb57 100644 --- a/compiler/rustc_next_trait_solver/src/normalize.rs +++ b/compiler/rustc_next_trait_solver/src/normalize.rs @@ -3,9 +3,8 @@ use std::fmt::Debug; use rustc_type_ir::data_structures::ensure_sufficient_stack; use rustc_type_ir::inherent::*; use rustc_type_ir::{ - self as ty, AliasTerm, Binder, FallibleTypeFolder, InferConst, InferCtxtLike, InferTy, - Interner, TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, - TypeVisitor, UniverseIndex, + self as ty, AliasTerm, Binder, FallibleTypeFolder, InferCtxtLike, Interner, TypeFoldable, + TypeSuperFoldable, TypeVisitableExt, UniverseIndex, }; use tracing::instrument; @@ -37,72 +36,6 @@ pub enum NormalizationWasAmbiguous { No, } -/// Finds the max universe present in infer vars. -struct MaxUniverse<'a, Infcx, I> -where - Infcx: InferCtxtLike, - I: Interner, -{ - infcx: &'a Infcx, - max_universe: ty::UniverseIndex, -} - -impl<'a, Infcx, I> MaxUniverse<'a, Infcx, I> -where - Infcx: InferCtxtLike, - I: Interner, -{ - fn new(infcx: &'a Infcx) -> Self { - MaxUniverse { infcx, max_universe: ty::UniverseIndex::ROOT } - } - - fn max_universe(self) -> ty::UniverseIndex { - self.max_universe - } -} - -impl<'a, Infcx, I> TypeVisitor for MaxUniverse<'a, Infcx, I> -where - Infcx: InferCtxtLike, - I: Interner, -{ - type Result = (); - - fn visit_ty(&mut self, t: I::Ty) { - if !t.has_infer() { - return; - } - - if let ty::Infer(InferTy::TyVar(vid)) = t.kind() { - // We shallow resolved the infer var before. - // So it should be a unresolved infer var with an universe. - self.max_universe = self.max_universe.max(self.infcx.universe_of_ty(vid).unwrap()); - } - - t.super_visit_with(self) - } - - fn visit_const(&mut self, c: I::Const) { - if !c.has_infer() { - return; - } - - if let ty::ConstKind::Infer(InferConst::Var(vid)) = c.kind() { - // We shallow resolved the infer var before. - // So it should be a unresolved infer var with an universe. - self.max_universe = self.max_universe.max(self.infcx.universe_of_ct(vid).unwrap()); - } - - c.super_visit_with(self) - } - - fn visit_region(&mut self, r: I::Region) { - if let ty::ReVar(vid) = r.kind() { - self.max_universe = self.max_universe.max(self.infcx.universe_of_lt(vid).unwrap()); - } - } -} - impl<'a, Infcx, I, F, E> NormalizationFolder<'a, Infcx, I, F> where Infcx: InferCtxtLike, @@ -130,9 +63,7 @@ where if normalization_was_ambiguous == NormalizationWasAmbiguous::Yes && has_escaping == HasEscapingBoundVars::Yes { - let mut visitor = MaxUniverse::new(self.infcx); - normalized.visit_with(&mut visitor); - let max_universe = visitor.max_universe(); + let max_universe = ty::max_universe_of_infer_vars(self.infcx, normalized); if max_universe.can_name(self.universes.first().unwrap().unwrap()) { return Ok(None); } diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/solver_region_constraints.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/solver_region_constraints.rs index aa92fc163f0ed..a232ab9192444 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/solver_region_constraints.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/solver_region_constraints.rs @@ -8,11 +8,11 @@ use rustc_type_ir::outlives::{Component, push_outlives_components}; #[cfg(not(feature = "nightly"))] use rustc_type_ir::region_constraint::TransitiveRelationBuilder; use rustc_type_ir::region_constraint::{ - Assumptions, RegionConstraint, eagerly_handle_placeholders_in_universe, max_universe, + Assumptions, RegionConstraint, eagerly_handle_placeholders_in_universe, }; use rustc_type_ir::{ AliasTy, Binder, ClauseKind, InferCtxtLike, Interner, OutlivesPredicate, TypeVisitable, - TypeVisitableExt, TypeVisitor, UniverseIndex, + TypeVisitableExt, TypeVisitor, UniverseIndex, max_universe, }; use tracing::{debug, instrument}; diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 0fdfe17b963fa..bef53e422b0d2 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -56,6 +56,7 @@ mod term_kind; mod ty; mod ty_info; mod ty_kind; +mod universe; mod unnormalized; mod upcast; mod visit; @@ -86,6 +87,7 @@ pub use term_kind::*; pub use ty::{Alias, *}; pub use ty_info::*; pub use ty_kind::*; +pub use universe::*; pub use unnormalized::Unnormalized; pub use upcast::*; pub use visit::*; diff --git a/compiler/rustc_type_ir/src/region_constraint.rs b/compiler/rustc_type_ir/src/region_constraint.rs index 626649027da28..e77e0b9e24d52 100644 --- a/compiler/rustc_type_ir/src/region_constraint.rs +++ b/compiler/rustc_type_ir/src/region_constraint.rs @@ -49,12 +49,11 @@ use crate::data_structures::IndexMap; use crate::fold::TypeSuperFoldable; use crate::inherent::*; use crate::relate::{Relate, RelateResult, TypeRelation, VarianceDiagInfo}; -use crate::visit::TypeSuperVisitable; use crate::{ - AliasTy, Binder, BoundRegion, BoundVar, BoundVariableKind, ConstKind, DebruijnIndex, - FallibleTypeFolder, InferCtxtLike, InferTy, Interner, IsRigid, OutlivesPredicate, RegionKind, - TyKind, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor, TypingMode, UniverseIndex, - Variance, VisitorResult, set_aliases_to_non_rigid, + AliasTy, Binder, BoundRegion, BoundVar, BoundVariableKind, DebruijnIndex, FallibleTypeFolder, + InferCtxtLike, Interner, IsRigid, OutlivesPredicate, RegionKind, TyKind, TypeFoldable, + TypeFolder, TypeVisitable, TypeVisitor, TypingMode, UniverseIndex, Variance, VisitorResult, + max_universe, set_aliases_to_non_rigid, }; #[derive_where(Clone, Debug; I: Interner)] @@ -990,84 +989,6 @@ pub fn regions_outlived_by_placeholder( }) } -/// The largest universe a variable or placeholder was from in `t` -pub fn max_universe, I: Interner, T: TypeFoldable>( - infcx: &Infcx, - t: T, -) -> UniverseIndex { - let mut visitor = MaxUniverse::new(infcx); - // `max_universe` is also used while rewriting constraints to lower universes, - // so do not rely on callers having already resolved non-region infer vars. - let t = infcx.resolve_vars_if_possible(t); - t.visit_with(&mut visitor); - visitor.max_universe() -} - -// FIXME(-Zassumptions-on-binders): Share this with the visitor used by generalization. We currently don't -// as generalization does not look at universes of inference variables but we do -struct MaxUniverse<'a, Infcx: InferCtxtLike> { - max_universe: UniverseIndex, - infcx: &'a Infcx, -} - -impl<'a, Infcx: InferCtxtLike> MaxUniverse<'a, Infcx> { - fn new(infcx: &'a Infcx) -> Self { - MaxUniverse { infcx, max_universe: UniverseIndex::ROOT } - } - - fn max_universe(self) -> UniverseIndex { - self.max_universe - } -} - -impl<'a, Infcx: InferCtxtLike, I: Interner> TypeVisitor - for MaxUniverse<'a, Infcx> -{ - type Result = (); - - fn visit_ty(&mut self, t: I::Ty) { - match t.kind() { - TyKind::Placeholder(p) => self.max_universe = self.max_universe.max(p.universe), - TyKind::Infer(InferTy::TyVar(inf)) => { - let u = self.infcx.universe_of_ty(inf).unwrap(); - debug!("var {inf:?} in universe {u:?}"); - self.max_universe = self.max_universe.max(u); - } - _ => t.super_visit_with(self), - } - } - - fn visit_const(&mut self, c: I::Const) { - match c.kind() { - ConstKind::Placeholder(p) => self.max_universe = self.max_universe.max(p.universe), - ConstKind::Infer(rustc_type_ir::InferConst::Var(inf)) => { - let u = self.infcx.universe_of_ct(inf).unwrap(); - debug!("var {inf:?} in universe {u:?}"); - self.max_universe = self.max_universe.max(u); - } - _ => c.super_visit_with(self), - } - } - - fn visit_region(&mut self, r: I::Region) { - match r.kind() { - RegionKind::RePlaceholder(p) => self.max_universe = self.max_universe.max(p.universe), - RegionKind::ReVar(var) => match self.infcx.opportunistic_resolve_lt_var(var).kind() { - RegionKind::RePlaceholder(p) => { - self.max_universe = self.max_universe.max(p.universe) - } - RegionKind::ReVar(var) => { - let u = self.infcx.universe_of_lt(var).unwrap(); - debug!("var {var:?} in universe {u:?}"); - self.max_universe = self.max_universe.max(u); - } - _ => (), - }, - _ => (), - } - } -} - pub struct PlaceholderReplacer { cx: I, existing_var_count: usize, diff --git a/compiler/rustc_type_ir/src/universe.rs b/compiler/rustc_type_ir/src/universe.rs new file mode 100644 index 0000000000000..9b0990d4d0ff0 --- /dev/null +++ b/compiler/rustc_type_ir/src/universe.rs @@ -0,0 +1,154 @@ +use tracing::{debug, instrument}; + +use crate::inherent::*; +use crate::visit::TypeVisitableExt; +use crate::{ + ConstKind, InferCtxtLike, InferTy, Interner, RegionKind, TyKind, TypeFoldable, + TypeSuperVisitable, TypeVisitable, TypeVisitor, UniverseIndex, +}; + +/// The largest universe a variable or placeholder was from in `t` +pub fn max_universe, I: Interner, T: TypeFoldable>( + infcx: &Infcx, + t: T, +) -> UniverseIndex { + max_universe_inner::<_, _, _, true, true>(infcx, t) +} + +/// The largest universe a variable was from in `t` +pub fn max_universe_of_infer_vars< + Infcx: InferCtxtLike, + I: Interner, + T: TypeFoldable, +>( + infcx: &Infcx, + t: T, +) -> UniverseIndex { + max_universe_inner::<_, _, _, false, true>(infcx, t) +} + +/// The largest universe a placeholder was from in `t` +pub fn max_universe_of_placeholders< + Infcx: InferCtxtLike, + I: Interner, + T: TypeFoldable, +>( + infcx: &Infcx, + t: T, +) -> UniverseIndex { + max_universe_inner::<_, _, _, true, false>(infcx, t) +} + +fn max_universe_inner< + Infcx: InferCtxtLike, + I: Interner, + T: TypeFoldable, + const VISIT_PLACEHOLDER: bool, + const VISIT_INFER: bool, +>( + infcx: &Infcx, + t: T, +) -> UniverseIndex { + if !MaxUniverse::::needs_visit(&t) { + return UniverseIndex::ROOT; + } + + let mut visitor = MaxUniverse::<_, VISIT_PLACEHOLDER, VISIT_INFER>::new(infcx); + // FIXME: make this a debug_assert and let callers resolve vars if there's + // perf win here. + let t = infcx.resolve_vars_if_possible(t); + t.visit_with(&mut visitor); + visitor.max_universe() +} + +struct MaxUniverse<'a, Infcx: InferCtxtLike, const VISIT_PLACEHOLDER: bool, const VISIT_INFER: bool> +{ + max_universe: UniverseIndex, + infcx: &'a Infcx, +} + +impl<'a, Infcx: InferCtxtLike, const VISIT_PLACEHOLDER: bool, const VISIT_INFER: bool> + MaxUniverse<'a, Infcx, VISIT_PLACEHOLDER, VISIT_INFER> +{ + fn new(infcx: &'a Infcx) -> Self { + MaxUniverse { infcx, max_universe: UniverseIndex::ROOT } + } + + fn max_universe(self) -> UniverseIndex { + self.max_universe + } + + #[instrument(ret, level = "debug")] + fn needs_visit, I: Interner>(t: &T) -> bool { + (VISIT_PLACEHOLDER && t.has_placeholders()) || (VISIT_INFER && t.has_infer()) + } +} + +impl< + 'a, + Infcx: InferCtxtLike, + I: Interner, + const VISIT_PLACEHOLDER: bool, + const VISIT_INFER: bool, +> TypeVisitor for MaxUniverse<'a, Infcx, VISIT_PLACEHOLDER, VISIT_INFER> +{ + type Result = (); + + fn visit_ty(&mut self, t: I::Ty) { + if !Self::needs_visit(&t) { + return; + } + + match t.kind() { + TyKind::Placeholder(p) if VISIT_PLACEHOLDER => { + self.max_universe = self.max_universe.max(p.universe) + } + TyKind::Infer(InferTy::TyVar(inf)) if VISIT_INFER => { + let u = self.infcx.universe_of_ty(inf).unwrap(); + debug!("var {inf:?} in universe {u:?}"); + self.max_universe = self.max_universe.max(u); + } + _ => t.super_visit_with(self), + } + } + + fn visit_const(&mut self, c: I::Const) { + if !Self::needs_visit(&c) { + return; + } + + match c.kind() { + ConstKind::Placeholder(p) if VISIT_PLACEHOLDER => { + self.max_universe = self.max_universe.max(p.universe) + } + ConstKind::Infer(rustc_type_ir::InferConst::Var(inf)) if VISIT_INFER => { + let u = self.infcx.universe_of_ct(inf).unwrap(); + debug!("var {inf:?} in universe {u:?}"); + self.max_universe = self.max_universe.max(u); + } + _ => c.super_visit_with(self), + } + } + + fn visit_region(&mut self, r: I::Region) { + match r.kind() { + RegionKind::RePlaceholder(p) if VISIT_PLACEHOLDER => { + self.max_universe = self.max_universe.max(p.universe) + } + RegionKind::ReVar(var) if VISIT_INFER => { + match self.infcx.opportunistic_resolve_lt_var(var).kind() { + RegionKind::RePlaceholder(p) if VISIT_PLACEHOLDER => { + self.max_universe = self.max_universe.max(p.universe) + } + RegionKind::ReVar(var) if VISIT_INFER => { + let u = self.infcx.universe_of_lt(var).unwrap(); + debug!("var {var:?} in universe {u:?}"); + self.max_universe = self.max_universe.max(u); + } + _ => (), + } + } + _ => (), + } + } +}