diff --git a/compiler/rustc_borrowck/src/borrow_set.rs b/compiler/rustc_borrowck/src/borrow_set.rs index 4644c210137fe..0879eba87196b 100644 --- a/compiler/rustc_borrowck/src/borrow_set.rs +++ b/compiler/rustc_borrowck/src/borrow_set.rs @@ -6,7 +6,7 @@ use rustc_index::bit_set::DenseBitSet; use rustc_middle::mir::visit::{MutatingUseContext, NonUseContext, PlaceContext, Visitor}; use rustc_middle::mir::{self, Body, Local, Location, traversal}; use rustc_middle::span_bug; -use rustc_middle::ty::{RegionVid, TyCtxt}; +use rustc_middle::ty::{RegionUtilitiesExt, RegionVid, TyCtxt}; use rustc_mir_dataflow::move_paths::MoveData; use tracing::debug; diff --git a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs index 538c5887118e6..1559186457073 100644 --- a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs @@ -14,7 +14,8 @@ use rustc_infer::traits::query::{ }; use rustc_middle::ty::error::TypeError; use rustc_middle::ty::{ - self, RePlaceholder, Region, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex, Unnormalized, + self, RePlaceholder, Region, RegionExt, RegionUtilitiesExt, RegionVid, Ty, TyCtxt, + TypeFoldable, UniverseIndex, Unnormalized, }; use rustc_span::Span; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 2735c800c4a38..8c60232297e1c 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -25,7 +25,7 @@ use rustc_middle::mir::{ }; use rustc_middle::ty::print::PrintTraitRefExt as _; use rustc_middle::ty::{ - self, PredicateKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor, Upcast, + self, PredicateKind, RegionUtilitiesExt, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor, Upcast, suggest_constraining_type_params, }; use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex}; diff --git a/compiler/rustc_borrowck/src/diagnostics/find_use.rs b/compiler/rustc_borrowck/src/diagnostics/find_use.rs index 96f48840468e5..ba244700313aa 100644 --- a/compiler/rustc_borrowck/src/diagnostics/find_use.rs +++ b/compiler/rustc_borrowck/src/diagnostics/find_use.rs @@ -3,7 +3,7 @@ use std::collections::VecDeque; use rustc_data_structures::fx::FxIndexSet; use rustc_middle::mir::visit::{PlaceContext, Visitor}; use rustc_middle::mir::{self, Body, Local, Location}; -use rustc_middle::ty::{RegionVid, TyCtxt}; +use rustc_middle::ty::{RegionUtilitiesExt, RegionVid, TyCtxt}; use crate::def_use::{self, DefUse}; use crate::region_infer::{Cause, RegionInferenceContext}; diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index 4e5f4408ca31a..d074e7a18469e 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -15,8 +15,8 @@ use rustc_middle::bug; use rustc_middle::hir::place::PlaceBase; use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint}; use rustc_middle::ty::{ - self, FnSigKind, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitor, - fold_regions, + self, FnSigKind, GenericArgs, Region, RegionUtilitiesExt, RegionVid, Ty, TyCtxt, TypeFoldable, + TypeVisitor, fold_regions, }; use rustc_span::{Ident, Span, kw}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index c82d114f91857..5ef675547021d 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -6,7 +6,9 @@ use rustc_errors::{Diag, EmissionGuarantee}; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_middle::ty::print::RegionHighlightMode; -use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, RegionVid, Ty, Unnormalized}; +use rustc_middle::ty::{ + self, GenericArgKind, GenericArgsRef, RegionUtilitiesExt, RegionVid, Ty, Unnormalized, +}; use rustc_middle::{bug, span_bug}; use rustc_span::{DUMMY_SP, Span, Symbol, kw, sym}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; diff --git a/compiler/rustc_borrowck/src/diagnostics/var_name.rs b/compiler/rustc_borrowck/src/diagnostics/var_name.rs index 114b4695f5fc9..ba24656725cc3 100644 --- a/compiler/rustc_borrowck/src/diagnostics/var_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/var_name.rs @@ -1,7 +1,7 @@ use rustc_index::IndexSlice; use rustc_middle::mir::visit::{PlaceContext, VisitPlacesWith, Visitor}; use rustc_middle::mir::{Body, Local, Place}; -use rustc_middle::ty::{self, RegionVid, TyCtxt}; +use rustc_middle::ty::{self, RegionUtilitiesExt, RegionVid, TyCtxt}; use rustc_span::{Span, Symbol}; use tracing::debug; diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 822fe1e58ebba..cde8a2bf3264a 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -37,7 +37,8 @@ use rustc_infer::infer::{ use rustc_middle::mir::*; use rustc_middle::query::Providers; use rustc_middle::ty::{ - self, ParamEnv, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypingMode, fold_regions, + self, ParamEnv, RegionUtilitiesExt, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitable, + TypingMode, fold_regions, }; use rustc_middle::{bug, span_bug}; use rustc_mir_dataflow::impls::{EverInitializedPlaces, MaybeUninitializedPlaces}; diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs index dd6eb17947577..94710b3a3f662 100644 --- a/compiler/rustc_borrowck/src/nll.rs +++ b/compiler/rustc_borrowck/src/nll.rs @@ -12,7 +12,7 @@ use rustc_index::IndexSlice; use rustc_middle::mir::pretty::PrettyPrintMirOptions; use rustc_middle::mir::{Body, MirDumper, PassWhere, Promoted}; use rustc_middle::ty::print::with_no_trimmed_paths; -use rustc_middle::ty::{self, TyCtxt}; +use rustc_middle::ty::{self, RegionExt, TyCtxt}; use rustc_mir_dataflow::move_paths::MoveData; use rustc_mir_dataflow::points::DenseLocationMap; use rustc_session::config::MirIncludeSpans; diff --git a/compiler/rustc_borrowck/src/polonius/dump.rs b/compiler/rustc_borrowck/src/polonius/dump.rs index af50f68640f8b..f11fbf0374ad7 100644 --- a/compiler/rustc_borrowck/src/polonius/dump.rs +++ b/compiler/rustc_borrowck/src/polonius/dump.rs @@ -4,7 +4,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_index::IndexVec; use rustc_middle::mir::pretty::{MirDumper, PassWhere, PrettyPrintMirOptions}; use rustc_middle::mir::{Body, Location}; -use rustc_middle::ty::{RegionVid, TyCtxt}; +use rustc_middle::ty::{RegionUtilitiesExt, RegionVid, TyCtxt}; use rustc_mir_dataflow::points::PointIndex; use rustc_session::config::MirIncludeSpans; diff --git a/compiler/rustc_borrowck/src/polonius/liveness_constraints.rs b/compiler/rustc_borrowck/src/polonius/liveness_constraints.rs index b6f8b4a79f39b..26cec60a0f24a 100644 --- a/compiler/rustc_borrowck/src/polonius/liveness_constraints.rs +++ b/compiler/rustc_borrowck/src/polonius/liveness_constraints.rs @@ -4,7 +4,7 @@ use rustc_hir::def_id::DefId; use rustc_middle::ty::relate::{ self, Relate, RelateResult, TypeRelation, relate_args_with_variances, }; -use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{self, RegionUtilitiesExt, RegionVid, Ty, TyCtxt, TypeVisitable}; use super::{ConstraintDirection, PoloniusContext}; use crate::universal_regions::UniversalRegions; diff --git a/compiler/rustc_borrowck/src/region_infer/graphviz.rs b/compiler/rustc_borrowck/src/region_infer/graphviz.rs index ceb33d82deba8..d434e808d513a 100644 --- a/compiler/rustc_borrowck/src/region_infer/graphviz.rs +++ b/compiler/rustc_borrowck/src/region_infer/graphviz.rs @@ -7,7 +7,7 @@ use std::io::{self, Write}; use itertools::Itertools; use rustc_graphviz as dot; -use rustc_middle::ty::UniverseIndex; +use rustc_middle::ty::{RegionUtilitiesExt, UniverseIndex}; use super::*; diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 5cdda777723b3..0cadb704b8124 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -16,7 +16,9 @@ use rustc_middle::mir::{ TerminatorKind, }; use rustc_middle::traits::{ObligationCause, ObligationCauseCode}; -use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex, fold_regions}; +use rustc_middle::ty::{ + self, RegionExt, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex, fold_regions, +}; use rustc_mir_dataflow::points::DenseLocationMap; use rustc_span::hygiene::DesugaringKind; use rustc_span::{DUMMY_SP, Span}; diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs index 49b3e75b25e39..abb2ae49caf92 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs @@ -11,8 +11,9 @@ use rustc_macros::extension; use rustc_middle::mir::{Body, ConstraintCategory}; use rustc_middle::ty::{ self, DefiningScopeKind, DefinitionSiteHiddenType, FallibleTypeFolder, GenericArg, - GenericArgsRef, OpaqueTypeKey, ProvisionalHiddenType, Region, RegionVid, Ty, TyCtxt, - TypeFoldable, TypeSuperFoldable, TypeVisitableExt, Unnormalized, fold_regions, + GenericArgsRef, OpaqueTypeKey, ProvisionalHiddenType, Region, RegionExt, RegionUtilitiesExt, + RegionVid, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitableExt, Unnormalized, + fold_regions, }; use rustc_mir_dataflow::points::DenseLocationMap; use rustc_span::Span; diff --git a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs index 868c6f11b68dc..52fc804a21aa6 100644 --- a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs +++ b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs @@ -8,7 +8,8 @@ use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound}; use rustc_infer::traits::query::type_op::DeeplyNormalize; use rustc_middle::bug; use rustc_middle::ty::{ - self, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, elaborate, fold_regions, + self, GenericArgKind, RegionExt, RegionUtilitiesExt, Ty, TyCtxt, TypeFoldable, + TypeVisitableExt, elaborate, fold_regions, }; use rustc_span::Span; use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput}; diff --git a/compiler/rustc_borrowck/src/type_check/liveness/mod.rs b/compiler/rustc_borrowck/src/type_check/liveness/mod.rs index 442c37e26ec18..07deb46c51fcd 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/mod.rs @@ -4,7 +4,9 @@ use rustc_middle::mir::visit::{TyContext, Visitor}; use rustc_middle::mir::{Body, Local, Location, SourceInfo}; use rustc_middle::span_bug; use rustc_middle::ty::relate::Relate; -use rustc_middle::ty::{GenericArgsRef, Region, RegionVid, Ty, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{ + GenericArgsRef, Region, RegionUtilitiesExt, RegionVid, Ty, TyCtxt, TypeVisitable, +}; use rustc_mir_dataflow::move_paths::MoveData; use rustc_mir_dataflow::points::DenseLocationMap; use tracing::debug; diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 59874e82e920f..78a3e5f7b6b3c 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -27,7 +27,8 @@ use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::cast::CastTy; use rustc_middle::ty::{ self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CoroutineArgsExt, - GenericArgsRef, Ty, TyCtxt, TypeVisitableExt, UserArgs, UserTypeAnnotationIndex, fold_regions, + GenericArgsRef, RegionUtilitiesExt, Ty, TyCtxt, TypeVisitableExt, UserArgs, + UserTypeAnnotationIndex, fold_regions, }; use rustc_mir_dataflow::move_paths::MoveData; use rustc_mir_dataflow::points::DenseLocationMap; diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index f18884df0b44d..88fcbbba9b434 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -12,7 +12,7 @@ use rustc_middle::traits::ObligationCause; use rustc_middle::traits::query::NoSolution; use rustc_middle::ty::relate::combine::{combine_ty_args, super_combine_consts, super_combine_tys}; use rustc_middle::ty::relate::relate_args_invariantly; -use rustc_middle::ty::{self, FnMutDelegate, Ty, TyCtxt, TypeVisitableExt}; +use rustc_middle::ty::{self, FnMutDelegate, RegionUtilitiesExt, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::{bug, span_bug}; use rustc_span::{Span, Symbol, sym}; use tracing::{debug, instrument}; diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index d057b67087c7b..1742eb57206ab 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -25,8 +25,8 @@ use rustc_infer::infer::NllRegionVariableOrigin; use rustc_macros::extension; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{ - self, GenericArgs, GenericArgsRef, InlineConstArgs, InlineConstArgsParts, RegionVid, Ty, - TyCtxt, TypeFoldable, TypeVisitableExt, fold_regions, + self, GenericArgs, GenericArgsRef, InlineConstArgs, InlineConstArgsParts, RegionExt, + RegionUtilitiesExt, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, fold_regions, }; use rustc_middle::{bug, span_bug}; use rustc_span::{ErrorGuaranteed, kw, sym}; diff --git a/compiler/rustc_hir_analysis/src/check/always_applicable.rs b/compiler/rustc_hir_analysis/src/check/always_applicable.rs index bdef0b57d6914..425202c3f7845 100644 --- a/compiler/rustc_hir_analysis/src/check/always_applicable.rs +++ b/compiler/rustc_hir_analysis/src/check/always_applicable.rs @@ -11,7 +11,7 @@ use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt}; use rustc_infer::traits::{ObligationCause, ObligationCauseCode}; use rustc_middle::span_bug; use rustc_middle::ty::util::CheckRegions; -use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypingMode}; +use rustc_middle::ty::{self, GenericArgsRef, RegionExt, Ty, TyCtxt, TypingMode}; use rustc_trait_selection::regions::InferCtxtRegionExt; use rustc_trait_selection::traits::{self, ObligationCtxt}; diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index ffc4e41d3e3a1..bad491ff7bdd1 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -14,9 +14,9 @@ use rustc_infer::infer::{self, BoundRegionConversionTime, InferCtxt, TyCtxtInfer use rustc_infer::traits::util; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::{ - self, BottomUpFolder, GenericArgs, GenericParamDefKind, Generics, Ty, TyCtxt, TypeFoldable, - TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, - Unnormalized, Upcast, + self, BottomUpFolder, GenericArgs, GenericParamDefKind, Generics, RegionExt, + RegionUtilitiesExt, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, + TypeVisitableExt, TypeVisitor, TypingMode, Unnormalized, Upcast, }; use rustc_middle::{bug, span_bug}; use rustc_span::{BytePos, DUMMY_SP, Span}; diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 6862b6fe863f8..c3235e7e19683 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -88,8 +88,8 @@ use rustc_middle::query::Providers; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::print::with_types_for_signature; use rustc_middle::ty::{ - self, GenericArgs, GenericArgsRef, OutlivesPredicate, Region, Ty, TyCtxt, TypingMode, - Unnormalized, + self, GenericArgs, GenericArgsRef, OutlivesPredicate, Region, RegionUtilitiesExt, Ty, TyCtxt, + TypingMode, Unnormalized, }; use rustc_middle::{bug, span_bug}; use rustc_session::parse::feature_err; diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 07ad2a79dc7ad..615941428f8be 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -22,9 +22,9 @@ use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::traits::solve::NoSolution; use rustc_middle::ty::trait_def::TraitSpecializationKind; use rustc_middle::ty::{ - self, AdtKind, GenericArgKind, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeFlags, - TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, - Unnormalized, Upcast, + self, AdtKind, GenericArgKind, GenericArgs, GenericParamDefKind, RegionExt, RegionUtilitiesExt, + Ty, TyCtxt, TypeFlags, TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, + TypeVisitor, TypingMode, Unnormalized, Upcast, }; use rustc_middle::{bug, span_bug}; use rustc_session::parse::feature_err; diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 69a3e519bb2ab..dcda219ccd598 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -34,8 +34,8 @@ use rustc_middle::hir::nested_filter; use rustc_middle::query::Providers; use rustc_middle::ty::util::{Discr, IntTypeExt}; use rustc_middle::ty::{ - self, AdtKind, Const, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized, - fold_regions, + self, AdtKind, Const, IsSuggestable, RegionExt, Ty, TyCtxt, TypeVisitableExt, TypingMode, + Unnormalized, fold_regions, }; use rustc_middle::{bug, span_bug}; use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym}; diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index bd1faded7587a..c6a082f90ab6c 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -7,7 +7,8 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::find_attr; use rustc_middle::ty::{ - self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitable, TypeVisitor, Upcast, + self, GenericPredicates, ImplTraitInTraitData, RegionExt, Ty, TyCtxt, TypeVisitable, + TypeVisitor, Upcast, }; use rustc_middle::{bug, span_bug}; use rustc_span::{DUMMY_SP, Ident, Span}; diff --git a/compiler/rustc_hir_analysis/src/delegation.rs b/compiler/rustc_hir_analysis/src/delegation.rs index da9d3f2290898..21bdce4bad20d 100644 --- a/compiler/rustc_hir_analysis/src/delegation.rs +++ b/compiler/rustc_hir_analysis/src/delegation.rs @@ -9,7 +9,8 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{HirId, PathSegment}; use rustc_middle::ty::{ - self, EarlyBinder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, + self, EarlyBinder, RegionExt, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, + TypeVisitableExt, }; use rustc_span::{ErrorGuaranteed, Span, kw}; diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index a027c552d92b9..8879fc0e732c2 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -39,8 +39,8 @@ use rustc_middle::middle::stability::AllowUnstable; use rustc_middle::ty::print::PrintPolyTraitRefExt as _; use rustc_middle::ty::{ self, Const, FnSigKind, GenericArgKind, GenericArgsRef, GenericParamDefKind, LitToConstInput, - Ty, TyCtxt, TypeSuperFoldable, TypeVisitableExt, TypingMode, Unnormalized, Upcast, - const_lit_matches_ty, fold_regions, + RegionExt, RegionUtilitiesExt, Ty, TyCtxt, TypeSuperFoldable, TypeVisitableExt, TypingMode, + Unnormalized, Upcast, const_lit_matches_ty, fold_regions, }; use rustc_middle::{bug, span_bug}; use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS; diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs index 1fb126fe3d69f..8882e49339180 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs @@ -72,7 +72,8 @@ use rustc_infer::traits::ObligationCause; use rustc_infer::traits::specialization_graph::Node; use rustc_middle::ty::trait_def::TraitSpecializationKind; use rustc_middle::ty::{ - self, GenericArg, GenericArgs, GenericArgsRef, TyCtxt, TypeVisitableExt, TypingMode, + self, GenericArg, GenericArgs, GenericArgsRef, RegionUtilitiesExt, TyCtxt, TypeVisitableExt, + TypingMode, }; use rustc_span::{ErrorGuaranteed, Span}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index ce5fcc558e694..a843618a43cc5 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -31,7 +31,9 @@ use rustc_middle::ty::print::{ PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths, with_no_visible_paths_if_doc_hidden, }; -use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt}; +use rustc_middle::ty::{ + self, GenericArgKind, IsSuggestable, RegionExt, Ty, TyCtxt, TypeVisitableExt, +}; use rustc_span::def_id::DefIdSet; use rustc_span::{ DUMMY_SP, ErrorGuaranteed, ExpnKind, FileName, Ident, MacroKind, Span, Symbol, edit_distance, diff --git a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs index c889381586753..4dd6c0776f219 100644 --- a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs +++ b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs @@ -10,8 +10,8 @@ use rustc_data_structures::sso::SsoHashMap; use rustc_index::Idx; use rustc_middle::bug; use rustc_middle::ty::{ - self, BoundVar, GenericArg, InferConst, List, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeFolder, - TypeSuperFoldable, TypeVisitableExt, + self, BoundVar, GenericArg, InferConst, List, RegionUtilitiesExt, Ty, TyCtxt, TypeFlags, + TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, }; use rustc_type_ir::TypingModeEqWrapper; use smallvec::SmallVec; diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 5203b4ee5d99f..f77e5399f3d61 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -14,7 +14,9 @@ use rustc_index::{Idx, IndexVec}; use rustc_middle::arena::ArenaAllocatable; use rustc_middle::bug; use rustc_middle::infer::canonical::CanonicalVarKind; -use rustc_middle::ty::{self, BoundVar, GenericArg, GenericArgKind, Ty, TyCtxt, TypeFoldable}; +use rustc_middle::ty::{ + self, BoundVar, GenericArg, GenericArgKind, RegionUtilitiesExt, Ty, TyCtxt, TypeFoldable, +}; use tracing::{debug, instrument}; use crate::infer::canonical::instantiate::{CanonicalExt, instantiate_value}; diff --git a/compiler/rustc_infer/src/infer/free_regions.rs b/compiler/rustc_infer/src/infer/free_regions.rs index 8fa5361121c67..26b7576b613f4 100644 --- a/compiler/rustc_infer/src/infer/free_regions.rs +++ b/compiler/rustc_infer/src/infer/free_regions.rs @@ -4,7 +4,7 @@ //! and use that to decide when one free region outlives another, and so forth. use rustc_data_structures::transitive_relation::TransitiveRelation; -use rustc_middle::ty::{Region, TyCtxt}; +use rustc_middle::ty::{Region, RegionUtilitiesExt, TyCtxt}; use tracing::debug; /// Combines a `FreeRegionMap` and a `TyCtxt`. diff --git a/compiler/rustc_infer/src/infer/lexical_region_resolve/indexed_edges.rs b/compiler/rustc_infer/src/infer/lexical_region_resolve/indexed_edges.rs index 382998e55d2e2..a9a027ebed2a8 100644 --- a/compiler/rustc_infer/src/infer/lexical_region_resolve/indexed_edges.rs +++ b/compiler/rustc_infer/src/infer/lexical_region_resolve/indexed_edges.rs @@ -1,4 +1,5 @@ use rustc_index::IndexVec; +use rustc_middle::ty::RegionUtilitiesExt; use rustc_type_ir::RegionVid; use crate::infer::SubregionOrigin; diff --git a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs index 9e5c31eecbe77..fa45fb4bec5d2 100644 --- a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs +++ b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs @@ -8,7 +8,7 @@ use rustc_data_structures::unord::UnordSet; use rustc_index::{IndexSlice, IndexVec}; use rustc_middle::ty::{ self, ReBound, ReEarlyParam, ReErased, ReError, ReLateParam, RePlaceholder, ReStatic, ReVar, - Region, RegionVid, Ty, TyCtxt, TypeFoldable, fold_regions, + Region, RegionUtilitiesExt, RegionVid, Ty, TyCtxt, TypeFoldable, fold_regions, }; use rustc_middle::{bug, span_bug}; use rustc_span::Span; diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 26c03066c7e40..ed0ca8ef493bd 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -30,8 +30,9 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::{ self, BoundVarReplacerDelegate, ConstVid, FloatVid, GenericArg, GenericArgKind, GenericArgs, GenericArgsRef, GenericParamDefKind, InferConst, IntVid, OpaqueTypeKey, ProvisionalHiddenType, - PseudoCanonicalInput, Term, TermKind, Ty, TyCtxt, TyVid, TypeFoldable, TypeFolder, - TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypingEnv, TypingMode, fold_regions, + PseudoCanonicalInput, RegionExt, RegionUtilitiesExt, Term, TermKind, Ty, TyCtxt, TyVid, + TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypingEnv, + TypingMode, fold_regions, }; use rustc_span::{DUMMY_SP, Span, Symbol}; use snapshot::undo_log::InferCtxtUndoLogs; diff --git a/compiler/rustc_infer/src/infer/region_constraints/mod.rs b/compiler/rustc_infer/src/infer/region_constraints/mod.rs index 38b87eb7a9863..e7dbfcfc32c0b 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/mod.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/mod.rs @@ -8,7 +8,9 @@ use rustc_data_structures::undo_log::UndoLogs; use rustc_data_structures::unify as ut; use rustc_index::IndexVec; use rustc_macros::{TypeFoldable, TypeVisitable}; -use rustc_middle::ty::{self, ReBound, ReStatic, ReVar, Region, RegionVid, Ty, TyCtxt}; +use rustc_middle::ty::{ + self, ReBound, ReStatic, ReVar, Region, RegionExt, RegionUtilitiesExt, RegionVid, Ty, TyCtxt, +}; use rustc_middle::{bug, span_bug}; use tracing::{debug, instrument}; diff --git a/compiler/rustc_lint/src/impl_trait_overcaptures.rs b/compiler/rustc_lint/src/impl_trait_overcaptures.rs index 65dfa8b93de78..c2edbf6f05ddb 100644 --- a/compiler/rustc_lint/src/impl_trait_overcaptures.rs +++ b/compiler/rustc_lint/src/impl_trait_overcaptures.rs @@ -16,8 +16,8 @@ use rustc_middle::ty::relate::{ structurally_relate_tys, }; use rustc_middle::ty::{ - self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, - Unnormalized, + self, RegionExt, RegionUtilitiesExt, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, + TypeVisitableExt, TypeVisitor, Unnormalized, }; use rustc_middle::{bug, span_bug}; use rustc_session::lint::fcw; diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 280b3b12e5c85..3f3bcbc034bed 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -396,11 +396,6 @@ impl<'a> BlobDecodeContext<'a> { impl<'a, 'tcx> TyDecoder<'tcx> for MetadataDecodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; - #[inline] - fn interner(&self) -> TyCtxt<'tcx> { - self.tcx - } - fn cached_ty_for_shorthand(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx> where F: FnOnce(&mut Self) -> Ty<'tcx>, @@ -437,6 +432,15 @@ impl<'a, 'tcx> TyDecoder<'tcx> for MetadataDecodeContext<'a, 'tcx> { } } +impl<'a, 'tcx> rustc_middle::ty::InternerDecoder for MetadataDecodeContext<'a, 'tcx> { + type Interner = TyCtxt<'tcx>; + + #[inline] + fn interner(&self) -> TyCtxt<'tcx> { + self.tcx + } +} + impl<'a, 'tcx> Decodable> for ExpnIndex { #[inline] fn decode(d: &mut MetadataDecodeContext<'a, 'tcx>) -> ExpnIndex { diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index df2c8d8b98f97..cfc88bf978525 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -489,11 +489,6 @@ where impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = false; - #[inline] - fn interner(&self) -> TyCtxt<'tcx> { - self.tcx - } - fn cached_ty_for_shorthand(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx> where F: FnOnce(&mut Self) -> Ty<'tcx>, @@ -531,6 +526,15 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { } } +impl<'a, 'tcx> rustc_type_ir::InternerDecoder for CacheDecoder<'a, 'tcx> { + type Interner = TyCtxt<'tcx>; + + #[inline] + fn interner(&self) -> Self::Interner { + self.tcx + } +} + crate::implement_ty_decoder!(CacheDecoder<'a, 'tcx>); // This ensures that the `Decodable::decode` specialization for `Vec` is used diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 28bdeabf34dc1..9feba407f4d6e 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -41,11 +41,11 @@ pub trait TyEncoder<'tcx>: SpanEncoder { fn encode_alloc_id(&mut self, alloc_id: &AllocId); } -pub trait TyDecoder<'tcx>: SpanDecoder { +pub trait TyDecoder<'tcx>: + SpanDecoder + rustc_type_ir::InternerDecoder> +{ const CLEAR_CROSS_CRATE: bool; - fn interner(&self) -> TyCtxt<'tcx>; - fn cached_ty_for_shorthand(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx> where F: FnOnce(&mut Self) -> Ty<'tcx>; @@ -158,12 +158,6 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Clause<'tcx> { } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Region<'tcx> { - fn encode(&self, e: &mut E) { - self.kind().encode(e); - } -} - impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Const<'tcx> { fn encode(&self, e: &mut E) { self.0.0.encode(e); @@ -297,12 +291,6 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Region<'tcx> { - fn decode(decoder: &mut D) -> Self { - ty::Region::new_from_kind(decoder.interner(), Decodable::decode(decoder)) - } -} - impl<'tcx, D: TyDecoder<'tcx>> Decodable for CanonicalVarKinds<'tcx> { fn decode(decoder: &mut D) -> Self { let len = decoder.read_usize(); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 44cd6499fb088..febcc23779b7f 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -68,6 +68,7 @@ use crate::thir::Thir; use crate::traits; use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques}; use crate::ty::predicate::ExistentialPredicateStableCmpExt as _; +use crate::ty::region::{RegionExt, RegionUtilitiesExt}; use crate::ty::{ self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, GenericArg, GenericArgs, GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, Pattern, diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 733985c606e22..98f6a21a78559 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -3,6 +3,7 @@ use std::{debug_assert_matches, fmt}; use rustc_abi::ExternAbi; +use rustc_data_structures::intern::Interned; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind}; @@ -11,7 +12,8 @@ use rustc_hir::lang_items::LangItem; use rustc_span::{DUMMY_SP, Span, Symbol}; use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem}; use rustc_type_ir::{ - CollectAndApply, FnSigKind, Interner, TypeFoldable, Unnormalized, search_graph, + BoundVar, CollectAndApply, DebruijnIndex, FnSigKind, Interner, TypeFoldable, Unnormalized, + search_graph, }; use crate::dep_graph::{DepKind, DepNodeIndex}; @@ -21,8 +23,8 @@ use crate::traits::solve::{ self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect, }; use crate::ty::{ - self, Clause, Const, List, ParamTy, Pattern, PolyExistentialPredicate, Predicate, Region, Ty, - TyCtxt, + self, BoundRegion, Clause, Const, List, ParamTy, Pattern, PolyExistentialPredicate, Predicate, + Region, RegionKind, Ty, TyCtxt, }; #[allow(rustc::usage_of_ty_tykind)] @@ -103,8 +105,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type ExprConst = ty::Expr<'tcx>; type ValTree = ty::ValTree<'tcx>; type ScalarInt = ty::ScalarInt; - - type Region = Region<'tcx>; + type InternedRegionKind = Interned<'tcx, ty::RegionKind<'tcx>>; type EarlyParamRegion = ty::EarlyParamRegion; type LateParamRegion = ty::LateParamRegion; @@ -737,6 +738,54 @@ impl<'tcx> Interner for TyCtxt<'tcx> { bug!("item_name: no name for {:?}", self.def_path(id)); }) } + + fn get_anon_re_bounds_lifetime(self, idx: usize, var_idx: usize) -> Option> { + if let Some(inner) = self.lifetimes.anon_re_bounds.get(idx) { + inner.get(var_idx).copied() + } else { + None + } + } + + fn get_anon_re_canonical_bounds_lifetime(self, idx: usize) -> Option> { + self.lifetimes.anon_re_canonical_bounds.get(idx).copied() + } + + fn get_re_static_lifetime(self) -> Region<'tcx> { + self.lifetimes.re_static + } + + fn intern_region(self, region_kind: RegionKind<'tcx>) -> Region<'tcx> { + self.intern_region(region_kind) + } + + fn intern_bound_region( + self, + debruijn: DebruijnIndex, + bound_region: BoundRegion<'tcx>, + ) -> Region<'tcx> { + // Use a pre-interned one when possible. + if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region + && let Some(inner) = self.lifetimes.anon_re_bounds.get(debruijn.as_usize()) + && let Some(re) = inner.get(var.as_usize()).copied() + { + re + } else { + self.intern_region(ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), bound_region)) + } + } + + fn intern_canonical_bound(self, var: BoundVar) -> Region<'tcx> { + // Use a pre-interned one when possible. + if let Some(re) = self.lifetimes.anon_re_canonical_bounds.get(var.as_usize()).copied() { + re + } else { + self.intern_region(ty::ReBound( + ty::BoundVarIndexKind::Canonical, + BoundRegion { var, kind: ty::BoundRegionKind::Anon }, + )) + } + } } /// Defines trivial conversion functions between the main [`LangItem`] enum, diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index a43a0c687261a..f0b9e37cb7e80 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -4,9 +4,7 @@ use std::fmt::Write; use std::ops::ControlFlow; use rustc_data_structures::fx::FxIndexMap; -use rustc_errors::{ - Applicability, Diag, DiagArgValue, IntoDiagArg, into_diag_arg_using_display, listify, pluralize, -}; +use rustc_errors::{Applicability, Diag, DiagArgValue, IntoDiagArg, listify, pluralize}; use rustc_hir::def::{DefKind, Namespace}; use rustc_hir::def_id::DefId; use rustc_hir::{self as hir, AmbigArg, LangItem, PredicateOrigin, WherePredicateKind}; @@ -37,10 +35,6 @@ impl IntoDiagArg for Instance<'_> { } } -into_diag_arg_using_display! { - ty::Region<'_>, -} - impl<'tcx> Ty<'tcx> { /// Similar to `Ty::is_primitive`, but also considers inferred numeric values to be primitive. pub fn is_primitive_ty(self) -> bool { diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 3d9148d6ed7ba..c146e7c982de9 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -2,6 +2,7 @@ use rustc_data_structures::fx::FxIndexMap; use rustc_hir::def_id::DefId; use rustc_type_ir::data_structures::DelayedMap; +use crate::ty::region::RegionExt; use crate::ty::{ self, Binder, BoundTy, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index fd6078942e3ce..9cb1215881031 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -7,6 +7,7 @@ use tracing::instrument; use super::{Clause, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt, Unnormalized}; use crate::ty; +use crate::ty::region::RegionExt; use crate::ty::{EarlyBinder, GenericArgsRef}; #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 56a7abfac6635..cbaa123bd43f2 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -101,7 +101,8 @@ pub use self::predicate::{ TraitRef, TypeOutlivesPredicate, }; pub use self::region::{ - EarlyParamRegion, LateParamRegion, LateParamRegionKind, Region, RegionKind, RegionVid, + EarlyParamRegion, LateParamRegion, LateParamRegionKind, Region, RegionExt, RegionKind, + RegionUtilitiesExt, RegionVid, }; pub use self::sty::{ AliasTy, AliasTyKind, Article, Binder, BoundConst, BoundRegion, BoundRegionKind, BoundTy, diff --git a/compiler/rustc_middle/src/ty/opaque_types.rs b/compiler/rustc_middle/src/ty/opaque_types.rs index 2b024b7b6cbb3..31ca9c0a54db4 100644 --- a/compiler/rustc_middle/src/ty/opaque_types.rs +++ b/compiler/rustc_middle/src/ty/opaque_types.rs @@ -5,7 +5,8 @@ use tracing::{debug, instrument, trace}; use crate::error::ConstNotUsedTraitAlias; use crate::ty::{ - self, GenericArg, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, + self, GenericArg, GenericArgKind, RegionExt, Ty, TyCtxt, TypeFoldable, TypeFolder, + TypeSuperFoldable, }; pub type OpaqueTypeKey<'tcx> = rustc_type_ir::OpaqueTypeKey>; @@ -133,7 +134,7 @@ impl<'tcx> TypeFolder> for ReverseMapper<'tcx> { self.span, format!( "lifetime `{r}` is part of concrete type but not used in \ - parameter list of the `impl Trait` type alias" + parameter list of the `impl Trait` type alias", ), ) .emit(); diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index d3132d3f65780..2ebdd9d9609d1 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -24,6 +24,7 @@ use smallvec::SmallVec; use super::*; use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar}; use crate::query::{IntoQueryKey, Providers}; +use crate::ty::region::{RegionExt, RegionUtilitiesExt}; use crate::ty::{ ConstInt, Expr, GenericArgKind, ParamConst, ScalarInt, Term, TermKind, TraitPredicate, TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, @@ -3127,7 +3128,6 @@ macro_rules! define_print_and_forward_display { } forward_display_to_print! { - ty::Region<'tcx>, Ty<'tcx>, &'tcx ty::List>, ty::Const<'tcx> diff --git a/compiler/rustc_middle/src/ty/region.rs b/compiler/rustc_middle/src/ty/region.rs index 798b98c5def5c..ea82d0ff1badb 100644 --- a/compiler/rustc_middle/src/ty/region.rs +++ b/compiler/rustc_middle/src/ty/region.rs @@ -1,45 +1,20 @@ -use rustc_data_structures::intern::Interned; use rustc_errors::MultiSpan; use rustc_hir::def_id::DefId; -use rustc_macros::{HashStable, TyDecodable, TyEncodable}; +use rustc_macros::{HashStable, TyDecodable, TyEncodable, extension}; use rustc_span::{DUMMY_SP, ErrorGuaranteed, Symbol, kw, sym}; -use rustc_type_ir::RegionKind as IrRegionKind; pub use rustc_type_ir::RegionVid; +use rustc_type_ir::{Region as IrRegion, RegionKind as IrRegionKind}; use tracing::debug; use crate::ty::{self, BoundVar, TyCtxt, TypeFlags}; +pub type Region<'tcx> = IrRegion>; pub type RegionKind<'tcx> = IrRegionKind>; -/// Use this rather than `RegionKind`, whenever possible. -#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)] -#[rustc_pass_by_value] -pub struct Region<'tcx>(pub Interned<'tcx, RegionKind<'tcx>>); - -impl<'tcx> rustc_type_ir::inherent::IntoKind for Region<'tcx> { - type Kind = RegionKind<'tcx>; - - fn kind(self) -> RegionKind<'tcx> { - *self.0.0 - } -} - -impl<'tcx> rustc_type_ir::Flags for Region<'tcx> { - fn flags(&self) -> TypeFlags { - self.type_flags() - } - - fn outer_exclusive_binder(&self) -> ty::DebruijnIndex { - match self.kind() { - ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) => debruijn.shifted_in(1), - _ => ty::INNERMOST, - } - } -} - +#[extension(pub trait RegionExt<'tcx>)] impl<'tcx> Region<'tcx> { #[inline] - pub fn new_early_param( + fn new_early_param( tcx: TyCtxt<'tcx>, early_bound_region: ty::EarlyParamRegion, ) -> Region<'tcx> { @@ -47,47 +22,13 @@ impl<'tcx> Region<'tcx> { } #[inline] - pub fn new_bound( - tcx: TyCtxt<'tcx>, - debruijn: ty::DebruijnIndex, - bound_region: ty::BoundRegion<'tcx>, - ) -> Region<'tcx> { - // Use a pre-interned one when possible. - if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region - && let Some(inner) = tcx.lifetimes.anon_re_bounds.get(debruijn.as_usize()) - && let Some(re) = inner.get(var.as_usize()).copied() - { - re - } else { - tcx.intern_region(ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), bound_region)) - } - } - - #[inline] - pub fn new_canonical_bound(tcx: TyCtxt<'tcx>, var: ty::BoundVar) -> Region<'tcx> { - // Use a pre-interned one when possible. - if let Some(re) = tcx.lifetimes.anon_re_canonical_bounds.get(var.as_usize()).copied() { - re - } else { - tcx.intern_region(ty::ReBound( - ty::BoundVarIndexKind::Canonical, - ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon }, - )) - } - } - - #[inline] - pub fn new_late_param( - tcx: TyCtxt<'tcx>, - scope: DefId, - kind: LateParamRegionKind, - ) -> Region<'tcx> { + fn new_late_param(tcx: TyCtxt<'tcx>, scope: DefId, kind: LateParamRegionKind) -> Region<'tcx> { let data = LateParamRegion { scope, kind }; tcx.intern_region(ty::ReLateParam(data)) } #[inline] - pub fn new_var(tcx: TyCtxt<'tcx>, v: ty::RegionVid) -> Region<'tcx> { + fn new_var(tcx: TyCtxt<'tcx>, v: ty::RegionVid) -> Region<'tcx> { // Use a pre-interned one when possible. tcx.lifetimes .re_vars @@ -96,24 +37,16 @@ impl<'tcx> Region<'tcx> { .unwrap_or_else(|| tcx.intern_region(ty::ReVar(v))) } - #[inline] - pub fn new_placeholder( - tcx: TyCtxt<'tcx>, - placeholder: ty::PlaceholderRegion<'tcx>, - ) -> Region<'tcx> { - tcx.intern_region(ty::RePlaceholder(placeholder)) - } - /// Constructs a `RegionKind::ReError` region. #[track_caller] - pub fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Region<'tcx> { + fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Region<'tcx> { tcx.intern_region(ty::ReError(guar)) } /// Constructs a `RegionKind::ReError` region and registers a delayed bug to ensure it gets /// used. #[track_caller] - pub fn new_error_misc(tcx: TyCtxt<'tcx>) -> Region<'tcx> { + fn new_error_misc(tcx: TyCtxt<'tcx>) -> Region<'tcx> { Region::new_error_with_message( tcx, DUMMY_SP, @@ -124,7 +57,7 @@ impl<'tcx> Region<'tcx> { /// Constructs a `RegionKind::ReError` region and registers a delayed bug with the given `msg` /// to ensure it gets used. #[track_caller] - pub fn new_error_with_message>( + fn new_error_with_message>( tcx: TyCtxt<'tcx>, span: S, msg: &'static str, @@ -135,7 +68,7 @@ impl<'tcx> Region<'tcx> { /// Avoid this in favour of more specific `new_*` methods, where possible, /// to avoid the cost of the `match`. - pub fn new_from_kind(tcx: TyCtxt<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx> { + fn new_from_kind(tcx: TyCtxt<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx> { match kind { ty::ReEarlyParam(region) => Region::new_early_param(tcx, region), ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), region) => { @@ -156,39 +89,14 @@ impl<'tcx> Region<'tcx> { } } -impl<'tcx> rustc_type_ir::inherent::Region> for Region<'tcx> { - fn new_bound( - interner: TyCtxt<'tcx>, - debruijn: ty::DebruijnIndex, - var: ty::BoundRegion<'tcx>, - ) -> Self { - Region::new_bound(interner, debruijn, var) - } - - fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self { - Region::new_bound(tcx, debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon }) - } - - fn new_canonical_bound(tcx: TyCtxt<'tcx>, var: rustc_type_ir::BoundVar) -> Self { - Region::new_canonical_bound(tcx, var) - } - - fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderRegion<'tcx>) -> Self { - Region::new_placeholder(tcx, placeholder) - } - - fn new_static(tcx: TyCtxt<'tcx>) -> Self { - tcx.lifetimes.re_static - } -} - /// Region utilities +#[extension(pub trait RegionUtilitiesExt<'tcx>)] impl<'tcx> Region<'tcx> { - pub fn kind(self) -> RegionKind<'tcx> { + fn kind(self) -> RegionKind<'tcx> { *self.0.0 } - pub fn get_name(self, tcx: TyCtxt<'tcx>) -> Option { + fn get_name(self, tcx: TyCtxt<'tcx>) -> Option { match self.kind() { ty::ReEarlyParam(ebr) => ebr.is_named().then_some(ebr.name), ty::ReBound(_, br) => br.kind.get_name(tcx), @@ -199,7 +107,7 @@ impl<'tcx> Region<'tcx> { } } - pub fn get_name_or_anon(self, tcx: TyCtxt<'tcx>) -> Symbol { + fn get_name_or_anon(self, tcx: TyCtxt<'tcx>) -> Symbol { match self.get_name(tcx) { Some(name) => name, None => sym::anon, @@ -207,7 +115,7 @@ impl<'tcx> Region<'tcx> { } /// Is this region named by the user? - pub fn is_named(self, tcx: TyCtxt<'tcx>) -> bool { + fn is_named(self, tcx: TyCtxt<'tcx>) -> bool { match self.kind() { ty::ReEarlyParam(ebr) => ebr.is_named(), ty::ReBound(_, br) => br.kind.is_named(tcx), @@ -221,39 +129,39 @@ impl<'tcx> Region<'tcx> { } #[inline] - pub fn is_error(self) -> bool { + fn is_error(self) -> bool { matches!(self.kind(), ty::ReError(_)) } #[inline] - pub fn is_static(self) -> bool { + fn is_static(self) -> bool { matches!(self.kind(), ty::ReStatic) } #[inline] - pub fn is_erased(self) -> bool { + fn is_erased(self) -> bool { matches!(self.kind(), ty::ReErased) } #[inline] - pub fn is_bound(self) -> bool { + fn is_bound(self) -> bool { matches!(self.kind(), ty::ReBound(..)) } #[inline] - pub fn is_placeholder(self) -> bool { + fn is_placeholder(self) -> bool { matches!(self.kind(), ty::RePlaceholder(..)) } #[inline] - pub fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool { + fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool { match self.kind() { ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) => debruijn >= index, _ => false, } } - pub fn type_flags(self) -> TypeFlags { + fn type_flags(self) -> TypeFlags { let mut flags = TypeFlags::empty(); match self.kind() { @@ -301,14 +209,14 @@ impl<'tcx> Region<'tcx> { } /// True for free regions other than `'static`. - pub fn is_param(self) -> bool { + fn is_param(self) -> bool { matches!(self.kind(), ty::ReEarlyParam(_) | ty::ReLateParam(_)) } /// True for free region in the current context. /// /// This is the case for `'static` and param regions. - pub fn is_free(self) -> bool { + fn is_free(self) -> bool { match self.kind() { ty::ReStatic | ty::ReEarlyParam(..) | ty::ReLateParam(..) => true, ty::ReVar(..) @@ -319,11 +227,11 @@ impl<'tcx> Region<'tcx> { } } - pub fn is_var(self) -> bool { + fn is_var(self) -> bool { matches!(self.kind(), ty::ReVar(_)) } - pub fn as_var(self) -> RegionVid { + fn as_var(self) -> RegionVid { match self.kind() { ty::ReVar(vid) => vid, _ => bug!("expected region {:?} to be of kind ReVar", self), @@ -332,7 +240,7 @@ impl<'tcx> Region<'tcx> { /// Given some item `binding_item`, check if this region is a generic parameter introduced by it /// or one of the parent generics. Returns the `DefId` of the parameter definition if so. - pub fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId) -> Option { + fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId) -> Option { match self.kind() { ty::ReEarlyParam(ebr) => { Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id) diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index 9f2a1187a9ddc..f4b46ee3ca582 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -98,16 +98,6 @@ impl<'tcx> Relate> for ty::GenericArgsRef<'tcx> { } } -impl<'tcx> Relate> for ty::Region<'tcx> { - fn relate>>( - relation: &mut R, - a: ty::Region<'tcx>, - b: ty::Region<'tcx>, - ) -> RelateResult<'tcx, ty::Region<'tcx>> { - relation.regions(a, b) - } -} - impl<'tcx> Relate> for ty::Const<'tcx> { fn relate>>( relation: &mut R, diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index f6d5d226683b3..02e36670ff4c2 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -11,7 +11,7 @@ use rustc_hir::def_id::LocalDefId; use rustc_span::Spanned; use rustc_type_ir::{ConstKind, TypeFolder, VisitorResult, try_visit}; -use super::{GenericArg, GenericArgKind, Pattern, Region}; +use super::{GenericArg, GenericArgKind, Pattern}; use crate::mir::PlaceElem; use crate::ty::print::{FmtPrinter, Printer, with_no_trimmed_paths}; use crate::ty::{ @@ -169,12 +169,6 @@ impl<'tcx> fmt::Debug for GenericArg<'tcx> { } } -impl<'tcx> fmt::Debug for Region<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self.kind()) - } -} - /////////////////////////////////////////////////////////////////////////// // Atomic structs // @@ -479,25 +473,6 @@ impl<'tcx> TypeSuperVisitable> for Ty<'tcx> { } } -impl<'tcx> TypeFoldable> for ty::Region<'tcx> { - fn try_fold_with>>( - self, - folder: &mut F, - ) -> Result { - folder.try_fold_region(self) - } - - fn fold_with>>(self, folder: &mut F) -> Self { - folder.fold_region(self) - } -} - -impl<'tcx> TypeVisitable> for ty::Region<'tcx> { - fn visit_with>>(&self, visitor: &mut V) -> V::Result { - visitor.visit_region(*self) - } -} - impl<'tcx> TypeFoldable> for ty::Predicate<'tcx> { fn try_fold_with>>( self, diff --git a/compiler/rustc_mir_transform/src/promote_consts.rs b/compiler/rustc_mir_transform/src/promote_consts.rs index a47b3ce64ed23..156c3e31a140c 100644 --- a/compiler/rustc_mir_transform/src/promote_consts.rs +++ b/compiler/rustc_mir_transform/src/promote_consts.rs @@ -21,7 +21,7 @@ use rustc_hir::def::DefKind; use rustc_index::{IndexSlice, IndexVec}; use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::*; -use rustc_middle::ty::{self, GenericArgs, List, Ty, TyCtxt, TypeVisitableExt}; +use rustc_middle::ty::{self, GenericArgs, List, RegionUtilitiesExt, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::{bug, mir, span_bug}; use rustc_span::{Span, Spanned}; use tracing::{debug, instrument}; diff --git a/compiler/rustc_next_trait_solver/src/canonical/canonicalizer.rs b/compiler/rustc_next_trait_solver/src/canonical/canonicalizer.rs index 0a3672343e33c..0fbbebd2cc2ff 100644 --- a/compiler/rustc_next_trait_solver/src/canonical/canonicalizer.rs +++ b/compiler/rustc_next_trait_solver/src/canonical/canonicalizer.rs @@ -3,7 +3,7 @@ use rustc_type_ir::inherent::*; use rustc_type_ir::solve::{Goal, QueryInput}; use rustc_type_ir::{ self as ty, Canonical, CanonicalParamEnvCacheEntry, CanonicalVarKind, Flags, InferCtxtLike, - Interner, PlaceholderConst, PlaceholderType, TypeFlags, TypeFoldable, TypeFolder, + Interner, PlaceholderConst, PlaceholderType, Region, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, }; @@ -409,7 +409,7 @@ impl, I: Interner> TypeFolder for Canonicaliz self.delegate.cx() } - fn fold_region(&mut self, r: I::Region) -> I::Region { + fn fold_region(&mut self, r: Region) -> Region { let kind = match r.kind() { ty::ReBound(..) => return r, diff --git a/compiler/rustc_next_trait_solver/src/coherence.rs b/compiler/rustc_next_trait_solver/src/coherence.rs index 589d055192029..77569e5ec02c7 100644 --- a/compiler/rustc_next_trait_solver/src/coherence.rs +++ b/compiler/rustc_next_trait_solver/src/coherence.rs @@ -4,7 +4,7 @@ use std::ops::ControlFlow; use derive_where::derive_where; use rustc_type_ir::inherent::*; use rustc_type_ir::{ - self as ty, InferCtxtLike, Interner, TrivialTypeTraversalImpls, TypeVisitable, + self as ty, InferCtxtLike, Interner, Region, TrivialTypeTraversalImpls, TypeVisitable, TypeVisitableExt, TypeVisitor, }; use tracing::instrument; @@ -317,7 +317,7 @@ where { type Result = ControlFlow>; - fn visit_region(&mut self, _r: I::Region) -> Self::Result { + fn visit_region(&mut self, _r: Region) -> Self::Result { ControlFlow::Continue(()) } diff --git a/compiler/rustc_next_trait_solver/src/placeholder.rs b/compiler/rustc_next_trait_solver/src/placeholder.rs index 83f3bdf01dc83..f7b03f3de71a5 100644 --- a/compiler/rustc_next_trait_solver/src/placeholder.rs +++ b/compiler/rustc_next_trait_solver/src/placeholder.rs @@ -4,7 +4,7 @@ use rustc_type_ir::data_structures::IndexMap; use rustc_type_ir::inherent::*; use rustc_type_ir::{ self as ty, InferCtxtLike, Interner, PlaceholderConst, PlaceholderRegion, PlaceholderType, - TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, + Region, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, }; pub struct BoundVarReplacer<'a, Infcx, I = ::Interner> @@ -90,7 +90,7 @@ where t } - fn fold_region(&mut self, r: I::Region) -> I::Region { + fn fold_region(&mut self, r: Region) -> Region { match r.kind() { ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) if debruijn.as_usize() diff --git a/compiler/rustc_next_trait_solver/src/resolve.rs b/compiler/rustc_next_trait_solver/src/resolve.rs index c3c57eccd6eff..ab3415a2bbc70 100644 --- a/compiler/rustc_next_trait_solver/src/resolve.rs +++ b/compiler/rustc_next_trait_solver/src/resolve.rs @@ -1,7 +1,7 @@ use rustc_type_ir::data_structures::DelayedMap; use rustc_type_ir::inherent::*; use rustc_type_ir::{ - self as ty, InferCtxtLike, Interner, TypeFoldable, TypeFolder, TypeSuperFoldable, + self as ty, InferCtxtLike, Interner, Region, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, }; @@ -72,7 +72,7 @@ impl, I: Interner> TypeFolder for EagerResolv } } - fn fold_region(&mut self, r: I::Region) -> I::Region { + fn fold_region(&mut self, r: Region) -> Region { match r.kind() { ty::ReVar(vid) => self.delegate.opportunistic_resolve_lt_var(vid), _ => r, diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index b2a4e90b5bfcb..0d8b2824dfce0 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -11,7 +11,7 @@ use rustc_type_ir::lang_items::SolverTraitLangItem; use rustc_type_ir::search_graph::CandidateHeadUsages; use rustc_type_ir::solve::{AliasBoundKind, SizedTraitKind}; use rustc_type_ir::{ - self as ty, AliasTy, Interner, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, + self as ty, AliasTy, Interner, Region, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, Unnormalized, Upcast, elaborate, }; @@ -1368,7 +1368,7 @@ where } } - fn visit_region(&mut self, r: I::Region) -> Self::Result { + fn visit_region(&mut self, r: Region) -> Self::Result { match self.ecx.eager_resolve_region(r).kind() { ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()), ty::RePlaceholder(p) => { diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs index c3bffb5475c76..a9f164962bd4a 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs @@ -8,7 +8,7 @@ use rustc_type_ir::lang_items::{SolverLangItem, SolverTraitLangItem}; use rustc_type_ir::solve::SizedTraitKind; use rustc_type_ir::solve::inspect::ProbeKind; use rustc_type_ir::{ - self as ty, Binder, FallibleTypeFolder, Interner, Movability, Mutability, TypeFoldable, + self as ty, Binder, FallibleTypeFolder, Interner, Movability, Mutability, Region, TypeFoldable, TypeSuperFoldable, Unnormalized, Upcast as _, elaborate, }; use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic}; @@ -435,7 +435,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable, ) -> Result<(ty::Binder>, Vec), NoSolution> { match self_ty.kind() { ty::CoroutineClosure(def_id, args) => { @@ -613,7 +613,7 @@ fn fn_item_to_async_callable( fn coroutine_closure_to_certain_coroutine( cx: I, goal_kind: ty::ClosureKind, - goal_region: I::Region, + goal_region: Region, def_id: I::CoroutineClosureId, args: ty::CoroutineClosureArgs, sig: ty::CoroutineClosureSignature, @@ -637,7 +637,7 @@ fn coroutine_closure_to_certain_coroutine( fn coroutine_closure_to_ambiguous_coroutine( cx: I, goal_kind: ty::ClosureKind, - goal_region: I::Region, + goal_region: Region, def_id: I::CoroutineClosureId, args: ty::CoroutineClosureArgs, sig: ty::CoroutineClosureSignature, diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs index 8933ac16b2b10..f2be6fd9ed75f 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs @@ -10,7 +10,7 @@ use rustc_type_ir::relate::solver_relating::RelateExt; use rustc_type_ir::search_graph::{CandidateHeadUsages, PathKind}; use rustc_type_ir::solve::OpaqueTypesJank; use rustc_type_ir::{ - self as ty, CanonicalVarValues, InferCtxtLike, Interner, TypeFoldable, TypeFolder, + self as ty, CanonicalVarValues, InferCtxtLike, Interner, Region, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, }; @@ -775,7 +775,7 @@ where } } - pub(super) fn next_region_var(&mut self) -> I::Region { + pub(super) fn next_region_var(&mut self) -> Region { let region = self.delegate.next_region_infer(); self.inspect.add_var_value(region); region @@ -1076,7 +1076,7 @@ where self.delegate.shallow_resolve(ty) } - pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region { + pub(super) fn eager_resolve_region(&self, r: Region) -> Region { if let ty::ReVar(vid) = r.kind() { self.delegate.opportunistic_resolve_lt_var(vid) } else { @@ -1092,11 +1092,11 @@ where args } - pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) { + pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: Region) { self.delegate.register_ty_outlives(ty, lt, self.origin_span); } - pub(super) fn register_region_outlives(&self, a: I::Region, b: I::Region) { + pub(super) fn register_region_outlives(&self, a: Region, b: Region) { // `'a: 'b` ==> `'b <= 'a` self.delegate.sub_regions(b, a, self.origin_span); } diff --git a/compiler/rustc_next_trait_solver/src/solve/mod.rs b/compiler/rustc_next_trait_solver/src/solve/mod.rs index d37b328920ce1..64d5b7e9c1ba7 100644 --- a/compiler/rustc_next_trait_solver/src/solve/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/mod.rs @@ -24,7 +24,7 @@ mod trait_goals; use derive_where::derive_where; use rustc_type_ir::inherent::*; pub use rustc_type_ir::solve::*; -use rustc_type_ir::{self as ty, Interner, TyVid, TypingMode}; +use rustc_type_ir::{self as ty, Interner, Region, TyVid, TypingMode}; use tracing::instrument; pub use self::eval_ctxt::{ @@ -98,7 +98,7 @@ where #[instrument(level = "trace", skip(self))] fn compute_region_outlives_goal( &mut self, - goal: Goal>, + goal: Goal>>, ) -> QueryResult { let ty::OutlivesPredicate(a, b) = goal.predicate; self.register_region_outlives(a, b); diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs index bbc5739268ef7..8375d58bb3c6a 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs @@ -8,7 +8,7 @@ use rustc_type_ir::inherent::*; use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem}; use rustc_type_ir::solve::SizedTraitKind; use rustc_type_ir::{ - self as ty, FieldInfo, Interner, NormalizesTo, PredicateKind, Unnormalized, Upcast as _, + self as ty, FieldInfo, Interner, NormalizesTo, PredicateKind, Region, Unnormalized, Upcast as _, }; use tracing::instrument; diff --git a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs index ffe90da4cb0fe..de3d383373d9c 100644 --- a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs +++ b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs @@ -8,8 +8,8 @@ use rustc_type_ir::solve::{ AliasBoundKind, CandidatePreferenceMode, CanonicalResponse, SizedTraitKind, }; use rustc_type_ir::{ - self as ty, FieldInfo, Interner, Movability, PredicatePolarity, TraitPredicate, TraitRef, - TypeVisitableExt as _, TypingMode, Unnormalized, Upcast as _, elaborate, + self as ty, FieldInfo, Interner, Movability, PredicatePolarity, Region, TraitPredicate, + TraitRef, TypeVisitableExt as _, TypingMode, Unnormalized, Upcast as _, elaborate, }; use tracing::{debug, instrument, trace}; @@ -942,9 +942,9 @@ where &mut self, goal: Goal, a_data: I::BoundExistentialPredicates, - a_region: I::Region, + a_region: Region, b_data: I::BoundExistentialPredicates, - b_region: I::Region, + b_region: Region, ) -> Vec> { let cx = self.cx(); let Goal { predicate: (a_ty, _b_ty), .. } = goal; @@ -990,7 +990,7 @@ where &mut self, goal: Goal, b_data: I::BoundExistentialPredicates, - b_region: I::Region, + b_region: Region, ) -> Result, NoSolution> { let cx = self.cx(); let Goal { predicate: (a_ty, _), .. } = goal; @@ -1032,9 +1032,9 @@ where goal: Goal, source: CandidateSource, a_data: I::BoundExistentialPredicates, - a_region: I::Region, + a_region: Region, b_data: I::BoundExistentialPredicates, - b_region: I::Region, + b_region: Region, upcast_principal: Option>>, ) -> Result, NoSolution> { let param_env = goal.param_env; diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 46a7e092e6bc9..991713034d866 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -17,8 +17,8 @@ use rustc_middle::bug; use rustc_middle::ty::layout::IntegerExt; use rustc_middle::ty::print::{Print, PrintError, Printer}; use rustc_middle::ty::{ - self, FloatTy, GenericArg, GenericArgKind, Instance, IntTy, ReifyReason, Ty, TyCtxt, - TypeVisitable, TypeVisitableExt, UintTy, Unnormalized, + self, FloatTy, GenericArg, GenericArgKind, Instance, IntTy, RegionUtilitiesExt, ReifyReason, + Ty, TyCtxt, TypeVisitable, TypeVisitableExt, UintTy, Unnormalized, }; use rustc_span::sym; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index 86fd705e68aea..6033d38605717 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -64,8 +64,8 @@ use rustc_middle::traits::PatternOriginExpr; use rustc_middle::ty::error::{ExpectedFound, TypeError, TypeErrorToStringExt}; use rustc_middle::ty::print::{PrintTraitRefExt as _, WrapBinderMode, with_forced_trimmed_paths}; use rustc_middle::ty::{ - self, List, ParamEnv, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable, - TypeVisitableExt, Unnormalized, + self, List, ParamEnv, Region, RegionUtilitiesExt, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, + TypeVisitable, TypeVisitableExt, Unnormalized, }; use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Pos, Span, sym}; use tracing::{debug, instrument}; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs index 711aab43d9e7b..4acae5c520d2b 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs @@ -6,7 +6,7 @@ use rustc_errors::{ErrorGuaranteed, MultiSpan}; use rustc_hir as hir; use rustc_hir::intravisit::VisitorExt; use rustc_middle::bug; -use rustc_middle::ty::TypeVisitor; +use rustc_middle::ty::{RegionUtilitiesExt, TypeVisitor}; use tracing::debug; use crate::error_reporting::infer::nice_region_error::NiceRegionError; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs index 73a04d78519c8..3bf952737d5ed 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs @@ -2,6 +2,7 @@ //! where one region is named and the other is anonymous. use rustc_errors::Diag; +use rustc_middle::ty::RegionUtilitiesExt; use tracing::debug; use crate::error_reporting::infer::nice_region_error::NiceRegionError; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs index 50dbae0a05d6f..37e1784866721 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs @@ -7,7 +7,9 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId}; use rustc_middle::bug; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::print::{FmtPrinter, Print, PrintTraitRefExt as _, RegionHighlightMode}; -use rustc_middle::ty::{self, GenericArgsRef, RePlaceholder, Region, TyCtxt}; +use rustc_middle::ty::{ + self, GenericArgsRef, RePlaceholder, Region, RegionExt, RegionUtilitiesExt, TyCtxt, +}; use tracing::{debug, instrument}; use crate::error_reporting::infer::nice_region_error::NiceRegionError; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs index b227cd065eab9..63d3f83cdb8a8 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs @@ -8,7 +8,7 @@ use rustc_hir::{ self as hir, AmbigArg, GenericBound, GenericParam, GenericParamKind, Item, ItemKind, Lifetime, LifetimeKind, LifetimeParamKind, MissingLifetimeKind, Node, TyKind, }; -use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor}; +use rustc_middle::ty::{self, RegionUtilitiesExt, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor}; use rustc_span::def_id::LocalDefId; use rustc_span::{Ident, Span}; use tracing::debug; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs index 7e6f566e242a3..fc441485e18ae 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs @@ -10,7 +10,7 @@ use rustc_middle::hir::nested_filter; use rustc_middle::traits::ObligationCauseCode; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::print::RegionHighlightMode; -use rustc_middle::ty::{self, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{self, RegionUtilitiesExt, TyCtxt, TypeVisitable}; use rustc_span::{Ident, Span}; use tracing::debug; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs index 2e9dcae700d2a..31c5d90a963e9 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs @@ -12,7 +12,7 @@ use rustc_middle::bug; use rustc_middle::traits::ObligationCauseCode; use rustc_middle::ty::error::TypeError; use rustc_middle::ty::{ - self, IsSuggestable, Region, Ty, TyCtxt, TypeVisitableExt as _, Upcast as _, + self, IsSuggestable, Region, RegionUtilitiesExt, Ty, TyCtxt, TypeVisitableExt as _, Upcast as _, }; use rustc_span::{BytePos, ErrorGuaranteed, Span, Symbol, kw, sym}; use tracing::{debug, instrument}; diff --git a/compiler/rustc_trait_selection/src/opaque_types.rs b/compiler/rustc_trait_selection/src/opaque_types.rs index 28b9bf21eee59..b46d84b5dc5b9 100644 --- a/compiler/rustc_trait_selection/src/opaque_types.rs +++ b/compiler/rustc_trait_selection/src/opaque_types.rs @@ -4,8 +4,8 @@ use rustc_hir::def_id::LocalDefId; use rustc_infer::infer::outlives::env::OutlivesEnvironment; use rustc_infer::infer::{InferCtxt, TyCtxtInferExt}; use rustc_middle::ty::{ - self, DefiningScopeKind, GenericArgKind, GenericArgs, OpaqueTypeKey, Ty, TyCtxt, - TypeVisitableExt, TypingMode, fold_regions, + self, DefiningScopeKind, GenericArgKind, GenericArgs, OpaqueTypeKey, RegionUtilitiesExt, Ty, + TyCtxt, TypeVisitableExt, TypingMode, fold_regions, }; use rustc_span::{ErrorGuaranteed, Span}; diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index f28e6f32b5f86..4556ed204fd6f 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -8,7 +8,7 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry}; use rustc_data_structures::unord::UnordSet; use rustc_hir::def_id::CRATE_DEF_ID; use rustc_infer::infer::DefineOpaqueTypes; -use rustc_middle::ty::{Region, RegionVid}; +use rustc_middle::ty::{Region, RegionUtilitiesExt, RegionVid}; use tracing::debug; use super::*; diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 61ed05d873dd5..70d9ec86667a7 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -20,8 +20,8 @@ use rustc_middle::traits::solve::{CandidateSource, Certainty, Goal}; use rustc_middle::traits::specialization_graph::OverlapMode; use rustc_middle::ty::fast_reject::DeepRejectCtxt; use rustc_middle::ty::{ - self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, - Unnormalized, + self, RegionUtilitiesExt, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, + TypeVisitor, TypingMode, Unnormalized, }; pub use rustc_next_trait_solver::coherence::*; use rustc_next_trait_solver::solve::SolverDelegateEvalExt; diff --git a/compiler/rustc_ty_utils/src/implied_bounds.rs b/compiler/rustc_ty_utils/src/implied_bounds.rs index 659229a58d539..4d02d383ba971 100644 --- a/compiler/rustc_ty_utils/src/implied_bounds.rs +++ b/compiler/rustc_ty_utils/src/implied_bounds.rs @@ -5,7 +5,7 @@ use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalDefId; use rustc_middle::query::Providers; -use rustc_middle::ty::{self, Ty, TyCtxt, Unnormalized, fold_regions}; +use rustc_middle::ty::{self, RegionExt, Ty, TyCtxt, Unnormalized, fold_regions}; use rustc_middle::{bug, span_bug}; use rustc_span::Span; diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index cb08ca0757876..a83b9de9e713a 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -6,8 +6,8 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_middle::bug; use rustc_middle::query::Providers; use rustc_middle::ty::{ - self, SizedTraitKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, Unnormalized, - Upcast, fold_regions, + self, RegionExt, SizedTraitKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, + Unnormalized, Upcast, fold_regions, }; use rustc_span::DUMMY_SP; use rustc_span::def_id::{CRATE_DEF_ID, DefId, LocalDefId}; diff --git a/compiler/rustc_type_ir/src/binder.rs b/compiler/rustc_type_ir/src/binder.rs index 2b0dc221844ec..6c0e503256d2e 100644 --- a/compiler/rustc_type_ir/src/binder.rs +++ b/compiler/rustc_type_ir/src/binder.rs @@ -16,7 +16,7 @@ use crate::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldabl use crate::inherent::*; use crate::lift::Lift; use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor}; -use crate::{self as ty, DebruijnIndex, Interner, UniverseIndex, Unnormalized}; +use crate::{self as ty, DebruijnIndex, Interner, Region, UniverseIndex, Unnormalized}; /// `Binder` is a binder for higher-ranked lifetimes or types. It is part of the /// compiler's representation for things like `for<'a> Fn(&'a isize)` @@ -339,7 +339,7 @@ impl TypeVisitor for ValidateBoundVars { c.super_visit_with(self) } - fn visit_region(&mut self, r: I::Region) -> Self::Result { + fn visit_region(&mut self, r: Region) -> Self::Result { match r.kind() { ty::ReBound(index, br) if index == ty::BoundVarIndexKind::Bound(self.binder_index) => { let idx = br.var().as_usize(); @@ -708,7 +708,7 @@ impl<'a, I: Interner> TypeFolder for ArgFolder<'a, I> { t } - fn fold_region(&mut self, r: I::Region) -> I::Region { + fn fold_region(&mut self, r: Region) -> Region { // Note: This routine only handles regions that are bound on // type declarations and other outer declarations, not those // bound in *fn types*. Region instantiation of the bound @@ -846,7 +846,7 @@ impl<'a, I: Interner> ArgFolder<'a, I> { fn region_param_expected( &self, ebr: I::EarlyParamRegion, - r: I::Region, + r: Region, kind: ty::GenericArgKind, ) -> ! { panic!( @@ -861,7 +861,7 @@ impl<'a, I: Interner> ArgFolder<'a, I> { #[cold] #[inline(never)] - fn region_param_out_of_range(&self, ebr: I::EarlyParamRegion, r: I::Region) -> ! { + fn region_param_out_of_range(&self, ebr: I::EarlyParamRegion, r: Region) -> ! { panic!( "region parameter `{:?}` ({:?}/{}) out of range when instantiating args={:?}", ebr, @@ -922,7 +922,7 @@ impl<'a, I: Interner> ArgFolder<'a, I> { } } - fn shift_region_through_binders(&self, region: I::Region) -> I::Region { + fn shift_region_through_binders(&self, region: Region) -> Region { if self.binders_passed == 0 || !region.has_escaping_bound_vars() { region } else { @@ -961,6 +961,14 @@ pub enum BoundVarIndexKind { Canonical, } +impl Lift for BoundVarIndexKind { + type Lifted = BoundVarIndexKind; + + fn lift_to_interner(self, _interner: U) -> Option { + Some(self) + } +} + /// The "placeholder index" fully defines a placeholder region, type, or const. Placeholders are /// identified by both a universe, as well as a name residing within that universe. Distinct bound /// regions/types/consts within the same universe simply have an unknown relationship to one diff --git a/compiler/rustc_type_ir/src/canonical.rs b/compiler/rustc_type_ir/src/canonical.rs index be5e483c808d4..1fa14ed2ed0dd 100644 --- a/compiler/rustc_type_ir/src/canonical.rs +++ b/compiler/rustc_type_ir/src/canonical.rs @@ -11,7 +11,7 @@ use rustc_type_ir_macros::{ use crate::data_structures::HashMap; use crate::inherent::*; -use crate::{self as ty, Interner, TypingModeEqWrapper, UniverseIndex}; +use crate::{self as ty, Interner, Region, TypingModeEqWrapper, UniverseIndex}; #[derive_where(Clone, Hash, PartialEq, Debug; I: Interner, V)] #[derive_where(Copy; I: Interner, V: Copy)] diff --git a/compiler/rustc_type_ir/src/elaborate.rs b/compiler/rustc_type_ir/src/elaborate.rs index be3661518d7d9..855b9534c0e12 100644 --- a/compiler/rustc_type_ir/src/elaborate.rs +++ b/compiler/rustc_type_ir/src/elaborate.rs @@ -6,7 +6,7 @@ use crate::data_structures::HashSet; use crate::inherent::*; use crate::lang_items::SolverTraitLangItem; use crate::outlives::{Component, push_outlives_components}; -use crate::{self as ty, Interner, Unnormalized, Upcast as _}; +use crate::{self as ty, Interner, Region, Unnormalized, Upcast as _}; /// "Elaboration" is the process of identifying all the predicates that /// are implied by a source predicate. Currently, this basically means @@ -249,7 +249,7 @@ impl> Elaborator { fn elaborate_component_to_clause( cx: I, component: Component, - outlives_region: I::Region, + outlives_region: Region, ) -> Option> { match component { Component::Region(r) => { diff --git a/compiler/rustc_type_ir/src/error.rs b/compiler/rustc_type_ir/src/error.rs index eba4c7c6644ac..d4c535dd93b2d 100644 --- a/compiler/rustc_type_ir/src/error.rs +++ b/compiler/rustc_type_ir/src/error.rs @@ -2,7 +2,7 @@ use derive_where::derive_where; use rustc_type_ir_macros::{GenericTypeVisitable, TypeFoldable_Generic, TypeVisitable_Generic}; use crate::solve::NoSolution; -use crate::{self as ty, Interner}; +use crate::{self as ty, Interner, Region}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(TypeFoldable_Generic, TypeVisitable_Generic, GenericTypeVisitable)] @@ -32,8 +32,8 @@ pub enum TypeError { ArraySize(ExpectedFound), ArgCount, - RegionsDoesNotOutlive(I::Region, I::Region), - RegionsInsufficientlyPolymorphic(ty::BoundRegion, I::Region), + RegionsDoesNotOutlive(Region, Region), + RegionsInsufficientlyPolymorphic(ty::BoundRegion, Region), RegionsPlaceholderMismatch, Sorts(ExpectedFound), diff --git a/compiler/rustc_type_ir/src/flags.rs b/compiler/rustc_type_ir/src/flags.rs index 50c30f4252703..9851c404d6c15 100644 --- a/compiler/rustc_type_ir/src/flags.rs +++ b/compiler/rustc_type_ir/src/flags.rs @@ -1,6 +1,6 @@ use crate::inherent::*; use crate::visit::Flags; -use crate::{self as ty, Interner}; +use crate::{self as ty, Interner, Region}; bitflags::bitflags! { /// Flags that we track on types. These flags are propagated upwards @@ -446,7 +446,7 @@ impl FlagComputation { } } - fn add_region(&mut self, r: I::Region) { + fn add_region(&mut self, r: Region) { self.add_flags(r.flags()); if let ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) = r.kind() { self.add_bound_var(debruijn); diff --git a/compiler/rustc_type_ir/src/fold.rs b/compiler/rustc_type_ir/src/fold.rs index d1a50599e8b9c..0fc87caec7dba 100644 --- a/compiler/rustc_type_ir/src/fold.rs +++ b/compiler/rustc_type_ir/src/fold.rs @@ -55,7 +55,7 @@ use tracing::{debug, instrument}; use crate::inherent::*; use crate::visit::{TypeVisitable, TypeVisitableExt as _}; -use crate::{self as ty, BoundVarIndexKind, Interner, TypeFlags}; +use crate::{self as ty, BoundVarIndexKind, Interner, Region, TypeFlags}; /// This trait is implemented for every type that can be folded, /// providing the skeleton of the traversal. @@ -141,7 +141,7 @@ pub trait TypeFolder: Sized { // The default region folder is a no-op because `Region` is non-recursive // and has no `super_fold_with` method to call. - fn fold_region(&mut self, r: I::Region) -> I::Region { + fn fold_region(&mut self, r: Region) -> Region { r } @@ -183,7 +183,7 @@ pub trait FallibleTypeFolder: Sized { // The default region folder is a no-op because `Region` is non-recursive // and has no `super_fold_with` method to call. - fn try_fold_region(&mut self, r: I::Region) -> Result { + fn try_fold_region(&mut self, r: Region) -> Result, Self::Error> { Ok(r) } @@ -396,7 +396,7 @@ impl TypeFolder for Shifter { t } - fn fold_region(&mut self, r: I::Region) -> I::Region { + fn fold_region(&mut self, r: Region) -> Region { match r.kind() { ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), br) if debruijn >= self.current_index => @@ -439,7 +439,7 @@ impl TypeFolder for Shifter { } } -pub fn shift_region(cx: I, region: I::Region, amount: u32) -> I::Region { +pub fn shift_region(cx: I, region: Region, amount: u32) -> Region { match region.kind() { ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), br) if amount > 0 => { Region::new_bound(cx, debruijn.shifted_in(amount), br) @@ -466,7 +466,7 @@ where pub fn fold_regions( cx: I, value: T, - f: impl FnMut(I::Region, ty::DebruijnIndex) -> I::Region, + f: impl FnMut(Region, ty::DebruijnIndex) -> Region, ) -> T where T: TypeFoldable, @@ -505,7 +505,7 @@ impl RegionFolder { impl TypeFolder for RegionFolder where I: Interner, - F: FnMut(I::Region, ty::DebruijnIndex) -> I::Region, + F: FnMut(Region, ty::DebruijnIndex) -> Region, { fn cx(&self) -> I { self.cx @@ -519,7 +519,7 @@ where } #[instrument(skip(self), level = "debug", ret)] - fn fold_region(&mut self, r: I::Region) -> I::Region { + fn fold_region(&mut self, r: Region) -> Region { match r.kind() { ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) if debruijn < self.current_index => diff --git a/compiler/rustc_type_ir/src/generic_arg.rs b/compiler/rustc_type_ir/src/generic_arg.rs index 5d612740fdd84..dfdd308432d86 100644 --- a/compiler/rustc_type_ir/src/generic_arg.rs +++ b/compiler/rustc_type_ir/src/generic_arg.rs @@ -3,7 +3,7 @@ use derive_where::derive_where; use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; use rustc_type_ir_macros::GenericTypeVisitable; -use crate::Interner; +use crate::{Interner, Region}; #[derive_where(Clone, Copy, PartialEq, Debug; I: Interner)] #[derive(GenericTypeVisitable)] @@ -12,7 +12,7 @@ use crate::Interner; derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext) )] pub enum GenericArgKind { - Lifetime(I::Region), + Lifetime(Region), Type(I::Ty), Const(I::Const), } diff --git a/compiler/rustc_type_ir/src/infer_ctxt.rs b/compiler/rustc_type_ir/src/infer_ctxt.rs index 905b005cd48f8..f95070c1df248 100644 --- a/compiler/rustc_type_ir/src/infer_ctxt.rs +++ b/compiler/rustc_type_ir/src/infer_ctxt.rs @@ -8,7 +8,7 @@ use crate::fold::TypeFoldable; use crate::inherent::*; use crate::relate::RelateResult; use crate::relate::combine::PredicateEmittingRelation; -use crate::{self as ty, Interner, TyVid}; +use crate::{self as ty, Interner, Region, TyVid}; /// The current typing mode of an inference context. We unfortunately have some /// slightly different typing rules depending on the current context. See the @@ -252,14 +252,11 @@ pub trait InferCtxtLike: Sized { &self, vid: ty::ConstVid, ) -> ::Const; - fn opportunistic_resolve_lt_var( - &self, - vid: ty::RegionVid, - ) -> ::Region; + fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> Region; fn is_changed_arg(&self, arg: ::GenericArg) -> bool; - fn next_region_infer(&self) -> ::Region; + fn next_region_infer(&self) -> Region; fn next_ty_infer(&self) -> ::Ty; fn next_const_infer(&self) -> ::Const; fn fresh_args_for_item( @@ -321,22 +318,22 @@ pub trait InferCtxtLike: Sized { fn sub_regions( &self, - sub: ::Region, - sup: ::Region, + sub: Region, + sup: Region, span: ::Span, ); fn equate_regions( &self, - a: ::Region, - b: ::Region, + a: Region, + b: Region, span: ::Span, ); fn register_ty_outlives( &self, ty: ::Ty, - r: ::Region, + r: Region, span: ::Span, ); diff --git a/compiler/rustc_type_ir/src/inherent.rs b/compiler/rustc_type_ir/src/inherent.rs index cf94dc4298958..5e083d3a821df 100644 --- a/compiler/rustc_type_ir/src/inherent.rs +++ b/compiler/rustc_type_ir/src/inherent.rs @@ -14,7 +14,7 @@ use crate::relate::Relate; use crate::solve::{AdtDestructorKind, SizedTraitKind}; use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable}; use crate::{ - self as ty, ClauseKind, CollectAndApply, FieldInfo, Interner, PredicateKind, UpcastFrom, + self as ty, ClauseKind, CollectAndApply, FieldInfo, Interner, PredicateKind, Region, UpcastFrom, }; pub trait Ty>: @@ -78,7 +78,7 @@ pub trait Ty>: fn new_foreign(interner: I, def_id: I::ForeignId) -> Self; - fn new_dynamic(interner: I, preds: I::BoundExistentialPredicates, region: I::Region) -> Self; + fn new_dynamic(interner: I, preds: I::BoundExistentialPredicates, region: Region) -> Self; fn new_coroutine(interner: I, def_id: I::CoroutineId, args: I::GenericArgs) -> Self; @@ -100,7 +100,7 @@ pub trait Ty>: fn new_ptr(interner: I, ty: Self, mutbl: Mutability) -> Self; - fn new_ref(interner: I, region: I::Region, ty: Self, mutbl: Mutability) -> Self; + fn new_ref(interner: I, region: Region, ty: Self, mutbl: Mutability) -> Self; fn new_array_with_const_len(interner: I, ty: Self, len: I::Const) -> Self; @@ -251,31 +251,6 @@ pub trait Safety>: Copy + Debug + Hash + Eq { fn prefix_str(self) -> &'static str; } -pub trait Region>: - Copy - + Debug - + Hash - + Eq - + Into - + IntoKind> - + Flags - + Relate -{ - fn new_bound(interner: I, debruijn: ty::DebruijnIndex, var: ty::BoundRegion) -> Self; - - fn new_anon_bound(interner: I, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self; - - fn new_canonical_bound(interner: I, var: ty::BoundVar) -> Self; - - fn new_static(interner: I) -> Self; - - fn new_placeholder(interner: I, var: ty::PlaceholderRegion) -> Self; - - fn is_bound(self) -> bool { - matches!(self.kind(), ty::ReBound(..)) - } -} - pub trait Const>: Copy + Debug @@ -342,7 +317,7 @@ pub trait GenericArg>: + TypeVisitable + Relate + From - + From + + From> + From + From { @@ -370,11 +345,11 @@ pub trait GenericArg>: self.as_const().expect("expected a const") } - fn as_region(&self) -> Option { + fn as_region(&self) -> Option> { if let ty::GenericArgKind::Lifetime(c) = self.kind() { Some(c) } else { None } } - fn expect_region(&self) -> I::Region { + fn expect_region(&self) -> Region { self.as_region().expect("expected a const") } @@ -446,7 +421,7 @@ pub trait GenericArgs>: fn type_at(self, i: usize) -> I::Ty; - fn region_at(self, i: usize) -> I::Region; + fn region_at(self, i: usize) -> Region; fn const_at(self, i: usize) -> I::Const; @@ -491,7 +466,7 @@ pub trait Predicate>: + UpcastFrom>> + UpcastFrom> + UpcastFrom> - + UpcastFrom> + + UpcastFrom>> + IntoKind>> + Elaboratable { diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index f71f7c7c1ab38..87b070b39a354 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -5,6 +5,8 @@ use std::ops::Deref; use rustc_ast_ir::Movability; use rustc_index::bit_set::DenseBitSet; +#[cfg(feature = "nightly")] +use rustc_serialize::Decoder; use crate::fold::TypeFoldable; use crate::inherent::*; @@ -13,7 +15,10 @@ use crate::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem}; use crate::relate::Relate; use crate::solve::{CanonicalInput, Certainty, ExternalConstraintsData, QueryResult, inspect}; use crate::visit::{Flags, TypeVisitable}; -use crate::{self as ty, CanonicalParamEnvCacheEntry, search_graph}; +use crate::{ + self as ty, BoundRegion, BoundVar, CanonicalParamEnvCacheEntry, DebruijnIndex, Region, + RegionKind, search_graph, +}; #[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_interner")] pub trait Interner: @@ -159,10 +164,16 @@ pub trait Interner: type ScalarInt: Copy + Debug + Hash + Eq; // Kinds of regions - type Region: Region; type EarlyParamRegion: ParamLike; type LateParamRegion: Copy + Debug + Hash + Eq; + type InternedRegionKind: Copy + + Debug + + Hash + + Eq + + PartialEq + + IntoKind>; + type RegionAssumptions: Copy + Debug + Hash @@ -416,6 +427,30 @@ pub trait Interner: ) -> (QueryResult, Self::Probe); fn item_name(self, item_index: Self::DefId) -> Self::Symbol; + + fn get_anon_re_bounds_lifetime(self, idx: usize, var_idx: usize) -> Option>; + + fn get_anon_re_canonical_bounds_lifetime(self, idx: usize) -> Option>; + + fn get_re_static_lifetime(self) -> Region; + + fn intern_region(self, region_kind: RegionKind) -> Region; + + fn intern_bound_region( + self, + debruijn: DebruijnIndex, + bound_region: BoundRegion, + ) -> Region; + + fn intern_canonical_bound(self, var: BoundVar) -> Region; +} + +/// A decoder that can reconstruct interned IR values by supplying an interner. +#[cfg(feature = "nightly")] +pub trait InternerDecoder: Decoder { + type Interner: Interner; + + fn interner(&self) -> Self::Interner; } /// Imagine you have a function `F: FnOnce(&[T]) -> R`, plus an iterator `iter` diff --git a/compiler/rustc_type_ir/src/ir_print.rs b/compiler/rustc_type_ir/src/ir_print.rs index 5af2bd811bab4..952571c903aa8 100644 --- a/compiler/rustc_type_ir/src/ir_print.rs +++ b/compiler/rustc_type_ir/src/ir_print.rs @@ -3,8 +3,8 @@ use std::fmt; use crate::{ AliasTerm, AliasTy, Binder, ClosureKind, CoercePredicate, ExistentialProjection, ExistentialTraitRef, FnSig, HostEffectPredicate, Interner, NormalizesTo, OutlivesPredicate, - PatternKind, Placeholder, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef, - UnevaluatedConst, + PatternKind, Placeholder, ProjectionPredicate, Region, SubtypePredicate, TraitPredicate, + TraitRef, UnevaluatedConst, }; pub trait IrPrint { @@ -54,6 +54,15 @@ define_display_via_print!( define_debug_via_print!(TraitRef, ExistentialTraitRef, PatternKind); +impl fmt::Display for Region +where + I: IrPrint>, +{ + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + >>::print(self, fmt) + } +} + impl fmt::Display for OutlivesPredicate where I: IrPrint>, @@ -99,6 +108,12 @@ mod into_diag_arg_impls { } } + impl>> IntoDiagArg for Region { + fn into_diag_arg(self, path: &mut Option) -> DiagArgValue { + self.to_string().into_diag_arg(path) + } + } + impl IntoDiagArg for UnevaluatedConst { fn into_diag_arg(self, path: &mut Option) -> DiagArgValue { format!("{self:?}").into_diag_arg(path) diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 8f314ab86da24..ae5bf4ccedd9e 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -5,7 +5,7 @@ #![allow(rustc::usage_of_type_ir_inherent)] #![allow(rustc::usage_of_type_ir_traits)] #![cfg_attr(feature = "nightly", allow(internal_features))] -#![cfg_attr(feature = "nightly", feature(associated_type_defaults, rustc_attrs, negative_impls))] +#![cfg_attr(feature = "nightly", feature(associated_type_defaults, negative_impls, rustc_attrs))] // tidy-alphabetical-end extern crate self as rustc_type_ir; @@ -31,6 +31,7 @@ pub mod outlives; pub mod relate; pub mod search_graph; pub mod solve; +pub mod sty; pub mod walk; // These modules are not `pub` since they are glob-imported. @@ -79,6 +80,7 @@ pub use predicate_kind::*; pub use region_kind::*; pub use rustc_ast_ir::{FloatTy, IntTy, Movability, Mutability, Pinnedness, UintTy}; use rustc_type_ir_macros::GenericTypeVisitable; +pub use sty::*; pub use ty_info::*; pub use ty_kind::*; pub use unnormalized::Unnormalized; diff --git a/compiler/rustc_type_ir/src/opaque_ty.rs b/compiler/rustc_type_ir/src/opaque_ty.rs index 870e15af519d6..4909511d9e87f 100644 --- a/compiler/rustc_type_ir/src/opaque_ty.rs +++ b/compiler/rustc_type_ir/src/opaque_ty.rs @@ -4,7 +4,7 @@ use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContex use rustc_type_ir_macros::{GenericTypeVisitable, TypeFoldable_Generic, TypeVisitable_Generic}; use crate::inherent::*; -use crate::{self as ty, Interner}; +use crate::{self as ty, Interner, Region}; #[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner)] #[derive(TypeVisitable_Generic, GenericTypeVisitable, TypeFoldable_Generic)] @@ -34,7 +34,7 @@ impl OpaqueTypeKey { pub fn fold_captured_lifetime_args( self, cx: I, - mut f: impl FnMut(I::Region) -> I::Region, + mut f: impl FnMut(Region) -> Region, ) -> Self { let Self { def_id, args } = self; let variances = cx.variances_of(def_id.into()); diff --git a/compiler/rustc_type_ir/src/outlives.rs b/compiler/rustc_type_ir/src/outlives.rs index 5b4e44dc89ebe..0f3eb58397ac0 100644 --- a/compiler/rustc_type_ir/src/outlives.rs +++ b/compiler/rustc_type_ir/src/outlives.rs @@ -8,11 +8,11 @@ use smallvec::{SmallVec, smallvec}; use crate::data_structures::SsoHashSet; use crate::inherent::*; use crate::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt as _, TypeVisitor}; -use crate::{self as ty, Interner}; +use crate::{self as ty, Interner, Region}; #[derive_where(Debug; I: Interner)] pub enum Component { - Region(I::Region), + Region(Region), Param(I::ParamTy), Placeholder(ty::PlaceholderType), UnresolvedInferenceVariable(ty::InferTy), @@ -210,7 +210,7 @@ impl TypeVisitor for OutlivesCollector<'_, I> { } } - fn visit_region(&mut self, lt: I::Region) -> Self::Result { + fn visit_region(&mut self, lt: Region) -> Self::Result { if !lt.is_bound() { self.out.push(Component::Region(lt)); } diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs index e480b089b655c..4171bf81f6704 100644 --- a/compiler/rustc_type_ir/src/predicate.rs +++ b/compiler/rustc_type_ir/src/predicate.rs @@ -14,7 +14,7 @@ use crate::inherent::*; use crate::lift::Lift; use crate::upcast::{Upcast, UpcastFrom}; use crate::visit::TypeVisitableExt as _; -use crate::{self as ty, AliasTyKind, Interner}; +use crate::{self as ty, AliasTyKind, Interner, Region}; /// `A: 'region` #[derive_where(Clone, Hash, PartialEq, Debug; I: Interner, A)] @@ -24,7 +24,7 @@ use crate::{self as ty, AliasTyKind, Interner}; feature = "nightly", derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext) )] -pub struct OutlivesPredicate(pub A, pub I::Region); +pub struct OutlivesPredicate(pub A, pub Region); impl Eq for OutlivesPredicate {} @@ -33,7 +33,7 @@ impl Eq for OutlivesPredicate {} impl Lift for OutlivesPredicate where A: Lift, - I::Region: Lift, + Region: Lift>, { type Lifted = OutlivesPredicate; @@ -52,7 +52,7 @@ where feature = "nightly", derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext) )] -pub struct RegionEqPredicate(pub I::Region, pub I::Region); +pub struct RegionEqPredicate(pub Region, pub Region); impl RegionEqPredicate { /// Decompose `'a == 'b` into `['a: 'b, 'b: 'a]` diff --git a/compiler/rustc_type_ir/src/predicate_kind.rs b/compiler/rustc_type_ir/src/predicate_kind.rs index 445e85a888fca..f17bb3cccfe48 100644 --- a/compiler/rustc_type_ir/src/predicate_kind.rs +++ b/compiler/rustc_type_ir/src/predicate_kind.rs @@ -5,7 +5,7 @@ use derive_where::derive_where; use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; use rustc_type_ir_macros::{GenericTypeVisitable, TypeFoldable_Generic, TypeVisitable_Generic}; -use crate::{self as ty, Interner}; +use crate::{self as ty, Interner, Region}; /// A clause is something that can appear in where bounds or be inferred /// by implied bounds. @@ -22,7 +22,7 @@ pub enum ClauseKind { Trait(ty::TraitPredicate), /// `where 'a: 'r` - RegionOutlives(ty::OutlivesPredicate), + RegionOutlives(ty::OutlivesPredicate>), /// `where T: 'r` TypeOutlives(ty::OutlivesPredicate), diff --git a/compiler/rustc_type_ir/src/relate.rs b/compiler/rustc_type_ir/src/relate.rs index 2ae8d52db6f5e..bbbba3aa0dcc0 100644 --- a/compiler/rustc_type_ir/src/relate.rs +++ b/compiler/rustc_type_ir/src/relate.rs @@ -7,7 +7,7 @@ use tracing::{instrument, trace}; use crate::error::{ExpectedFound, TypeError}; use crate::fold::TypeFoldable; use crate::inherent::*; -use crate::{self as ty, Interner}; +use crate::{self as ty, Interner, Region}; pub mod combine; pub mod solver_relating; @@ -100,7 +100,7 @@ pub trait TypeRelation: Sized { fn tys(&mut self, a: I::Ty, b: I::Ty) -> RelateResult; - fn regions(&mut self, a: I::Region, b: I::Region) -> RelateResult; + fn regions(&mut self, a: Region, b: Region) -> RelateResult>; fn consts(&mut self, a: I::Const, b: I::Const) -> RelateResult; diff --git a/compiler/rustc_type_ir/src/relate/solver_relating.rs b/compiler/rustc_type_ir/src/relate/solver_relating.rs index 82ee4f75fcb0a..712be3c87a46c 100644 --- a/compiler/rustc_type_ir/src/relate/solver_relating.rs +++ b/compiler/rustc_type_ir/src/relate/solver_relating.rs @@ -5,7 +5,7 @@ use crate::data_structures::DelayedSet; use crate::relate::combine::combine_ty_args; pub use crate::relate::*; use crate::solve::Goal; -use crate::{self as ty, InferCtxtLike, Interner}; +use crate::{self as ty, InferCtxtLike, Interner, Region}; pub trait RelateExt: InferCtxtLike { fn relate>( @@ -253,7 +253,7 @@ where } #[instrument(skip(self), level = "trace")] - fn regions(&mut self, a: I::Region, b: I::Region) -> RelateResult { + fn regions(&mut self, a: Region, b: Region) -> RelateResult> { match self.ambient_variance { // Subtype(&'a u8, &'b u8) => Outlives('a: 'b) => SubRegion('b, 'a) ty::Covariant => self.infcx.sub_regions(b, a, self.span), diff --git a/compiler/rustc_type_ir/src/sty/mod.rs b/compiler/rustc_type_ir/src/sty/mod.rs new file mode 100644 index 0000000000000..7822f3d86ec75 --- /dev/null +++ b/compiler/rustc_type_ir/src/sty/mod.rs @@ -0,0 +1,191 @@ +use std::fmt; + +use derive_where::derive_where; +#[cfg(feature = "nightly")] +use rustc_data_structures::intern::Interned; +#[cfg(feature = "nightly")] +use rustc_macros::HashStable_NoContext; +#[cfg(feature = "nightly")] +use rustc_serialize::{Decodable, Encodable}; +use tracing::debug; + +use crate::inherent::*; +use crate::relate::{Relate, RelateResult, TypeRelation}; +use crate::{ + BoundRegion, BoundRegionKind, BoundVar, BoundVarIndexKind, DebruijnIndex, FallibleTypeFolder, + Flags, Interner, PlaceholderRegion, RegionKind, TypeFlags, TypeFoldable, TypeFolder, + TypeVisitable, TypeVisitor, +}; + +/// Use this rather than `RegionKind`, whenever possible. +#[derive_where(Clone, Copy, PartialEq, Eq, Hash; I: Interner)] +#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))] +#[cfg_attr(feature = "nightly", rustc_pass_by_value)] +pub struct Region(pub I::InternedRegionKind); + +// These are only the `inherent` trait methods that have been ported across +impl Region { + #[inline] + pub fn new_bound(interner: I, debruijn: DebruijnIndex, bound_region: BoundRegion) -> Self { + interner.intern_bound_region(debruijn, bound_region) + } + + #[inline] + pub fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self { + Self::new_bound(interner, debruijn, BoundRegion { var, kind: BoundRegionKind::Anon }) + } + + #[inline] + pub fn new_canonical_bound(interner: I, var: BoundVar) -> Self { + interner.intern_canonical_bound(var) + } + + #[inline] + pub fn new_placeholder(interner: I, placeholder: PlaceholderRegion) -> Self { + interner.intern_region(RegionKind::RePlaceholder(placeholder)) + } + + #[inline] + pub fn new_static(interner: I) -> Self { + interner.get_re_static_lifetime() + } + + #[inline] + pub fn is_bound(self) -> bool { + matches!(self.0.kind(), RegionKind::ReBound(..)) + } + + #[inline] + pub fn type_flags(self) -> TypeFlags { + let mut flags = TypeFlags::empty(); + + match self.0.kind() { + RegionKind::ReVar(..) => { + flags = flags | TypeFlags::HAS_FREE_REGIONS; + flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS; + flags = flags | TypeFlags::HAS_RE_INFER; + } + RegionKind::RePlaceholder(..) => { + flags = flags | TypeFlags::HAS_FREE_REGIONS; + flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS; + flags = flags | TypeFlags::HAS_RE_PLACEHOLDER; + } + RegionKind::ReEarlyParam(..) => { + flags = flags | TypeFlags::HAS_FREE_REGIONS; + flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS; + flags = flags | TypeFlags::HAS_RE_PARAM; + } + RegionKind::ReLateParam { .. } => { + flags = flags | TypeFlags::HAS_FREE_REGIONS; + flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS; + } + RegionKind::ReStatic => { + flags = flags | TypeFlags::HAS_FREE_REGIONS; + } + RegionKind::ReBound(BoundVarIndexKind::Canonical, _) => { + flags = flags | TypeFlags::HAS_RE_BOUND; + flags = flags | TypeFlags::HAS_CANONICAL_BOUND; + } + RegionKind::ReBound(BoundVarIndexKind::Bound(..), _) => { + flags = flags | TypeFlags::HAS_RE_BOUND; + } + RegionKind::ReErased => { + flags = flags | TypeFlags::HAS_RE_ERASED; + } + RegionKind::ReError(_) => { + flags = flags | TypeFlags::HAS_FREE_REGIONS; + flags = flags | TypeFlags::HAS_RE_ERROR; + } + } + debug!("type_flags({:?}) = {:?}", self, flags); + + flags + } + + #[inline] + pub fn kind(self) -> RegionKind { + self.0.kind() + } +} + +impl Flags for Region { + fn flags(&self) -> TypeFlags { + self.type_flags() + } + + fn outer_exclusive_binder(&self) -> DebruijnIndex { + match self.kind() { + RegionKind::ReBound(BoundVarIndexKind::Bound(debruijn), _) => debruijn.shifted_in(1), + _ => crate::INNERMOST, + } + } +} + +impl IntoKind for Region { + type Kind = RegionKind; + + fn kind(self) -> Self::Kind { + self.0.kind() + } +} + +#[cfg(feature = "nightly")] +impl<'tcx, T: Copy> IntoKind for Interned<'tcx, T> { + type Kind = T; + + fn kind(self) -> Self::Kind { + *self.0 + } +} + +impl Relate for Region { + fn relate>( + relation: &mut R, + a: Region, + b: Region, + ) -> RelateResult> { + relation.regions(a, b) + } +} + +impl fmt::Debug for Region { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self.kind()) + } +} + +impl TypeVisitable for Region { + fn visit_with>(&self, visitor: &mut V) -> V::Result { + visitor.visit_region(*self) + } +} + +impl TypeFoldable for Region { + fn try_fold_with>(self, folder: &mut F) -> Result { + folder.try_fold_region(self) + } + + fn fold_with>(self, folder: &mut F) -> Self { + folder.fold_region(self) + } +} + +#[cfg(feature = "nightly")] +impl Encodable for Region +where + RegionKind: Encodable, +{ + fn encode(&self, e: &mut E) { + self.kind().encode(e); + } +} + +#[cfg(feature = "nightly")] +impl> Decodable for Region +where + RegionKind: Decodable, +{ + fn decode(decoder: &mut D) -> Self { + decoder.interner().intern_region(Decodable::decode(decoder)) + } +} diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 61ac5acca7405..86b7650615f30 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -18,7 +18,7 @@ pub use self::closure::*; use crate::inherent::*; #[cfg(feature = "nightly")] use crate::visit::TypeVisitable; -use crate::{self as ty, BoundVarIndexKind, FloatTy, IntTy, Interner, UintTy}; +use crate::{self as ty, BoundVarIndexKind, FloatTy, IntTy, Interner, Region, UintTy}; mod closure; @@ -150,7 +150,7 @@ pub enum TyKind { /// A reference; a pointer with an associated lifetime. Written as /// `&'a mut T` or `&'a T`. - Ref(I::Region, I::Ty, Mutability), + Ref(Region, I::Ty, Mutability), /// The anonymous type of a function declaration/definition. /// @@ -194,7 +194,7 @@ pub enum TyKind { UnsafeBinder(UnsafeBinderInner), /// A trait object. Written as `dyn for<'b> Trait<'b, Assoc = u32> + Send + 'a`. - Dynamic(I::BoundExistentialPredicates, I::Region), + Dynamic(I::BoundExistentialPredicates, Region), /// The anonymous type of a closure. Used to represent the type of `|a| a`. /// diff --git a/compiler/rustc_type_ir/src/ty_kind/closure.rs b/compiler/rustc_type_ir/src/ty_kind/closure.rs index 1ad5ed45e8b10..e5ab67220a8c6 100644 --- a/compiler/rustc_type_ir/src/ty_kind/closure.rs +++ b/compiler/rustc_type_ir/src/ty_kind/closure.rs @@ -9,7 +9,7 @@ use crate::data_structures::DelayedMap; use crate::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable, shift_region}; use crate::inherent::*; use crate::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor}; -use crate::{self as ty, Interner}; +use crate::{self as ty, Interner, Region}; /// A closure can be modeled as a struct that looks like: /// ```ignore (illustrative) @@ -343,7 +343,7 @@ impl TypeVisitor for HasRegionsBoundAt { ControlFlow::Continue(()) } - fn visit_region(&mut self, r: I::Region) -> Self::Result { + fn visit_region(&mut self, r: Region) -> Self::Result { if matches!(r.kind(), ty::ReBound(ty::BoundVarIndexKind::Bound(binder), _) if self.binder == binder) { ControlFlow::Break(()) @@ -415,7 +415,7 @@ impl CoroutineClosureSignature { parent_args: I::GenericArgsSlice, coroutine_def_id: I::CoroutineId, goal_kind: ty::ClosureKind, - env_region: I::Region, + env_region: Region, closure_tupled_upvars_ty: I::Ty, coroutine_captures_by_ref_ty: I::Ty, ) -> I::Ty { @@ -452,7 +452,7 @@ impl CoroutineClosureSignature { tupled_inputs_ty: I::Ty, closure_tupled_upvars_ty: I::Ty, coroutine_captures_by_ref_ty: I::Ty, - env_region: I::Region, + env_region: Region, ) -> I::Ty { match kind { ty::ClosureKind::Fn | ty::ClosureKind::FnMut => { @@ -491,7 +491,7 @@ impl CoroutineClosureSignature { struct FoldEscapingRegions { interner: I, debruijn: ty::DebruijnIndex, - region: I::Region, + region: Region, // Depends on `debruijn` because we may have types with regions of different // debruijn depths depending on the binders we've entered. @@ -525,7 +525,7 @@ impl TypeFolder for FoldEscapingRegions { result } - fn fold_region(&mut self, r: ::Region) -> ::Region { + fn fold_region(&mut self, r: Region) -> Region { if let ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) = r.kind() { assert!( debruijn <= self.debruijn, diff --git a/compiler/rustc_type_ir/src/visit.rs b/compiler/rustc_type_ir/src/visit.rs index a078b860be774..e99df9bcb7559 100644 --- a/compiler/rustc_type_ir/src/visit.rs +++ b/compiler/rustc_type_ir/src/visit.rs @@ -52,7 +52,7 @@ use smallvec::SmallVec; use thin_vec::ThinVec; use crate::inherent::*; -use crate::{self as ty, Interner, TypeFlags}; +use crate::{self as ty, Interner, Region, TypeFlags}; /// This trait is implemented for every type that can be visited, /// providing the skeleton of the traversal. @@ -104,7 +104,7 @@ pub trait TypeVisitor: Sized { // The default region visitor is a no-op because `Region` is non-recursive // and has no `super_visit_with` method to call. - fn visit_region(&mut self, r: I::Region) -> Self::Result { + fn visit_region(&mut self, r: Region) -> Self::Result { if let ty::ReError(guar) = r.kind() { self.visit_error(guar) } else { @@ -447,7 +447,7 @@ impl TypeVisitor for HasTypeFlagsVisitor { } #[inline] - fn visit_region(&mut self, r: I::Region) -> Self::Result { + fn visit_region(&mut self, r: Region) -> Self::Result { // Note: no `super_visit_with` call, as usual for `Region`. let flags = r.flags(); if flags.intersects(self.flags) { @@ -555,7 +555,7 @@ impl TypeVisitor for HasEscapingVarsVisitor { } #[inline] - fn visit_region(&mut self, r: I::Region) -> Self::Result { + fn visit_region(&mut self, r: Region) -> Self::Result { // If the region is bound by `outer_index` or anything outside // of outer index, then it escapes the binders we have // visited. diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 554b81b14cd3d..562f1f2dc3831 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -3,7 +3,7 @@ use rustc_data_structures::thin_vec::ThinVec; use rustc_hir as hir; use rustc_infer::infer::region_constraints::{ConstraintKind, RegionConstraintData}; use rustc_middle::bug; -use rustc_middle::ty::{self, Region, Ty, fold_regions}; +use rustc_middle::ty::{self, Region, RegionUtilitiesExt, Ty, fold_regions}; use rustc_span::def_id::DefId; use rustc_span::symbol::{Symbol, kw}; use rustc_trait_selection::traits::auto_trait::{self, RegionTarget}; diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 345f13dae3224..dfad5638c65a7 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -47,7 +47,8 @@ use rustc_hir_analysis::{lower_const_arg_for_rustdoc, lower_ty}; use rustc_middle::metadata::Reexport; use rustc_middle::middle::resolve_bound_vars as rbv; use rustc_middle::ty::{ - self, AdtKind, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized, + self, AdtKind, GenericArgsRef, RegionUtilitiesExt, Ty, TyCtxt, TypeVisitableExt, TypingMode, + Unnormalized, }; use rustc_middle::{bug, span_bug}; use rustc_span::ExpnKind; diff --git a/src/tools/clippy/clippy_lints/src/eta_reduction.rs b/src/tools/clippy/clippy_lints/src/eta_reduction.rs index 229af104799d1..0e37a39be7a87 100644 --- a/src/tools/clippy/clippy_lints/src/eta_reduction.rs +++ b/src/tools/clippy/clippy_lints/src/eta_reduction.rs @@ -10,6 +10,7 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::adjustment::{Adjust, DerefAdjustKind}; use rustc_middle::ty::{ + RegionUtilitiesExt, self, Binder, ClosureKind, FnSig, GenericArg, GenericArgKind, List, Region, Ty, TypeVisitableExt, TypeckResults, }; use rustc_session::declare_lint_pass; diff --git a/src/tools/clippy/clippy_lints/src/operators/identity_op.rs b/src/tools/clippy/clippy_lints/src/operators/identity_op.rs index 832b12712f83f..17c084aaf1fe5 100644 --- a/src/tools/clippy/clippy_lints/src/operators/identity_op.rs +++ b/src/tools/clippy/clippy_lints/src/operators/identity_op.rs @@ -7,6 +7,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::{BinOpKind, Expr, ExprKind, Node, Path, QPath}; use rustc_lint::LateContext; use rustc_middle::ty; +use rustc_middle::ty::RegionUtilitiesExt; use rustc_span::{Span, SyntaxContext, kw}; use super::IDENTITY_OP; diff --git a/src/tools/clippy/clippy_lints/src/returns/let_and_return.rs b/src/tools/clippy/clippy_lints/src/returns/let_and_return.rs index 2ec921ed21c7d..829dd74ea43fc 100644 --- a/src/tools/clippy/clippy_lints/src/returns/let_and_return.rs +++ b/src/tools/clippy/clippy_lints/src/returns/let_and_return.rs @@ -8,7 +8,7 @@ use core::ops::ControlFlow; use rustc_errors::Applicability; use rustc_hir::{Block, Expr, PatKind, StmtKind}; use rustc_lint::{LateContext, LintContext}; -use rustc_middle::ty::GenericArgKind; +use rustc_middle::ty::{GenericArgKind, RegionUtilitiesExt}; use rustc_span::edition::Edition; use super::LET_AND_RETURN; diff --git a/src/tools/clippy/clippy_utils/src/lib.rs b/src/tools/clippy/clippy_utils/src/lib.rs index e5420f9f9d166..8bb68574d5f8a 100644 --- a/src/tools/clippy/clippy_utils/src/lib.rs +++ b/src/tools/clippy/clippy_utils/src/lib.rs @@ -109,6 +109,7 @@ use rustc_middle::mir::{AggregateKind, Operand, RETURN_PLACE, Rvalue, StatementK use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, DerefAdjustKind, PointerCoercion}; use rustc_middle::ty::layout::IntegerExt; use rustc_middle::ty::{ + RegionUtilitiesExt, self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, GenericArgKind, GenericArgsRef, IntTy, Ty, TyCtxt, TypeFlags, TypeVisitableExt, TypeckResults, UintTy, UpvarCapture, };