Skip to content

"Type annotations needed" error is shown in a misleading wrong location #72690

Closed
@Ran4

Description

@Ran4

This compiles just fine:

fn main() {
    // vec!["hello"]
    //     .iter_mut()
    //     .map(|x| String::from(x.as_ref()))
    //     .collect::<Vec<String>>();

    vec![String::from("First"), String::from("Second")]
        .iter()
        .find(|s| *s == "Second");
}

But this gives a compiler error:

fn main() {
    vec!["hello"]
        .iter_mut()
        .map(|x| String::from(x.as_ref()))
        .collect::<Vec<String>>();

    vec![String::from("First"), String::from("Second")]
        .iter()
        .find(|s| *s == "Second");
}
error[E0283]: type annotations needed for `&&std::string::String`
 --> src/main.rs:9:16
  |
9 |         .find(|s| *s == "Second");
  |                ^ 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::convert::From<&_>`
  = note: required by `std::convert::From::from`

error: aborting due to previous error

error: could not compile `programname`.

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

It seems really weird that the second statement is affected like this?

Meta

$ rustc --version --verbose
rustc 1.43.1 (8d69840ab 2020-05-04)
binary: rustc
commit-hash: 8d69840ab92ea7f4d323420088dd8c9775f180cd
commit-date: 2020-05-04
host: x86_64-unknown-linux-gnu
release: 1.43.1
LLVM version: 9.0

Empty project (created with cargo new), unmodified Cargo.toml (so no extra dependencies):

[package]
name = "programname"
version = "0.1.0"
authors = ["..."]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

This issue has been assigned to @doctorn via this comment.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustcP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions