rust-lang / rust-lang/rust

E0599 for incorrect boxing of closure is confusing

Open
#143,950 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-confusing T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

given the following (rustc test.rs):

fn main() {
    let f: Box<dyn Fn()> = Box::new(|| {});
    let g = Box::<dyn Fn()>::new(|| {});
}

the error for g is as follows:

error[E0599]: the function or associated item `new` exists for struct `Box<dyn Fn()>`, but its trait bounds were not satisfied
 --> test.rs:3:30
  |
3 |     let g = Box::<dyn Fn()>::new(|| {});
  |                              ^^^ function or associated item cannot be called on `Box<dyn Fn()>` due to unsatisfied trait bounds
  |
note: if you're trying to build a new `Box<dyn Fn()>` consider using one of the following associated functions:
      Box::<T>::from_raw
      Box::<T>::from_non_null
      Box::<T, A>::from_raw_in
      Box::<T, A>::from_non_null_in
 --> /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/alloc/src/boxed.rs:1044:5
  = note: the following trait bounds were not satisfied:
          `dyn Fn(): Sized`

error: aborting due to 1 previous error

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

maybe this is just a skill issue, because i’ll admit i still don’t actually understand why f is allowed and g is not, but the error message was misleading.

i was pretty confident that from_raw was not the way to go, so next i tried changing dyn Fn() to dyn Fn() + Sized, which yielded E0225. in the end, i had to google “how to box closure rust” to learn that the trait object type should go in the binding, not in a turbofish.

Meta

rustc --version --verbose:

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5

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

Reproduce the diagnostic with the test.rs example using rustc, then start by tracing how E0599 is produced for Box<dyn Fn()> and how its associated-function suggestions are selected. Done means the incorrect turbofish form receives guidance that distinguishes it from the valid binding form without presenting from_raw as the likely solution.

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
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.