Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Confusing errors with missing let in if let #77238

Closed
camelid opened this issue Sep 26, 2020 · 2 comments · Fixed by #77283
Closed

Confusing errors with missing let in if let #77238

camelid opened this issue Sep 26, 2020 · 2 comments · Fixed by #77283
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@camelid
Copy link
Member

camelid commented Sep 26, 2020

I thought this code would provide one of the nice suggestions that @estebank implemented in #75931, but it doesn't. (By the way, making cache a reference to Cache will give an ICE because of #77218.)

pub struct Cache {
    data: Vec<i32>,
}

pub fn list_data(cache: Cache, key: &usize) {
    for reference in vec![1, 2, 3] {
        if /*let*/ Some(reference) = cache.data.get(*key) {
            unimplemented!()
        }
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:7:38
  |
7 |         if /*let*/ Some(reference) = cache.data.get(*key) {
  |                                      ^^^^^^^^^^^^^^^^^^^^ expected integer, found `&i32`
  |
  = note: expected enum `Option<{integer}>`
             found enum `Option<&i32>`

error[E0308]: mismatched types
 --> src/lib.rs:7:20
  |
7 |         if /*let*/ Some(reference) = cache.data.get(*key) {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2020
@estebank estebank self-assigned this Sep 28, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 26, 2020
…crum

Tweak `if let` suggestion to be more liberal with suggestion and to not ICE

Fix rust-lang#77218. Fix rust-lang#77238.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 26, 2020
…crum

Tweak `if let` suggestion to be more liberal with suggestion and to not ICE

Fix rust-lang#77218. Fix rust-lang#77238.
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 26, 2020
Tweak `if let` suggestion to be more liberal with suggestion and to not ICE

Fix rust-lang#77218. Fix rust-lang#77238.
@bors bors closed this as completed in cabf6d0 Oct 26, 2020
Mark-Simulacrum pushed a commit to Mark-Simulacrum/rust that referenced this issue Nov 6, 2020
@estebank
Copy link
Contributor

Reopening as per #77283 (comment)

@estebank estebank reopened this Jan 13, 2021
@estebank estebank removed their assignment Jan 13, 2021
@estebank
Copy link
Contributor

This is fixed in beta now:

error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find value `foo` in this scope
 --> src/main.rs:2:18
  |
2 |     if Some(3) = foo {}
  |                  ^^^ not found in this scope

error[[E0070]](https://doc.rust-lang.org/nightly/error-index.html#E0070): invalid left-hand side of assignment
 --> src/main.rs:2:16
  |
2 |     if Some(3) = foo {}
  |             -  ^
  |             |
  |             cannot assign to this expression

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:2:8
  |
2 |     if Some(3) = foo {}
  |        ^^^^^^^^^^^^^ expected `bool`, found `()`
  |
help: consider adding `let`
  |
2 |     if let Some(3) = foo {}
  |        +++
error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/lib.rs:7:25
  |
6 |     for reference in vec![1, 2, 3] {
  |         --------- expected due to the type of this binding
7 |         if /*let*/ Some(reference) = cache.data.get(*key) {
  |                         ^^^^^^^^^ expected integer, found `&i32`
  |
help: consider dereferencing the borrow
  |
7 |         if /*let*/ Some(*reference) = cache.data.get(*key) {
  |                         +

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/lib.rs:7:20
  |
7 |         if /*let*/ Some(reference) = cache.data.get(*key) {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
  |
help: consider adding `let`
  |
7 |         if /*let*/ let Some(reference) = cache.data.get(*key) {
  |                    +++

It is still quite verbose (too many errors), but we lead people in the right direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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.

2 participants