rust-lang / rust-lang/rust

`DispatchFromDyn` impl on `Box` with custom allocator is allowed

Open
#156,922 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-allocators A-box C-bug F-dispatch_from_dyn needs-triage
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.