Type annotations needed for closure with associated type
Open
Nobody has claimed this yet.
A-closures
A-inference
C-enhancement
T-types
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
use std::marker::PhantomData;
fn test_good() -> Builder<String> {
// this compiles
Builder::default().with_func(|s| drop(s))
}
fn test_bad() -> Builder<String> {
// error[E0282]: type annotations needed
Builder::default().with_func(|s| drop(s.len()))
}
#[derive(Default)]
pub struct Builder<Foo>(PhantomData<Foo>);
impl<Foo> Builder<Foo> {
fn with_func(self, _: impl Fn(Foo)) -> Self {
self
}
}
I expect this to compile, but the test_bad function does not compile.
Meta
Rust Version: 1.86.0
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 reproducing the provided test_good and test_bad examples with Rust 1.86.0 and compare the compiler diagnostics. No repository file or test is named in the report, so tracing the type-inference path will be necessary; done means the test_bad example compiles without requiring an explicit closure parameter type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100