Skip to content

Type annotations needed for closure with associated type #140603

Open
@camsteffen

Description

@camsteffen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions