Skip to content

Misleading message for mismatch of associated type #58092

Closed
@statiolake

Description

@statiolake

In the compiler message for type mismatching error of associated type, "expected A, found B" should be reversed ("expected B, found A").

struct X;

trait Foo {
    type Item;
}

impl Foo for X {
    type Item = i32;
}

fn foo(_: impl Foo<Item = f64>) {}

fn main() {
    foo(X);
}

The above example shows the error below:

error[E0271]: type mismatch resolving `<X as Foo>::Item == f64`
  --> (filename):14:5
   |
14 |     foo(X);
   |     ^^^ expected i32, found f64
   |
note: required by `foo`
  --> (filename):11:1
   |
11 | fn foo(_: impl Foo<Item = f64>) {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.

Especially, compiler says "expected i32, found f64". However "expected f64, found i32" is more natural since the mismatching here is that the function foo() requires X::Item to be f64 but X::Item is actually i32.

Another example is below:

fn main() {
    let _ = vec![1, 2, 3].into_iter().cloned();
}

It causes an error like:

error[E0271]: type mismatch resolving `<std::vec::IntoIter<{integer}> as std::iter::Iterator>::Item == &_`
 --> (filename):2:39
  |
2 |     let _ = vec![1, 2, 3].into_iter().cloned();
  |                                       ^^^^^^ expected integer, found reference
  |
  = note: expected type `{integer}`
             found type `&_`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.

The error here is that Iterator::cloned() requires Iterator::Item to be a reference but the supplied iterator here (vec![1, 2, 3].into_iter()) is a iterator generating integers. However error message is shown as if Iterator::cloned() required something to be an integer but that was reference. The message should be "expected reference, found integer".

Meta

rustc --version --verbose:

rustc 1.33.0-nightly (b43986184 2019-01-11)
binary: rustc
commit-hash: b43986184b8f4e0d633e8ae1704f0e19aec30cb2
commit-date: 2019-01-11
host: x86_64-pc-windows-msvc
release: 1.33.0-nightly
LLVM version: 8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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