Closure parameter type is not inferred through uniquely resolving trait bound involving `FnOnce(A::State)`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
use std::marker::PhantomData;
fn main() {
Builder::<MyFold>(PhantomData).enforce(|x| {
if !x {}
});
}
trait Fold {
type State;
}
struct MyFold;
impl Fold for MyFold {
type State = bool;
}
trait Enforce<F, S> {}
impl<T, A> Enforce<(A,), (A::State,)> for T
where
A: Fold,
T: FnOnce(A::State),
{
}
struct Builder<A>(PhantomData<A>);
impl<A: Fold> Builder<A> {
fn enforce<E>(self, _: E)
where
E: Enforce<(A,), (A::State,)>,
{
}
}
I expected to see this happen:
The closure parameter x should be inferred as bool, because:
A = MyFoldMyFold::State = bool- the only applicable
Enforce<(A,), (A::State,)>impl requiresT: FnOnce(bool)
This compiles successfully if I explicitly annotate the closure parameter:
.enforce(|x: bool| {
if !x {}
});
Instead, this happened:
The compiler fails to infer the closure parameter type and emits:
error[E0282]: type annotations needed
--> src/main.rs:4:42
|
4 | Builder::<MyFold>(PhantomData).enforce(|x| {
| ^^
5 | if !x {}
| -- type must be known at this point
This suggests closure argument type inference is not propagating through the trait bound, even though the bound appears to uniquely determine the parameter type.
Meta
Tested on:
- stable 1.95.0
- nightly 1.97.0-nightly
rustc --version --verbose:
rustc 1.95.0 (59807616e 2026-04-14)
binary: rustc
commit-hash: 59807616e1fa2540724bfbac14d7976d7e4a3860
commit-date: 2026-04-14
host: x86_64-unknown-linux-gnu
release: 1.95.0
LLVM version: 22.1.2
and
rustc 1.97.0-nightly (66da6cae1 2026-04-20)
binary: rustc
commit-hash: 66da6cae1a6f12e9585493ab8f8f19cf753091fd
commit-date: 2026-04-20
host: x86_64-unknown-linux-gnu
release: 1.97.0-nightly
LLVM version: 22.1.2
Backtrace
No backtrace produced (compile-time type inference error).
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
Start by compiling the reproducer in src/main.rs with the stable and nightly rustc versions listed in the issue, then inspect how closure argument inference handles the Enforce<A, S> bound and its FnOnce(A::State) requirement. Done means the unannotated closure parameter is inferred as bool without changing the reproducer's trait definitions.
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
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100