Skip to content

type annotation warning involving completely unrelated scopes #72616

Closed
@matthiaskrgr

Description

@matthiaskrgr

I tried this code:

use std::convert::{TryFrom, TryInto};

pub fn main() {
    {
        let _: String = "".to_owned().try_into().unwrap();
    }
    {
        if String::from("a") == "a".try_into().unwrap() {}
    }
    {
        let _: String = match "_".try_into() {
            Ok(a) => a,
            Err(_) => "".into(),
        };
    }
}

This caused an error

error[E0283]: type annotations needed for `std::string::String`
 --> src/main.rs:8:30
  |
5 |         let _: String = "".to_owned().try_into().unwrap();
  |             - consider giving this pattern a type
...
8 |         if String::from("a") == "a".try_into().unwrap() {}
  |                              ^^ cannot infer type for struct `std::string::String`
  |
  = note: cannot satisfy `std::string::String: std::cmp::PartialEq<_>`

however there is not relation between lines 5 and lines 8 as far as I understand which makes this weird.
The lines are not even in the same scope.

Meta

rustc --version --verbose:

1.45.0-nightly
(2020-05-25 f93bb2a50b37bc8bafe4)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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