Skip to content

inference weirdness around allocator_api #144264

@flippette

Description

@flippette

I have this code:

pub struct Queue<T, A>
where
  A: Allocator
{
  alloc: A,
  /* ... */
}

struct Node<T> { /* ... */ }

impl<T, A> Queue<T, A>
where
  A: Allocator
{
  pub fn push(&self, elem: T) {
    let new_head = Box::into_raw(Box::new_in(
      Node { /* ... */ },
      &self.alloc,
    ));

    /* ... */
  }
}

The compiler infers Box to be of type Box<Node<T>, Global>, surprisingly:

error[E0308]: mismatched types                                                                                                                                                
   --> src/lib.rs:76:7                                                                                                                                                        
    |                                                                                                                                                                         
71  |     let new_head = Box::into_raw(Box::new_in(                                                                                                                           
    |                                  ----------- arguments to this function are incorrect                                                                                   
...                                                                                                                                                                           
76  |       &self.alloc,                                                                                                                                                      
    |       ^^^^^^^^^^^ expected `Global`, found `&A`                                                                                                                         
    |                                                                                                                                                                         
    = note: expected struct `alloc::alloc::Global`                                                                                                                            
            found reference `&A`                                                                                                                                              
note: associated function defined here                                                                                                                                        
   --> /home/flippette/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:412:12                                             
    |                                                                                                                                                                         
412 |     pub fn new_in(x: T, alloc: A) -> Self                                                                                                                               
    |            ^^^^^^                                                                                                                                                       
                                                                                                                                                                              
For more information about this error, try `rustc --explain E0308`.

Specifying the type gives a different, similarly confusing error:

    let new_head = Box::<_, &A>::into_raw(Box::<_, &A>::new_in(
      Node { /* ... */ },
      &self.alloc,
    ));
error[E0599]: no function or associated item named `into_raw` found for struct `Box<_, &A>` in the current scope
   --> src/lib.rs:71:34
    |
71  |     let new_head = Box::<_, &A>::into_raw(Box::<_, &A>::new_in(
    |                                  ^^^^^^^^ function or associated item not found in `Box<_, &A>`
    |
note: if you're trying to build a new `Box<_, &A>` consider using one of the following associated functions:
      Box::<T, A>::new_in
      Box::<T, A>::try_new_in
      Box::<T, A>::new_uninit_in
      Box::<T, A>::try_new_uninit_in
      and 10 others
   --> /home/flippette/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:412:5
    |
412 | /     pub fn new_in(x: T, alloc: A) -> Self
413 | |     where
414 | |         A: Allocator,
    | |_____________________^
...
438 | /     pub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
439 | |     where
440 | |         A: Allocator,
    | |_____________________^
...
467 | /     pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
468 | |     where
469 | |         A: Allocator,
    | |_____________________^
...
500 | /     pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
501 | |     where
502 | |         A: Allocator,
    | |_____________________^
    = note: the function or associated item was found for
            - `Box<T>`
help: there is a method `into` with a similar name, but with different arguments
   --> /home/flippette/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/convert/mod.rs:454:5
    |
454 |     fn into(self) -> T;
    |     ^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0599`.

However, if I use the equivalent type allocator_api2::boxed::Box, the compiler correctly infers the type as allocator_api2::boxed::Box<Node<T>, &A>, and compilation succeeds.

Meta

rustc --version --verbose:

rustc 1.90.0-nightly (9982d6462 2025-07-20)
binary: rustc
commit-hash: 9982d6462bedf1e793f7b2dbd655a4e57cdf67d4
commit-date: 2025-07-20
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8

Backtrace

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions