Skip to content

Commit 5eacec9

Browse files
committed
Auto merge of #85690 - bstrie:m2_arena, r=jackh726,nagisa
Macros 2.0-ify rustc_arena For the purpose of battle-testing macros 2.0 I'm looking to dogfood it in rustc, one crate at a time. (Note that there are only 12 changed lines if you ignore whitespace.)
2 parents 926f069 + 71db7cc commit 5eacec9

File tree

1 file changed

+76
-78
lines changed
  • compiler/rustc_arena/src

1 file changed

+76
-78
lines changed

compiler/rustc_arena/src/lib.rs

+76-78
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#![feature(new_uninit)]
1616
#![feature(maybe_uninit_slice)]
1717
#![feature(min_specialization)]
18+
#![feature(decl_macro)]
19+
#![feature(rustc_attrs)]
1820
#![cfg_attr(test, feature(test))]
1921

2022
use rustc_data_structures::sync;
@@ -608,117 +610,113 @@ impl DropArena {
608610
}
609611
}
610612

611-
#[macro_export]
612-
macro_rules! arena_for_type {
613+
pub macro arena_for_type {
613614
([][$ty:ty]) => {
614615
$crate::TypedArena<$ty>
615-
};
616+
},
616617
([few $(, $attrs:ident)*][$ty:ty]) => {
617618
::std::marker::PhantomData<$ty>
618-
};
619+
},
619620
([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
620621
$crate::arena_for_type!([$($attrs),*]$args)
621-
};
622+
},
622623
}
623624

624-
#[macro_export]
625-
macro_rules! which_arena_for_type {
625+
pub macro which_arena_for_type {
626626
([][$arena:expr]) => {
627627
::std::option::Option::Some($arena)
628-
};
628+
},
629629
([few$(, $attrs:ident)*][$arena:expr]) => {
630630
::std::option::Option::None
631-
};
631+
},
632632
([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
633633
$crate::which_arena_for_type!([$($attrs),*]$args)
634-
};
634+
},
635635
}
636636

637-
#[macro_export]
638-
macro_rules! declare_arena {
639-
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
640-
#[derive(Default)]
641-
pub struct Arena<$tcx> {
642-
pub dropless: $crate::DroplessArena,
643-
drop: $crate::DropArena,
644-
$($name: $crate::arena_for_type!($a[$ty]),)*
645-
}
637+
#[rustc_macro_transparency = "semitransparent"]
638+
pub macro declare_arena([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
639+
#[derive(Default)]
640+
pub struct Arena<$tcx> {
641+
pub dropless: $crate::DroplessArena,
642+
drop: $crate::DropArena,
643+
$($name: $crate::arena_for_type!($a[$ty]),)*
644+
}
646645

647-
pub trait ArenaAllocatable<'tcx, T = Self>: Sized {
648-
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self;
649-
fn allocate_from_iter<'a>(
650-
arena: &'a Arena<'tcx>,
651-
iter: impl ::std::iter::IntoIterator<Item = Self>,
652-
) -> &'a mut [Self];
646+
pub trait ArenaAllocatable<'tcx, T = Self>: Sized {
647+
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self;
648+
fn allocate_from_iter<'a>(
649+
arena: &'a Arena<'tcx>,
650+
iter: impl ::std::iter::IntoIterator<Item = Self>,
651+
) -> &'a mut [Self];
652+
}
653+
654+
impl<'tcx, T: Copy> ArenaAllocatable<'tcx, ()> for T {
655+
#[inline]
656+
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
657+
arena.dropless.alloc(self)
658+
}
659+
#[inline]
660+
fn allocate_from_iter<'a>(
661+
arena: &'a Arena<'tcx>,
662+
iter: impl ::std::iter::IntoIterator<Item = Self>,
663+
) -> &'a mut [Self] {
664+
arena.dropless.alloc_from_iter(iter)
653665
}
654666

