Skip to content

E0599 for incorrect boxing of closure is confusing #143950

@delan

Description

@delan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions