rust-lang / rust-lang/rust

Type annotations needed for closure with associated type

Open
#140,603 1 comment 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.