Skip to content

Commit 61178bd

Browse files
Rollup merge of #159955 - Zalathar:arena, r=mejrs
Stop using higher-order macros to declare arenas The arenas used by `rustc_hir` and `rustc_middle` are declared via macros. But instead of invoking `rustc_arena::declare_arena!` directly, those crates declare a higher-order macro containing a list of types, and then invoke that macro with `declare_arena!` as an argument. From what I can tell, the only reason for this arrangement is so that the list of types can also contain `[decode]` modifiers that produce a corresponding implementation of `RefDecodable` that decodes into the arena. But the number of types involved is small enough that it's easy to list them separately, and the advantage of doing so is that we can remove a layer of macro indirection, making it easier to navigate to the real code. There should be no change to compiler behaviour.
2 parents 27335d1 + 01cef43 commit 61178bd

5 files changed

Lines changed: 241 additions & 234 deletions

File tree

compiler/rustc_arena/src/lib.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -598,21 +598,32 @@ impl DroplessArena {
598598
}
599599
}
600600

601-
/// Declare an `Arena` containing one dropless arena and many typed arenas (the
602-
/// types of the typed arenas are specified by the arguments).
601+
/// Declares an `Arena` that can allocate values of a variety of `Copy`, `needs_drop` and
602+
/// `!needs_drop` types.
603603
///
604-
/// There are three cases of interest.
605-
/// - Types that are `Copy`: these need not be specified in the arguments. They
606-
/// will use the `DroplessArena`.
607-
/// - Types that are `!Copy` and `!Drop`: these must be specified in the
608-
/// arguments. An empty `TypedArena` will be created for each one, but the
609-
/// `DroplessArena` will always be used and the `TypedArena` will stay empty.
610-
/// This is odd but harmless, because an empty arena allocates no memory.
611-
/// - Types that are `!Copy` and `Drop`: these must be specified in the
612-
/// arguments. The `TypedArena` will be used for them.
604+
/// The declared arena actually contains a single [`DroplessArena`], plus a separate
605+
/// [`TypedArena`] for each of the types listed in the body of the macro invocation.
613606
///
607+
/// Any type that is `Copy` can be allocated in the arena without needing to be listed
608+
/// explicitly. Those values will be stored in the [`DroplessArena`].
609+
///
610+
/// Types that are `!Copy` can only be allocated if they are listed in the macro invocation.
611+
/// For types that are `!Copy + needs_drop`, values will be stored in the corresponding
612+
/// [`TypedArena`] and will be dropped when the arena is dropped.
613+
///
614+
/// As an optimization, types that are `!Copy + !needs_drop` will actually be stored in the
615+
/// [`DroplessArena`], and the corresponding [`TypedArena`] will remain empty. This makes
616+
/// better use of the dropless arena's storage blocks, while the overhead of having a few
617+
/// unused typed-arenas is negligible.
614618
#[rustc_macro_transparency = "semiopaque"]
615-
pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
619+
pub macro declare_arena(
620+
// Each of these entries becomes a `$name: TypedArena<$ty>` field in the arena.
621+
// This allows values of non-copy type $ty to be allocated in the arena.
622+
// The field names must be distinct, but have no further significance.
623+
$(
624+
$name:ident: $ty:ty,
625+
)*
626+
) {
616627
#[derive(Default)]
617628
pub struct Arena<'tcx> {
618629
pub dropless: $crate::DroplessArena,

compiler/rustc_hir/src/arena.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
/// This higher-order macro declares a list of types which can be allocated by `Arena`.
2-
/// Note that all `Copy` types can be allocated by default and need not be specified here.
3-
#[macro_export]
4-
macro_rules! arena_types {
5-
($macro:path) => (
6-
$macro!([
7-
// HIR types
8-
[] asm_template: rustc_ast::InlineAsmTemplatePiece,
9-
[] attribute: crate::Attribute,
10-
[] owner_info: crate::OwnerInfo<'tcx>,
11-
[] macro_def: rustc_ast::MacroDef,
12-
[] delegation_info: crate::DelegationInfo,
13-
]);
14-
)
1+
//! Declares an arena that can allocate values of any `Copy` type, and of
2+
//! any `!Copy` type listed below.
3+
4+
rustc_arena::declare_arena! {
5+
// HIR types
6+
asm_template: rustc_ast::InlineAsmTemplatePiece,
7+
attribute: crate::Attribute,
8+
owner_info: crate::OwnerInfo<'tcx>,
9+
macro_def: rustc_ast::MacroDef,
10+
delegation_info: crate::DelegationInfo,
1511
}

compiler/rustc_hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ pub use rustc_span::def_id;
4242
pub use stability::*;
4343
pub use target::{MethodKind, Target};
4444

45-
arena_types!(rustc_arena::declare_arena);
45+
pub use crate::arena::Arena;

0 commit comments

Comments
 (0)