You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide suggestion for error when if let used with enum variant whose values are not initialized #101208
enumE{One(i32,i32)}fnmain(){let var = E::One;ifletE::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.
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).
Activity
lyming2007 commentedon Aug 30, 2022
Can you give the output of what it should look like?
fmease commentedon Aug 30, 2022
It should probably look similar to the suggestion given for
let x: Option<i32> = Some;andlet 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;(wheremakeis afn(i32) -> bool).lyming2007 commentedon Aug 31, 2022
@rustbot claim
lyming2007 commentedon Sep 20, 2022
commit the fix #102062
Rollup merge of rust-lang#107098 - compiler-errors:pat-mismatch-fn-ca…
Rollup merge of rust-lang#107098 - compiler-errors:pat-mismatch-fn-ca…
Rollup merge of rust-lang#107098 - compiler-errors:pat-mismatch-fn-ca…
Rollup merge of rust-lang#107098 - compiler-errors:pat-mismatch-fn-ca…