Spurious type mismatches with function returning async closure returning a boxed trait object
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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) }
}
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the mismatch from the example in src/lib.rs or the linked Rust Playground, comparing make_work_builder with the compiling synchronous and wrapped variants. Start by tracing async-closure return-type checking and the boxed trait-object expectation. Done means the first example compiles correctly and the regression is covered by an appropriate compiler test.
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
- 35/100