Skip to content

Commit

Permalink
Rollup merge of rust-lang#122018 - RalfJung:box-custom-alloc, r=oli-obk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthiaskrgr authored Mar 5, 2024
2 parents 2dcbff1 + e3e1ea4 commit a27191d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ extern "Rust" {
#[unstable(feature = "allocator_api", issue = "32838")]
#[derive(Copy, Clone, Default, Debug)]
#[cfg(not(test))]
// the compiler needs to know when a Box uses the global allocator vs a custom one
#[cfg_attr(not(bootstrap), lang = "global_alloc_ty")]
pub struct Global;

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,9 @@ impl<Args: Tuple, F: AsyncFn<Args> + ?Sized, A: Allocator> AsyncFn<Args> for Box
#[unstable(feature = "coerce_unsized", issue = "18598")]
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}

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

Expand Down

0 comments on commit a27191d

Please sign in to comment.