Skip to content

Commit a27191d

Browse files
authored
Rollup merge of rust-lang#122018 - RalfJung:box-custom-alloc, r=oli-obk
only set noalias on Box with the global allocator As discovered in rust-lang/miri#3341, `noalias` and custom allocators don't go well together. rustc can now check whether a Box uses the global allocator. This replaces the previous ad-hoc and rather unprincipled check for a zero-sized allocator. This is the rustc part of fixing that; Miri will also need a patch.
2 parents 2dcbff1 + e3e1ea4 commit a27191d

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

alloc/src/alloc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ extern "Rust" {
5050
#[unstable(feature = "allocator_api", issue = "32838")]
5151
#[derive(Copy, Clone, Default, Debug)]
5252
#[cfg(not(test))]
53+
// the compiler needs to know when a Box uses the global allocator vs a custom one
54+
#[cfg_attr(not(bootstrap), lang = "global_alloc_ty")]
5355
pub struct Global;
5456

5557
#[cfg(test)]

alloc/src/boxed.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,9 @@ impl<Args: Tuple, F: AsyncFn<Args> + ?Sized, A: Allocator> AsyncFn<Args> for Box
20622062
#[unstable(feature = "coerce_unsized", issue = "18598")]
20632063
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
20642064

2065+
// It is quite crucial that we only allow the `Global` allocator here.
2066+
// Handling arbitrary custom allocators (which can affect the `Box` layout heavily!)
2067+
// would need a lot of codegen and interpreter adjustments.
20652068
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
20662069
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T, Global> {}
20672070

0 commit comments

Comments
 (0)