Skip to content

Diagnostics show different expected and found types #68220

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
vallentin opened this issue Jan 14, 2020 · 0 comments · Fixed by #106399
Closed

Diagnostics show different expected and found types #68220

vallentin opened this issue Jan 14, 2020 · 0 comments · Fixed by #106399
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@vallentin
Copy link
Contributor

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
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2020
@varkor varkor added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Jan 14, 2020
@bors bors closed this as completed in f361413 Jan 31, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants