diff --git a/compiler/rustc_arena/src/lib.rs b/compiler/rustc_arena/src/lib.rs index a4248a82bce73..3e318b84b77f2 100644 --- a/compiler/rustc_arena/src/lib.rs +++ b/compiler/rustc_arena/src/lib.rs @@ -612,7 +612,11 @@ impl DroplessArena { /// arguments. The `TypedArena` will be used for them. /// #[rustc_macro_transparency = "semiopaque"] -pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) { +pub macro declare_arena( + $( + [] $name:ident: $ty:ty, + )* +) { #[derive(Default)] pub struct Arena<'tcx> { pub dropless: $crate::DroplessArena, diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index 827538f58f980..aa3ddcbfb59e8 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -1,15 +1,11 @@ -/// This higher-order macro declares a list of types which can be allocated by `Arena`. -/// Note that all `Copy` types can be allocated by default and need not be specified here. -#[macro_export] -macro_rules! arena_types { - ($macro:path) => ( - $macro!([ - // HIR types - [] asm_template: rustc_ast::InlineAsmTemplatePiece, - [] attribute: rustc_hir::Attribute, - [] owner_info: rustc_hir::OwnerInfo<'tcx>, - [] macro_def: rustc_ast::MacroDef, - [] delegation_info: rustc_hir::DelegationInfo, - ]); - ) +//! Declares an arena that can allocate values of any `Copy` type, and of +//! any needs-drop type listed below. + +rustc_arena::declare_arena! { + // HIR types + [] asm_template: rustc_ast::InlineAsmTemplatePiece, + [] attribute: rustc_hir::Attribute, + [] owner_info: rustc_hir::OwnerInfo<'tcx>, + [] macro_def: rustc_ast::MacroDef, + [] delegation_info: rustc_hir::DelegationInfo, } diff --git a/compiler/rustc_hir/src/lib.rs b/compiler/rustc_hir/src/lib.rs index 9b976d21d5365..008ad52e0339f 100644 --- a/compiler/rustc_hir/src/lib.rs +++ b/compiler/rustc_hir/src/lib.rs @@ -47,4 +47,4 @@ pub use rustc_ast::attr::version::*; pub use stability::*; pub use target::{MethodKind, Target}; -arena_types!(rustc_arena::declare_arena); +pub use crate::arena::Arena; diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index 87e19fafc0e22..10c39ee1257bb 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -1,135 +1,202 @@ -/// This higher-order macro declares a list of types which can be allocated by `Arena`. -/// -/// Specifying the `decode` modifier will add decode impls for `&T` and `&[T]` where `T` is the type -/// listed. See the `impl_arena_allocatable_decoder!` macro for more. -#[macro_export] -macro_rules! arena_types { - ($macro:path) => ( - $macro!([ - [] layout: rustc_abi::LayoutData, - [] proxy_coroutine_layout: rustc_middle::mir::CoroutineLayout<'tcx>, - [] fn_abi: rustc_target::callconv::FnAbi<'tcx, rustc_middle::ty::Ty<'tcx>>, - [] adt_def: rustc_middle::ty::AdtDefData, - [] steal_thir: rustc_data_structures::steal::Steal>, - [] steal_mir: rustc_data_structures::steal::Steal>, - [decode] mir: rustc_middle::mir::Body<'tcx>, - [] steal_promoted: - rustc_data_structures::steal::Steal< - rustc_index::IndexVec< - rustc_middle::mir::Promoted, - rustc_middle::mir::Body<'tcx> - > - >, - [decode] promoted: - rustc_index::IndexVec< - rustc_middle::mir::Promoted, - rustc_middle::mir::Body<'tcx> - >, - [decode] typeck_results: rustc_middle::ty::TypeckResults<'tcx>, - [] borrowck_result: rustc_data_structures::fx::FxIndexMap< - rustc_hir::def_id::LocalDefId, - rustc_middle::ty::DefinitionSiteHiddenType<'tcx>, - >, - [] resolver: rustc_data_structures::steal::Steal>, - [] index_ast: rustc_index::IndexVec< - rustc_span::def_id::LocalDefId, - rustc_data_structures::steal::Steal<( - std::sync::Arc>, - rustc_ast::AstOwner - )> - >, - [] crate_alone: rustc_data_structures::steal::Steal, - [] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>, - [] resolutions: rustc_middle::ty::ResolverGlobalCtxt, - [] const_allocs: rustc_middle::mir::interpret::Allocation, - [] region_scope_tree: rustc_middle::middle::region::ScopeTree, - // Required for the incremental on-disk cache - [] mir_keys: rustc_hir::def_id::DefIdSet, - [] dropck_outlives: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, - rustc_middle::traits::query::DropckOutlivesResult<'tcx> - > - >, - [] normalize_canonicalized_projection: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, - rustc_middle::traits::query::NormalizationResult<'tcx> - > - >, - [] implied_outlives_bounds: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, - Vec> - > - >, - [] dtorck_constraint: rustc_middle::traits::query::DropckConstraint<'tcx>, - [] candidate_step: rustc_middle::traits::query::CandidateStep<'tcx>, - [] autoderef_bad_ty: rustc_middle::traits::query::MethodAutoderefBadTy<'tcx>, - [] query_region_constraints: rustc_middle::infer::canonical::QueryRegionConstraints<'tcx>, - [] type_op_subtype: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, ()> - >, - [] type_op_normalize_poly_fn_sig: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::PolyFnSig<'tcx>> - >, - [] type_op_normalize_fn_sig: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::FnSig<'tcx>> - >, - [] type_op_normalize_clause: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::Clause<'tcx>> - >, - [] type_op_normalize_ty: - rustc_middle::infer::canonical::Canonical<'tcx, - rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::Ty<'tcx>> - >, - [] inspect_probe: rustc_middle::traits::solve::inspect::Probe>, - [] effective_visibilities: rustc_middle::middle::privacy::EffectiveVisibilities, - [] upvars_mentioned: rustc_data_structures::fx::FxIndexMap, - [] dyn_compatibility_violations: rustc_middle::traits::DynCompatibilityViolation, - [] codegen_unit: rustc_middle::mono::CodegenUnit<'tcx>, - [decode] attribute: rustc_hir::Attribute, - [] name_set: rustc_data_structures::unord::UnordSet, - [] autodiff_item: rustc_hir::attrs::AutoDiffItem, - [] ordered_name_set: rustc_data_structures::fx::FxIndexSet, - [] stable_order_of_exportable_impls: - rustc_data_structures::fx::FxIndexMap, +//! Declares [`rustc_middle::arena::Arena`], which can allocate values of any +//! `Copy` type, and any needs-drop type explicitly listed below. - // Note that this deliberately duplicates items in the `rustc_hir::arena`, - // since we need to allocate this type on both the `rustc_hir` arena - // (during lowering) and the `rustc_middle` arena (for decoding MIR) - [decode] asm_template: rustc_ast::InlineAsmTemplatePiece, - [decode] used_trait_imports: rustc_data_structures::unord::UnordSet, - [] is_late_bound_map: rustc_data_structures::fx::FxIndexSet, - [decode] impl_source: rustc_middle::traits::ImplSource<'tcx, ()>, +use rustc_serialize::Decodable; - [] dep_kind_vtable: rustc_middle::dep_graph::DepKindVTable<'tcx>, +use crate::ty::codec::{RefDecodable, TyDecoder}; +use crate::ty::{Ty, TyCtxt}; - [decode] trait_impl_trait_tys: - rustc_data_structures::unord::UnordMap< - rustc_hir::def_id::DefId, - rustc_middle::ty::EarlyBinder<'tcx, rustc_middle::ty::Ty<'tcx>> - >, - [] external_constraints: rustc_middle::traits::solve::ExternalConstraintsData>, - [] doc_link_resolutions: rustc_hir::def::DocLinkResMap, - [] stripped_cfg_items: rustc_hir::attrs::StrippedCfgItem, - [] mod_child: rustc_middle::metadata::ModChild, - [] features: rustc_feature::Features, - [decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph, - [] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls, - [] hir_owner_nodes: rustc_hir::OwnerNodes<'tcx>, - [decode] token_stream: rustc_ast::tokenstream::TokenStream, - [] maybe_owner: rustc_middle::hir::ProjectedMaybeOwner<'tcx>, - [] owner_info: rustc_middle::hir::ProjectedOwnerInfo<'tcx>, - [] parenting: rustc_hir::def_id::LocalDefIdMap, - [] trait_candidates: rustc_hir::ItemLocalMap<&'tcx [rustc_hir::TraitCandidate<'tcx>]>, - [] delayed_lints: rustc_data_structures::steal::Steal, - ]); - ) +rustc_arena::declare_arena! { + [] layout: rustc_abi::LayoutData, + [] proxy_coroutine_layout: rustc_middle::mir::CoroutineLayout<'tcx>, + [] fn_abi: rustc_target::callconv::FnAbi<'tcx, Ty<'tcx>>, + [] adt_def: rustc_middle::ty::AdtDefData, + [] steal_thir: rustc_data_structures::steal::Steal>, + [] steal_mir: rustc_data_structures::steal::Steal>, + [] mir: rustc_middle::mir::Body<'tcx>, + [] steal_promoted: + rustc_data_structures::steal::Steal< + rustc_index::IndexVec< + rustc_middle::mir::Promoted, + rustc_middle::mir::Body<'tcx> + > + >, + [] promoted: + rustc_index::IndexVec< + rustc_middle::mir::Promoted, + rustc_middle::mir::Body<'tcx> + >, + [] typeck_results: rustc_middle::ty::TypeckResults<'tcx>, + [] borrowck_result: + rustc_data_structures::fx::FxIndexMap< + rustc_hir::def_id::LocalDefId, + rustc_middle::ty::DefinitionSiteHiddenType<'tcx>, + >, + [] resolver: rustc_data_structures::steal::Steal>, + [] index_ast: + rustc_index::IndexVec< + rustc_span::def_id::LocalDefId, + rustc_data_structures::steal::Steal<( + std::sync::Arc>, + rustc_ast::AstOwner + )> + >, + [] crate_alone: rustc_data_structures::steal::Steal, + [] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>, + [] resolutions: rustc_middle::ty::ResolverGlobalCtxt, + [] const_allocs: rustc_middle::mir::interpret::Allocation, + [] region_scope_tree: rustc_middle::middle::region::ScopeTree, + // Required for the incremental on-disk cache + [] mir_keys: rustc_hir::def_id::DefIdSet, + [] dropck_outlives: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, + rustc_middle::traits::query::DropckOutlivesResult<'tcx> + > + >, + [] normalize_canonicalized_projection: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, + rustc_middle::traits::query::NormalizationResult<'tcx> + > + >, + [] implied_outlives_bounds: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, + Vec> + > + >, + [] dtorck_constraint: rustc_middle::traits::query::DropckConstraint<'tcx>, + [] candidate_step: rustc_middle::traits::query::CandidateStep<'tcx>, + [] autoderef_bad_ty: rustc_middle::traits::query::MethodAutoderefBadTy<'tcx>, + [] query_region_constraints: rustc_middle::infer::canonical::QueryRegionConstraints<'tcx>, + [] type_op_subtype: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, ()> + >, + [] type_op_normalize_poly_fn_sig: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::PolyFnSig<'tcx>> + >, + [] type_op_normalize_fn_sig: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::FnSig<'tcx>> + >, + [] type_op_normalize_clause: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::Clause<'tcx>> + >, + [] type_op_normalize_ty: + rustc_middle::infer::canonical::Canonical<'tcx, + rustc_middle::infer::canonical::QueryResponse<'tcx, Ty<'tcx>> + >, + [] inspect_probe: rustc_middle::traits::solve::inspect::Probe>, + [] effective_visibilities: rustc_middle::middle::privacy::EffectiveVisibilities, + [] upvars_mentioned: rustc_data_structures::fx::FxIndexMap, + [] dyn_compatibility_violations: rustc_middle::traits::DynCompatibilityViolation, + [] codegen_unit: rustc_middle::mono::CodegenUnit<'tcx>, + [] attribute: rustc_hir::Attribute, + [] name_set: rustc_data_structures::unord::UnordSet, + [] autodiff_item: rustc_hir::attrs::AutoDiffItem, + [] ordered_name_set: rustc_data_structures::fx::FxIndexSet, + [] stable_order_of_exportable_impls: + rustc_data_structures::fx::FxIndexMap, + + // Note that this deliberately duplicates items in the `rustc_hir::arena`, + // since we need to allocate this type on both the `rustc_hir` arena + // (during lowering) and the `rustc_middle` arena (for decoding MIR) + [] asm_template: rustc_ast::InlineAsmTemplatePiece, + [] used_trait_imports: rustc_data_structures::unord::UnordSet, + [] is_late_bound_map: rustc_data_structures::fx::FxIndexSet, + [] impl_source: rustc_middle::traits::ImplSource<'tcx, ()>, + + [] dep_kind_vtable: rustc_middle::dep_graph::DepKindVTable<'tcx>, + + [] trait_impl_trait_tys: + rustc_data_structures::unord::UnordMap< + rustc_hir::def_id::DefId, + rustc_middle::ty::EarlyBinder<'tcx, Ty<'tcx>> + >, + [] external_constraints: rustc_middle::traits::solve::ExternalConstraintsData>, + [] doc_link_resolutions: rustc_hir::def::DocLinkResMap, + [] stripped_cfg_items: rustc_hir::attrs::StrippedCfgItem, + [] mod_child: rustc_middle::metadata::ModChild, + [] features: rustc_feature::Features, + [] specialization_graph: rustc_middle::traits::specialization_graph::Graph, + [] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls, + [] hir_owner_nodes: rustc_hir::OwnerNodes<'tcx>, + [] token_stream: rustc_ast::tokenstream::TokenStream, + [] maybe_owner: rustc_middle::hir::ProjectedMaybeOwner<'tcx>, + [] owner_info: rustc_middle::hir::ProjectedOwnerInfo<'tcx>, + [] parenting: rustc_hir::def_id::LocalDefIdMap, + [] trait_candidates: rustc_hir::ItemLocalMap<&'tcx [rustc_hir::TraitCandidate<'tcx>]>, + [] delayed_lints: rustc_data_structures::steal::Steal, } -arena_types!(rustc_arena::declare_arena); +#[inline] +fn decode_arena_allocable<'tcx, D, C, T>(decoder: &mut D) -> &'tcx T +where + D: TyDecoder<'tcx>, + T: ArenaAllocatable<'tcx, C> + Decodable, +{ + let value: T = Decodable::decode(decoder); + decoder.interner().arena.alloc(value) +} + +#[inline] +fn decode_arena_allocable_slice<'tcx, D, C, T>(decoder: &mut D) -> &'tcx [T] +where + D: TyDecoder<'tcx>, + T: ArenaAllocatable<'tcx, C> + Decodable, +{ + let values: Vec = Decodable::decode(decoder); + decoder.interner().arena.alloc_from_iter(values) +} + +/// Implements [`RefDecodable`] for `T` (and `[T]`), by decoding to `T` and +/// then moving the value or values into an arena allocation. +macro_rules! impl_ref_decodable_into_arena { + ( + $( + $ty:ty, + )* + ) => { + $( + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { + #[inline] + fn decode(decoder: &mut D) -> &'tcx Self { + decode_arena_allocable(decoder) + } + } + + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { + #[inline] + fn decode(decoder: &mut D) -> &'tcx Self { + decode_arena_allocable_slice(decoder) + } + } + )* + } +} + +impl_ref_decodable_into_arena! { + // tidy-alphabetical-start + (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo), + rustc_ast::InlineAsmTemplatePiece, + rustc_ast::tokenstream::TokenStream, + rustc_data_structures::unord::UnordMap>>, + rustc_data_structures::unord::UnordSet, + rustc_hir::Attribute, + rustc_index::IndexVec>, + rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs, + rustc_middle::mir::Body<'tcx>, + rustc_middle::traits::ImplSource<'tcx, ()>, + rustc_middle::traits::specialization_graph::Graph, + rustc_middle::ty::TypeckResults<'tcx>, + rustc_middle::ty::Variance, + rustc_span::Ident, + rustc_span::Span, + rustc_span::def_id::DefId, + rustc_span::def_id::LocalDefId, + // tidy-alphabetical-end +} diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 5f76467ed7db2..0b2a1c55cfcf5 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -17,7 +17,6 @@ use rustc_middle::ty::Const; use rustc_serialize::{Decodable, Encodable}; use rustc_span::{Span, SpanDecoder, SpanEncoder, Spanned}; -use crate::arena::ArenaAllocatable; use crate::infer::canonical::{CanonicalVarKind, CanonicalVarKinds}; use crate::mir::interpret::{AllocId, ConstAllocation, CtfeProvenance}; use crate::mono::MonoItem; @@ -206,24 +205,6 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::ParamEnv<'tcx> { } } -#[inline] -fn decode_arena_allocable<'tcx, D: TyDecoder<'tcx>, T: ArenaAllocatable<'tcx> + Decodable>( - decoder: &mut D, -) -> &'tcx T { - decoder.interner().arena.alloc(Decodable::decode(decoder)) -} - -#[inline] -fn decode_arena_allocable_slice< - 'tcx, - D: TyDecoder<'tcx>, - T: ArenaAllocatable<'tcx> + Decodable, ->( - decoder: &mut D, -) -> &'tcx [T] { - decoder.interner().arena.alloc_from_iter( as Decodable>::decode(decoder)) -} - impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { #[allow(rustc::usage_of_ty_tykind)] fn decode(decoder: &mut D) -> Ty<'tcx> { @@ -500,64 +481,6 @@ macro_rules! __impl_decoder_methods { } } -macro_rules! impl_arena_allocatable_decoder { - ([] $name:ident: $ty:ty) => {}; - ([decode] $name:ident: $ty:ty) => { - impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { - #[inline] - fn decode(decoder: &mut D) -> &'tcx Self { - decode_arena_allocable(decoder) - } - } - - impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { - #[inline] - fn decode(decoder: &mut D) -> &'tcx Self { - decode_arena_allocable_slice(decoder) - } - } - }; -} - -macro_rules! impl_arena_allocatable_decoders { - ([$($a:tt $name:ident: $ty:ty,)*]) => { - $( - impl_arena_allocatable_decoder!($a $name: $ty); - )* - } -} - -rustc_hir::arena_types!(impl_arena_allocatable_decoders); -arena_types!(impl_arena_allocatable_decoders); - -macro_rules! impl_arena_copy_decoder { - (<$tcx:tt> $($ty:ty,)*) => { - $(impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { - #[inline] - fn decode(decoder: &mut D) -> &'tcx Self { - decoder.interner().arena.alloc(Decodable::decode(decoder)) - } - } - - impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { - #[inline] - fn decode(decoder: &mut D) -> &'tcx Self { - decoder.interner().arena.alloc_from_iter( as Decodable>::decode(decoder)) - } - })* - }; -} - -impl_arena_copy_decoder! {<'tcx> - Span, - rustc_span::Ident, - ty::Variance, - rustc_span::def_id::DefId, - rustc_span::def_id::LocalDefId, - (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo), - rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs, -} - #[macro_export] macro_rules! implement_ty_decoder { ($DecoderName:ident <$($typaram:tt),*>) => {