Skip to content

Incorrect compiler error message on mismatched types #43420

Closed
@isaacg1

Description

@isaacg1

I ran cargo build with some code that did not compile due to a type mismatch, error E0308. Help text was displayed, which was false/very misleading.

Here's a reduced test case:

fn foo(b: &[u16]) {}

fn main() {
    let a: Vec<u8> = Vec::new();
    foo(&a);
}

After running cargo build, the error displayed was:

   Compiling test-compile v0.1.0 (file:///home/isaac/prog/rust/test-compile)
error[E0308]: mismatched types
 --> src/main.rs:5:9
  |
5 |     foo(&a);
  |         ^^ expected slice, found struct `std::vec::Vec`
  |
  = note: expected type `&[u16]`
             found type `&std::vec::Vec<u8>`
  = help: here are some functions which might fulfill your needs:
          - .as_slice()

error: aborting due to previous error(s)

error: Could not compile `test-compile`.

Focusing in on the line: expected slice, found struct std::vec::Vec

This is incorrect, because std::vec::Vec dereferences into slice. The actual type mismatch, which should have been displayed, is between u16 and u8. I would expect the error to say:

expected u16, found u8

Meta:

I discovered this on nightly, but reproduced it on stable.

rustc --version --verbose:

rustc 1.19.0 (0ade339 2017-07-17)
binary: rustc
commit-hash: 0ade339
commit-date: 2017-07-17
host: x86_64-unknown-linux-gnu
release: 1.19.0
LLVM version: 4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions