Skip to content

Misleading message for mismatch of associated type #58092

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Feb 3, 2019
  2. estebank commented on Sep 26, 2019

    @estebank
    Contributor

    Current output for the first case:

    error[E0271]: type mismatch resolving `<X as Foo>::Item == f64`
      --> src/main.rs:16:5
       |
    13 | fn foo(_: impl Foo<Item = f64>) {}
       |    ---             ---------- required by this bound in `foo`
    ...
    16 |     foo(X);
       |     ^^^ expected i32, found f64
    

    No change in the second.

  3. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Sep 26, 2019
  4. added
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    on Oct 18, 2019
  5. estebank commented on Oct 18, 2019

    @estebank
    Contributor
  6. added
    D-confusingDiagnostics: Confusing error or lint that should be reworked.
    on Oct 18, 2019
  7. added 2 commits that reference this issue on Oct 31, 2019
    07b6b70
    959a563
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 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