655-
impl<'tcx, T: Copy> ArenaAllocatable<'tcx, ()> for T {
667+
}
668+
$(
669+
impl<$tcx> ArenaAllocatable<$tcx, $ty> for $ty {
656670
#[inline]
657-
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
658-
arena.dropless.alloc(self)
671+
fn allocate_on<'a>(self, arena: &'a Arena<$tcx>) -> &'a mut Self {
672+
if !::std::mem::needs_drop::<Self>() {
673+
return arena.dropless.alloc(self);
674+
}
675+
match $crate::which_arena_for_type!($a[&arena.$name]) {
676+
::std::option::Option::<&$crate::TypedArena<Self>>::Some(ty_arena) => {
677+
ty_arena.alloc(self)
678+
}
679+
::std::option::Option::None => unsafe { arena.drop.alloc(self) },
680+
}
659681
}
682+
660683
#[inline]
661684
fn allocate_from_iter<'a>(
662-
arena: &'a Arena<'tcx>,
685+
arena: &'a Arena<$tcx>,
663686
iter: impl ::std::iter::IntoIterator<Item = Self>,
664687
) -> &'a mut [Self] {
665-
arena.dropless.alloc_from_iter(iter)
666-
}
667-
668-
}
669-
$(
670-
impl<$tcx> ArenaAllocatable<$tcx, $ty> for $ty {
671-
#[inline]
672-
fn allocate_on<'a>(self, arena: &'a Arena<$tcx>) -> &'a mut Self {
673-
if !::std::mem::needs_drop::<Self>() {
674-
return arena.dropless.alloc(self);
675-
}
676-
match $crate::which_arena_for_type!($a[&arena.$name]) {
677-
::std::option::Option::<&$crate::TypedArena<Self>>::Some(ty_arena) => {
678-
ty_arena.alloc(self)
679-
}
680-
::std::option::Option::None => unsafe { arena.drop.alloc(self) },
681-
}
688+
if !::std::mem::needs_drop::<Self>() {
689+
return arena.dropless.alloc_from_iter(iter);
682690
}
683-
684-
#[inline]
685-
fn allocate_from_iter<'a>(
686-
arena: &'a Arena<$tcx>,
687-
iter: impl ::std::iter::IntoIterator<Item = Self>,
688-
) -> &'a mut [Self] {
689-
if !::std::mem::needs_drop::<Self>() {
690-
return arena.dropless.alloc_from_iter(iter);
691-
}
692-
match $crate::which_arena_for_type!($a[&arena.$name]) {
693-
::std::option::Option::<&$crate::TypedArena<Self>>::Some(ty_arena) => {
694-
ty_arena.alloc_from_iter(iter)
695-
}
696-
::std::option::Option::None => unsafe { arena.drop.alloc_from_iter(iter) },
691+
match $crate::which_arena_for_type!($a[&arena.$name]) {
692+
::std::option::Option::<&$crate::TypedArena<Self>>::Some(ty_arena) => {
693+
ty_arena.alloc_from_iter(iter)
697694
}
695+
::std::option::Option::None => unsafe { arena.drop.alloc_from_iter(iter) },
698696
}
699697
}
700-
)*
698+
}
699+
)*
701700

702-
impl<'tcx> Arena<'tcx> {
703-
#[inline]
704-
pub fn alloc<T: ArenaAllocatable<'tcx, U>, U>(&self, value: T) -> &mut T {
705-
value.allocate_on(self)
706-
}
701+
impl<'tcx> Arena<'tcx> {
702+
#[inline]
703+
pub fn alloc<T: ArenaAllocatable<'tcx, U>, U>(&self, value: T) -> &mut T {
704+
value.allocate_on(self)
705+
}
707706

708-
#[inline]
709-
pub fn alloc_slice<T: ::std::marker::Copy>(&self, value: &[T]) -> &mut [T] {
710-
if value.is_empty() {
711-
return &mut [];
712-
}
713-
self.dropless.alloc_slice(value)
707+
#[inline]
708+
pub fn alloc_slice<T: ::std::marker::Copy>(&self, value: &[T]) -> &mut [T] {
709+
if value.is_empty() {
710+
return &mut [];
714711
}
712+
self.dropless.alloc_slice(value)
713+
}
715714

716-
pub fn alloc_from_iter<'a, T: ArenaAllocatable<'tcx, U>, U>(
717-
&'a self,
718-
iter: impl ::std::iter::IntoIterator<Item = T>,
719-
) -> &'a mut [T] {
720-
T::allocate_from_iter(self, iter)
721-
}
715+
pub fn alloc_from_iter<'a, T: ArenaAllocatable<'tcx, U>, U>(
716+
&'a self,
717+
iter: impl ::std::iter::IntoIterator<Item = T>,
718+
) -> &'a mut [T] {
719+
T::allocate_from_iter(self, iter)
722720
}
723721
}
724722
}

0 commit comments

Comments
 (0)