Skip to content

Move rustc_middle::parameterized #144717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rustc_data_structures::owned_slice::OwnedSlice;
use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
use rustc_middle::parameterized_over_tcx;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::def_id::{DefIndex, DefPathHash};

Expand All @@ -11,10 +10,6 @@ pub(crate) enum DefPathHashMapRef<'tcx> {
BorrowedFromTcx(&'tcx DefPathHashMap),
}

parameterized_over_tcx! {
DefPathHashMapRef,
}

impl DefPathHashMapRef<'_> {
#[inline]
pub(crate) fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
Expand Down
31 changes: 4 additions & 27 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use decoder::{DecodeContext, Metadata};
use def_path_hash_map::DefPathHashMapRef;
use encoder::EncodeContext;
pub use encoder::{EncodedMetadata, encode_metadata, rendered_const};
pub(crate) use parameterized::ParameterizedOverTcx;
use rustc_abi::{FieldIdx, ReprOptions, VariantIdx};
use rustc_attr_data_structures::StrippedCfgItem;
use rustc_data_structures::fx::FxHashMap;
Expand All @@ -26,12 +27,10 @@ use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
use rustc_middle::middle::lib_features::FeatureStability;
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
use rustc_middle::mir;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::{
self, DeducedParamAttrs, ParameterizedOverTcx, Ty, TyCtxt, UnusedGenericParams,
};
use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt, UnusedGenericParams};
use rustc_middle::util::Providers;
use rustc_middle::{mir, trivially_parameterized_over_tcx};
use rustc_serialize::opaque::FileEncoder;
use rustc_session::config::{SymbolManglingVersion, TargetModifier};
use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
Expand All @@ -47,6 +46,7 @@ use crate::creader::CrateMetadataRef;
mod decoder;
mod def_path_hash_map;
mod encoder;
mod parameterized;
mod table;

