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

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

Closed
Rageking8 opened this issue Aug 30, 2022 · 4 comments · Fixed by #107098
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Rageking8
Copy link
Contributor

Rageking8 commented Aug 30, 2022

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.

@Rageking8 Rageking8 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 30, 2022
@lyming2007
Copy link

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

@fmease
Copy link
Member

fmease commented Aug 30, 2022

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).

@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Aug 31, 2022
@lyming2007
Copy link

@rustbot claim

@lyming2007
Copy link

commit the fix #102062

lyming2007 pushed a commit to lyming2007/rust that referenced this issue Oct 12, 2022
lyming2007 pushed a commit to lyming2007/rust that referenced this issue Oct 25, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 8, 2023
…ll, r=lcnr

Suggest function call on pattern type mismatch

Fixes rust-lang#101208

This could definitely be generalized to support more suggestions in pattern matches. We can't use all of [`FnCtxt::emit_type_mismatch_suggestions`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/fn_ctxt/struct.FnCtxt.html#method.emit_type_mismatch_suggestions), but it's on my to-do list to play around with more suggestions that would be productive in this position.
compiler-errors added a commit to compiler-errors/rust that referenced this issue Feb 9, 2023
…ll, r=lcnr

Suggest function call on pattern type mismatch

Fixes rust-lang#101208

This could definitely be generalized to support more suggestions in pattern matches. We can't use all of [`FnCtxt::emit_type_mismatch_suggestions`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/fn_ctxt/struct.FnCtxt.html#method.emit_type_mismatch_suggestions), but it's on my to-do list to play around with more suggestions that would be productive in this position.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 10, 2023
…ll, r=lcnr

Suggest function call on pattern type mismatch

Fixes rust-lang#101208

This could definitely be generalized to support more suggestions in pattern matches. We can't use all of [`FnCtxt::emit_type_mismatch_suggestions`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/fn_ctxt/struct.FnCtxt.html#method.emit_type_mismatch_suggestions), but it's on my to-do list to play around with more suggestions that would be productive in this position.
@bors bors closed this as completed in 400b03a Feb 11, 2023
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 D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants