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

When pattern doesn't match the type of if let expression, see if a field would be of the correct type and provide an apprpriate structured suggestion #81222

Closed
estebank opened this issue Jan 20, 2021 · 1 comment · Fixed by #81504
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-typesystem Area: The type system D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 20, 2021

Just encountered the following output:

error[E0308]: mismatched types
   --> compiler/rustc_passes/src/loops.rs:152:40
    |
152 |   ...                   if let hir::ExprKind::Path(hir::QPath::Resolved(
    |  ______________________________^
153 | | ...                       None,
154 | | ...                       Path { ident, res: hir::def::Res::Err, .. },
155 | | ...                   )) = break_expr
    | |                        ^   ---------- this expression has type `&&rustc_hir::Expr<'_>`
    | |________________________|
    |                          expected struct `rustc_hir::Expr`, found enum `rustc_hir::ExprKind`

It would be nice if we could peek at the fields of break_expr and provide an appropriate structured suggestion:

error[E0308]: mismatched types
   --> compiler/rustc_passes/src/loops.rs:152:40
    |
152 |   ...                   if let hir::ExprKind::Path(hir::QPath::Resolved(
    |  ______________________________^
153 | | ...                       None,
154 | | ...                       Path { ident, res: hir::def::Res::Err, .. },
155 | | ...                   )) = break_expr
    | |                        ^   ---------- this expression has type `&&rustc_hir::Expr<'_>`
    | |________________________|
    |                          expected struct `rustc_hir::Expr`, found enum `rustc_hir::ExprKind`
    |
help: you might have meant to use field `kind` of type `rustc_hir::ExprKind`
    |
155 | ...                   )) = break_expr.kind
    |                                      ^^^^^
@estebank estebank added A-typesystem Area: The type system 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. A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jan 20, 2021
@hkmatsumoto
Copy link
Member

@rustbot claim

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-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-typesystem Area: The type system D-papercut Diagnostics: An error or lint that needs small tweaks. 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