Skip to content

Bad error message when closure parameter type can't be inferred #71732

Closed
@rbtcollins

Description

@rbtcollins

This is either a regression of #45685 or it was closed prematurely.

Fairly minimal reproducer:

use std::collections::hash_map::HashMap;

fn foo(parameters: &HashMap<String, String>) -> bool {
    parameters
        .get(&"key".into())
        .and_then(|found: &String| {
            Some(false)
        })
        .unwrap_or(false)
}

->

error[E0283]: type annotations needed for `&std::string::String`
 --> src/lib.rs:6:20
  |
6 |         .and_then(|found: &String| {
  |                    ^^^^^ consider giving this closure parameter the explicit type `&std::string::String`, where the type parameter `std::string::String` is specified
  |
  = note: cannot resolve `std::string::String: std::borrow::Borrow<_>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0283`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c8754ceba0148a4da807002ae99ffa25

I'm still not entirely sure what is actually wrong here - is it the lifetime as in #22340? Anyhow - the message doesn't refer to any T that needs to be specified, and the named explicit type is not sufficient to fix the issue even when given.

Happens with nightly too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler 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