Skip to content

Provide suggestion for error when if let used with enum variant whose values are not initialized #101208

Closed

Description

@Rageking8

Given the following code: link

enum E {
    One(i32, i32)
}

fn main() {
    let var = E::One;
    if let E::One(var1, var2) = var {
        println!("{var1} {var2}");
    }
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:7:12
  |
7 |     if let E::One(var1, var2) = var {
  |            ^^^^^^^^^^^^^^^^^^   --- this expression has type `fn(i32, i32) -> E {E::One}`
  |            |
  |            expected fn item, found enum `E`
  |
  = note: expected fn item `fn(i32, i32) -> E {E::One}`
                found enum `E`

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

Ideally the output should suggest to the user that they need to initialize the enum variant's value in order to use the if let.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 30, 2022
  2. lyming2007 commented on Aug 30, 2022

    @lyming2007

    Can you give the output of what it should look like?

  3. fmease commented on Aug 30, 2022

    @fmease
    Member

    It should probably look similar to the suggestion given for

    • let x: Option<i32> = Some; and
    • let some = Some; let x: Option<i32> = some;

    So I guess sth. like:

      error[E0308]: mismatched types
       --> src/main.rs:7:12
        |
      7 |     if let E::One(var1, var2) = var {
        |            ^^^^^^^^^^^^^^^^^^   --- this expression has type `fn(i32, i32) -> E {E::One}`
        |            |
        |            expected fn item, found enum `E`
        |
        = note: expected fn item `fn(i32, i32) -> E {E::One}`
                      found enum `E`
    + help: use parentheses to instantiate this tuple variant
    +   |
    + 7 |     if let E::One(var1, var2) = var(_, _) {
    +   |                                    ++++++

    Edit: Or instead of saying “to instantiate this tuple variant” we could say “to call this function” like it's done for the erroneous snippet let x: bool = make; (where make is a fn(i32) -> bool).

  4. added
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
    on Aug 31, 2022
  5. lyming2007 commented on Aug 31, 2022

    @lyming2007

    @rustbot claim

  6. lyming2007 commented on Sep 20, 2022

    @lyming2007

    commit the fix #102062

  7. added a commit that references this issue on Feb 8, 2023
    0bd0eaf
  8. added a commit that references this issue on Feb 9, 2023
    72c634b
  9. added a commit that references this issue on Feb 10, 2023
    fd78269
  10. added a commit that references this issue on Feb 11, 2023
    400b03a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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