Skip to content

Diagnostics show different expected and found types #68220

Closed
@vallentin

Description

@vallentin

Found a case where the diagnostics show an incorrect type (missing &), when calling slice methods taking &T, if the slice is e.g. [&T], [&&T], etc.

Thus the diagnostics are incorrect for e.g. [&str], [&i32], [&&bool], but are correct for e.g. [i32].


Unexpected - Example 1

Calling contains on [&str] incorrectly with a &str (instead of &&str). Then the highlighted line says expected &str (wrong) and the note after says expected type &&str (correct).

let arr = ["A", "B", "C"];
arr.contains("B");

Diagnostics

error[E0308]: mismatched types                                             
  --> src\main.rs:24:18
   |
24 |     arr.contains("B");
   |                  ^^^ expected &str, found str
   |
   = note: expected type `&&str`
              found type `&'static str`

Unexpected - Example 2

Second example using a const [&i32] and calling binary_search incorrectly.

const ARR: [&i32; 3] = [&1, &2, &3];
ARR.binary_search(&2);

Diagnostics

error[E0308]: mismatched types                                            
  --> src\main.rs:22:23
   |
22 |     ARR.binary_search(&2);
   |                       ^^ expected &i32, found integer
   |
   = note: expected type `&&i32`
              found type `&{integer}

Unexpected - Example 3

const ARR: [&&bool; 3] = [&&true, &&true, &&true];
ARR.contains(&&true);

Diagnostics

error[E0308]: mismatched types
  --> src\main.rs:11:18
   |
11 |     ARR.contains(&&true);
   |                  ^^^^^^ expected &bool, found bool
   |
   = note: expected type `&&&bool`
              found type `&&bool`

Expected - Example 1

Just to reiterate, using [i32] instead of [&i32] results in the expected error.

let arr = [1, 2, 3];
arr.contains(2);

Diagnostics

error[E0308]: mismatched types                                            
  --> src\main.rs:32:18
   |
32 |     arr.contains(2);
   |                  ^
   |                  |
   |                  expected &{integer}, found integer
   |                  help: consider borrowing here: `&2`
   |
   = note: expected type `&{integer}`
              found type `{integer}`

$ rustc --version --verbose
rustc 1.40.0 (73528e339 2019-12-16)
binary: rustc
commit-hash: 73528e339aae0f17a15ffa49a8ac608f50c6cf14
commit-date: 2019-12-16
host: x86_64-pc-windows-msvc
release: 1.40.0
LLVM version: 9.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.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