Skip to content

Commit 6035487

Browse files
committed
Clarify ArenaAllocatable's second parameter.
It's simply a binary thing to allow different behaviour for `Copy` vs `!Copy` types. The new code makes this much clearer; I was scratching my head over the old code for some time.
1 parent 9065c7c commit 6035487

File tree

1 file changed

+10
-5
lines changed
  • compiler/rustc_arena/src

1 file changed

+10
-5
lines changed

compiler/rustc_arena/src/lib.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
546546
$($name: $crate::TypedArena<$ty>,)*
547547
}
548548

549-
pub trait ArenaAllocatable<'tcx, T = Self>: Sized {
549+
pub trait ArenaAllocatable<'tcx, C = rustc_arena::IsNotCopy>: Sized {
550550
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self;
551551
fn allocate_from_iter<'a>(
552552
arena: &'a Arena<'tcx>,
@@ -555,7 +555,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
555555
}
556556

557557
// Any type that impls `Copy` can be arena-allocated in the `DroplessArena`.
558-
impl<'tcx, T: Copy> ArenaAllocatable<'tcx, ()> for T {
558+
impl<'tcx, T: Copy> ArenaAllocatable<'tcx, rustc_arena::IsCopy> for T {
559559
#[inline]
560560
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
561561
arena.dropless.alloc(self)
@@ -569,7 +569,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
569569
}
570570
}
571571
$(
572-
impl<'tcx> ArenaAllocatable<'tcx, $ty> for $ty {
572+
impl<'tcx> ArenaAllocatable<'tcx, rustc_arena::IsNotCopy> for $ty {
573573
#[inline]
574574
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
575575
if !::std::mem::needs_drop::<Self>() {
@@ -595,7 +595,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
595595

596596
impl<'tcx> Arena<'tcx> {
597597
#[inline]
598-
pub fn alloc<T: ArenaAllocatable<'tcx, U>, U>(&self, value: T) -> &mut T {
598+
pub fn alloc<T: ArenaAllocatable<'tcx, C>, C>(&self, value: T) -> &mut T {
599599
value.allocate_on(self)
600600
}
601601

@@ -608,7 +608,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
608608
self.dropless.alloc_slice(value)
609609
}
610610

611-
pub fn alloc_from_iter<'a, T: ArenaAllocatable<'tcx, U>, U>(
611+
pub fn alloc_from_iter<'a, T: ArenaAllocatable<'tcx, C>, C>(
612612
&'a self,
613613
iter: impl ::std::iter::IntoIterator<Item = T>,
614614
) -> &'a mut [T] {
@@ -617,5 +617,10 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
617617
}
618618
}
619619

620+
// Marker types that let us give different behaviour for arenas allocating
621+
// `Copy` types vs `!Copy` types.
622+
pub struct IsCopy;
623+
pub struct IsNotCopy;
624+
620625
#[cfg(test)]
621626
mod tests;

0 commit comments

Comments
 (0)