pub(crate) fn rustc_version(cfg_version: &'static str) -> String {
Expand Down Expand Up @@ -86,10 +86,6 @@ struct LazyValue<T> {
_marker: PhantomData<fn() -> T>,
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyValue<T> {
type Value<'tcx> = LazyValue<T::Value<'tcx>>;
}

impl<T> LazyValue<T> {
fn from_position(position: NonZero<usize>) -> LazyValue<T> {
LazyValue { position, _marker: PhantomData }
Expand All @@ -112,10 +108,6 @@ struct LazyArray<T> {
_marker: PhantomData<fn() -> T>,
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
type Value<'tcx> = LazyArray<T::Value<'tcx>>;
}

impl<T> Default for LazyArray<T> {
fn default() -> LazyArray<T> {
LazyArray::from_position_and_num_elems(NonZero::new(1).unwrap(), 0)
Expand Down Expand Up @@ -143,10 +135,6 @@ struct LazyTable<I, T> {
_marker: PhantomData<fn(I) -> T>,
}

impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for LazyTable<I, T> {
type Value<'tcx> = LazyTable<I, T::Value<'tcx>>;
}

impl<I, T> LazyTable<I, T> {
fn from_position_and_encoded_size(
position: NonZero<usize>,
Expand Down Expand Up @@ -594,14 +582,3 @@ pub fn provide(providers: &mut Providers) {
encoder::provide(providers);
decoder::provide(providers);
}

trivially_parameterized_over_tcx! {
VariantData,
RawDefId,
TraitImpls,
IncoherentImpls,
CrateHeader,
CrateRoot,
CrateDep,
AttrFlags,
}
166 changes: 166 additions & 0 deletions compiler/rustc_metadata/src/rmeta/parameterized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use std::hash::Hash;

use rustc_data_structures::unord::UnordMap;
use rustc_hir::def_id::DefIndex;
use rustc_index::{Idx, IndexVec};
use rustc_middle::ty::{Binder, EarlyBinder};
use rustc_span::Symbol;

use crate::rmeta::{LazyArray, LazyTable, LazyValue};

pub(crate) trait ParameterizedOverTcx: 'static {
type Value<'tcx>;
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for Option<T> {
type Value<'tcx> = Option<T::Value<'tcx>>;
}

impl<A: ParameterizedOverTcx, B: ParameterizedOverTcx> ParameterizedOverTcx for (A, B) {
type Value<'tcx> = (A::Value<'tcx>, B::Value<'tcx>);
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for Vec<T> {
type Value<'tcx> = Vec<T::Value<'tcx>>;
}

impl<I: Idx + 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for IndexVec<I, T> {
type Value<'tcx> = IndexVec<I, T::Value<'tcx>>;
}

impl<I: Hash + Eq + 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for UnordMap<I, T> {
type Value<'tcx> = UnordMap<I, T::Value<'tcx>>;
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for Binder<'static, T> {
type Value<'tcx> = Binder<'tcx, T::Value<'tcx>>;
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for EarlyBinder<'static, T> {
type Value<'tcx> = EarlyBinder<'tcx, T::Value<'tcx>>;
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyValue<T> {
type Value<'tcx> = LazyValue<T::Value<'tcx>>;
}

impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
type Value<'tcx> = LazyArray<T::Value<'tcx>>;
}

impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for LazyTable<I, T> {
type Value<'tcx> = LazyTable<I, T::Value<'tcx>>;
}

macro_rules! trivially_parameterized_over_tcx {
($($ty:ty),+ $(,)?) => {
$(
impl ParameterizedOverTcx for $ty {
#[allow(unused_lifetimes)]
type Value<'tcx> = $ty;
}
)*
}
}

trivially_parameterized_over_tcx! {
bool,
u64,
usize,
std::string::String,
// tidy-alphabetical-start
crate::rmeta::AttrFlags,
crate::rmeta::CrateDep,
crate::rmeta::CrateHeader,
crate::rmeta::CrateRoot,
crate::rmeta::IncoherentImpls,
crate::rmeta::RawDefId,
crate::rmeta::TraitImpls,
crate::rmeta::VariantData,
rustc_abi::ReprOptions,
rustc_ast::DelimArgs,
rustc_attr_data_structures::ConstStability,
rustc_attr_data_structures::DefaultBodyStability,
rustc_attr_data_structures::Deprecation,
rustc_attr_data_structures::Stability,
rustc_attr_data_structures::StrippedCfgItem<rustc_hir::def_id::DefIndex>,
rustc_hir::Attribute,
rustc_hir::Constness,
rustc_hir::CoroutineKind,
rustc_hir::Defaultness,
rustc_hir::LangItem,
rustc_hir::OpaqueTyOrigin<rustc_hir::def_id::DefId>,
rustc_hir::PreciseCapturingArgKind<Symbol, Symbol>,
rustc_hir::Safety,
rustc_hir::def::DefKind,
rustc_hir::def::DocLinkResMap,
rustc_hir::def_id::DefId,
rustc_hir::def_id::DefIndex,
rustc_hir::definitions::DefKey,
rustc_index::bit_set::DenseBitSet<u32>,
rustc_middle::metadata::ModChild,
rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs,
rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile,
rustc_middle::middle::exported_symbols::SymbolExportInfo,
rustc_middle::middle::lib_features::FeatureStability,
rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault,
rustc_middle::mir::ConstQualifs,
rustc_middle::ty::AnonConstKind,
rustc_middle::ty::AssocItemContainer,
rustc_middle::ty::AsyncDestructor,
rustc_middle::ty::Asyncness,
rustc_middle::ty::DeducedParamAttrs,
rustc_middle::ty::Destructor,
rustc_middle::ty::Generics,
rustc_middle::ty::ImplTraitInTraitData,
rustc_middle::ty::IntrinsicDef,
rustc_middle::ty::TraitDef,
rustc_middle::ty::Variance,
rustc_middle::ty::Visibility<DefIndex>,
rustc_middle::ty::adjustment::CoerceUnsizedInfo,
rustc_middle::ty::fast_reject::SimplifiedType,
rustc_session::config::TargetModifier,
rustc_session::cstore::ForeignModule,
rustc_session::cstore::LinkagePreference,
rustc_session::cstore::NativeLib,
rustc_span::ExpnData,
rustc_span::ExpnHash,
rustc_span::ExpnId,
rustc_span::Ident,
rustc_span::SourceFile,
rustc_span::Span,
rustc_span::Symbol,
rustc_span::hygiene::SyntaxContextKey,
// tidy-alphabetical-end
}

// HACK(compiler-errors): This macro rule can only take a fake path,
// not a real, due to parsing ambiguity reasons.
macro_rules! parameterized_over_tcx {
($($( $fake_path:ident )::+ ),+ $(,)?) => {
$(
impl ParameterizedOverTcx for $( $fake_path )::+ <'static> {
type Value<'tcx> = $( $fake_path )::+ <'tcx>;
}
)*
}
}

parameterized_over_tcx! {
// tidy-alphabetical-start
crate::rmeta::DefPathHashMapRef,
rustc_middle::middle::exported_symbols::ExportedSymbol,
rustc_middle::mir::Body,
rustc_middle::mir::CoroutineLayout,
rustc_middle::mir::interpret::ConstAllocation,
rustc_middle::ty::Clause,
rustc_middle::ty::ClauseKind,
rustc_middle::ty::Const,
rustc_middle::ty::ConstConditions,
rustc_middle::ty::FnSig,
rustc_middle::ty::GenericPredicates,
rustc_middle::ty::ImplTraitHeader,
rustc_middle::ty::TraitRef,
rustc_middle::ty::Ty,
// tidy-alphabetical-end
}
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ pub use self::fold::*;
pub use self::instance::{Instance, InstanceKind, ReifyReason, ShortInstance, UnusedGenericParams};
pub use self::list::{List, ListWithCachedTypeInfo};
pub use self::opaque_types::OpaqueTypeKey;
pub use self::parameterized::ParameterizedOverTcx;
pub use self::pattern::{Pattern, PatternKind};
pub use self::predicate::{
AliasTerm, ArgOutlivesPredicate, Clause, ClauseKind, CoercePredicate, ExistentialPredicate,
Expand Down Expand Up @@ -158,7 +157,6 @@ mod instance;
mod intrinsic;
mod list;
mod opaque_types;
mod parameterized;
mod predicate;
mod region;
mod rvalue_scopes;
Expand Down
Loading
Loading