`DispatchFromDyn` impl on `Box` with custom allocator is allowed
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I'm not sure if this is a bug or not.
The following code currently compiles:
#![feature(allocator_api, dispatch_from_dyn, unsize)]
use std::{
alloc::{self, Allocator, Global, Layout},
marker::Unsize,
ops::DispatchFromDyn,
ptr::NonNull,
};
struct MyAllocator;
unsafe impl Allocator for MyAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, alloc::AllocError> {
Global.allocate(layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { Global.deallocate(ptr, layout) }
}
}
impl<U: ?Sized> DispatchFromDyn<Box<U, MyAllocator>> for Box<i32, MyAllocator> where i32: Unsize<U> {}
This seems very suspicious, given that there's a comment saying that this impl should not be allowed: https://github.com/rust-lang/rust/blob/6083f7891b5088184968e050d0eaecc7bcfd2b3d/library/alloc/src/boxed.rs#L2330-L2334
I am unable to find a way to do anything interesting with this though. I couldn't even find a way to do dyn-dispatch with this allocator.
Meta
Reproducible on the playground with version 1.98.0-nightly (2026-05-24 423e3d2529999d5e57fa)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the DispatchFromDyn implementation and comment in library/alloc/src/boxed.rs around lines 2330-2334, then compile the provided nightly reproducer. Determine whether the custom-allocator implementation should be rejected despite compiling; done means the behavior is resolved consistently with the comment.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100