Skip to content

Spurious E0282 'type annotations needed' when other compile errors exist #88630

Open
@kpreid

Description

@kpreid

Given the following code:

fn troublesome() {
    does_not_exist();
}

fn innocent_bystander() {
    // Approximately what assert_eq! does, inlined for the sake of simplicity
    if "x".as_bytes() != &[] {
        panic!();
    }
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find function `does_not_exist` in this scope
 --> src/lib.rs:2:5
  |
2 |     does_not_exist();
  |     ^^^^^^^^^^^^^^ not found in this scope

error[E0282]: type annotations needed
 --> src/lib.rs:7:23
  |
7 |     if "x".as_bytes() != &[] {
  |                       ^^ cannot infer type

However, if does_not_exist(); is commented out, not only does its error E0425 disappear, the type inference error E0282 also does. This results in a frustrating situation where, whenever you have an error of the first sort, any ==s or assert_eq!()s elsewhere in your crate that are fragile in this way also flood the error output. This makes it unnecessarily difficult to practice the workflow of “change something in a breaking way, then fix all the now-erroneous call sites”. It could also mislead beginners because the compiler is confidently stating a falsehood.

Ideally, these “type annotations needed” errors would not appear unless the annotations were actually needed. An approximation of this would be to not show them when any other errors appear. An even better approximation, I speculate in ignorance of the compiler internals, would be to not show them if whatever inference pass would have resolved the concrete type is known to have been skipped due to previous errors.

Or, perhaps this code is inherently fraught, and it would be wise for the programmer to actually provide a type annotation. In that case, it would be nice if there was a warning about that when the code compiles successfully.


Tested on rustc 1.54.0 stable, also current nightly.

I previously mentioned this problem in #44345, but got no response there, and I also now think that this situation is significantly different. In particular, no trait implementations are being added, and the error is entirely spurious rather than being a newly-ambiguous trait implementation selection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceT-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