Skip to content
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

Confusing error suggests giving a type to an unrelated variable #77982

Closed
WaffleLapkin opened this issue Oct 15, 2020 · 0 comments · Fixed by #78111
Closed

Confusing error suggests giving a type to an unrelated variable #77982

WaffleLapkin opened this issue Oct 15, 2020 · 0 comments · Fixed by #78111
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@WaffleLapkin
Copy link
Member

I tried this code:

use std::collections::HashMap;

fn what() {
    let descr = String::new();
    let mut opts = HashMap::new();
    let opt = String::new();
    
    // To fix the error, `as_ref` should be replaced with `as_str`
    if let None = opts.get(opt.as_ref()) {
        opts.insert(opt, ());
    }
}

(playground)

I expected to see this happen: error pointing to opt.as_ref()

Instead, this happened: error points to descr which is unrelated to the error

error[E0283]: type annotations needed for `String`
 --> src/lib.rs:8:24
  |
4 |     let descr = String::new();
  |         ----- consider giving `descr` a type
...
8 |     if let None = opts.get(opt.as_ref()) {
  |                        ^^^ cannot infer type for struct `String`
  |
  = note: cannot satisfy `String: Borrow<_>`

Meta

Tested with rustc 1.47.0 (18bf6b4f0 2020-10-07) and rustc 1.49.0-nightly (2020-10-14 e160e5cb80652bc2afe7)

@WaffleLapkin WaffleLapkin added the C-bug Category: This is a bug. label Oct 15, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 15, 2020
@jyn514 jyn514 added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Oct 15, 2020
@bors bors closed this as completed in 3f1c637 Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants