Skip to content

Type annotations needed for closure with associated type #140603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
camsteffen opened this issue May 3, 2025 · 1 comment
Open

Type annotations needed for closure with associated type #140603

camsteffen opened this issue May 3, 2025 · 1 comment
Labels
A-closures Area: Closures (`|…| { … }`) A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@camsteffen
Copy link
Contributor

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

@camsteffen camsteffen added the C-bug Category: This is a bug. label May 3, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 3, 2025
@moxian
Copy link
Contributor

moxian commented May 3, 2025

See also #71713 which is very similar (not sure if it is an exact duplicate)

@rustbot label: +T-types +A-inference +A-closures -C-bug +C-enhancement -needs-triage

@rustbot rustbot added A-closures Area: Closures (`|…| { … }`) A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants