Skip to content

Type checker gives confusing tip when collecting std::str::Char into std::rc::Rc using std::from::From. #70959

Description

@hank-der-hafenarbeiter

Hey everyone,

I noticed that when collecting an std::str::Chars into a std::rc::Rc using Rc::from(...), the compiler tells you to annotate the type on your variable, when you actually need to annotate the type of your std::Iter::collect() call.

This code produces the bug:

fn main() {
    let s = "Hello, Github!!!!".to_string();
    let r:Rc<str> = Rc::from(s.chars().collect());
}

The compiler tells me to annotate the type of r, which already has a type annotation.

error[E0283]: type annotations needed for `std::rc::Rc<str>`
 --> src/main.rs:5:21
  |
5 |     let r:Rc<str> = Rc::from(s.chars().collect());
  |         -           ^^^^^^^^ cannot infer type for struct `std::rc::Rc<str>`
  |         |
  |         consider giving `r` a type
  |
  = note: cannot resolve `std::rc::Rc<str>: std::convert::From<_>`
  = note: required by `std::convert::From::from`

In actuality the fix is to annotate the collect()-call:

fn main() {
    let s = "Hello, Github!!!!".to_string();
    let r:Rc<str> = Rc::from(s.chars().collect::<String>());
}

Meta

I used the following version:

rustc --version --verbose:

rustc 1.44.0-nightly (1edd389cc 2020-03-23)
binary: rustc
commit-hash: 1edd389cc4c7b5be7a3dd4fe4b986f6017018e54
commit-date: 2020-03-23
host: x86_64-unknown-linux-gnu
release: 1.44.0-nightly
LLVM version: 9.0

The current beta (1.43.0-beta.5) and stable (1.42.0) on the rust playground produce the same output.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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