Skip to content

Spurious type mismatches with function returning async closure returning a boxed trait object #145105

@tim3z

Description

@tim3z

I tried this code:

type BoxedWork = Box<dyn Fn(u32) -> u32>;

// error[E0308]: mismatched types
fn make_work_builder() -> impl AsyncFn() -> BoxedWork {
    async || Box::new(move |x| x + 1)
}

// With a regular `Fn` all is fine.
fn make_sync_work_builder() -> impl Fn() -> BoxedWork {
    || Box::new(|x| x + 1)
}

// Wrapping the Box into a struct fixes it. WTF.
struct WrappedWork { work: BoxedWork }
fn make_wrapped_work_builder() -> impl AsyncFn() -> WrappedWork {
    async || WrappedWork { work: Box::new(|x| x + 1) }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b508bb1c161e3e4aedca432175026d5c

I expected to see this happen: It compiles.

Instead, this happened: The first function make_work_builder fails to compile with

error[E0308]: mismatched types
 --> src/lib.rs:5:14
  |
5 |     async || Box::new(move |x| x + 1)
  |              ^^^^^^^^^^^^^^^^^^^^^^^^ expected `dyn Fn`, found closure
  |
  = note: expected `async` closure body `{async closure body@src/lib.rs:5:14: 5:38}` (`dyn Fn`)
             found `async` closure body `{async closure body@src/lib.rs:5:14: 5:38}` (closure)
  = note: no two async blocks, even if identical, have the same type
  = help: consider pinning your async block and casting it to a trait object

This seems wrong, especially considering that the latter two examples compile just fine.

Meta

Reproduces on playground with current stable 1.89 but also in my local project with 1.88

rustc --version --verbose:

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: aarch64-apple-darwin
release: 1.88.0
LLVM version: 20.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions