Skip to content

Add note explaining that the same variant of the same enum with different type params are different types #95284

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

Open
estebank opened this issue Mar 24, 2022 · 3 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Mar 24, 2022

As brought up in #51479, Result<A, i32>::Err(0) and Result<B, i32>::Err(0) are not of the same type. We should add a note (or ideally a structured suggestion on what to write) explaining this.

error[E0308]: `match` arms have incompatible types
 --> src/lib.rs:8:16
  |
6 | /     match f2() {
7 | |         Ok(value) => Ok(value as i64),
  | |                      ---------------- this is found to be of type `Result<i64, String>`
8 | |         err => err,
  | |                ^^^ expected `i64`, found `i32`
9 | |     }
  | |_____- `match` arms have incompatible types
  |
  = note: expected enum `Result<i64, _>`
             found enum `Result<i32, _>`
@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Mar 24, 2022
@SparrowLii
Copy link
Member

@rustbot claim

@SparrowLii
Copy link
Member

SparrowLii commented Mar 31, 2022

I tried to implement it but got stuck. The biggest question is how to determine that they are "the same variant". For example, in the above err => err statement, we can only know that err is of type Result<_, _> rather than the specific Result<_, _>::Err(..)variant.

I was going to use rustc_mir_build::thir::pattern::usefulness::compute_match_usefulness which checks whether a match is exhaustive. From the result returned by this function, we can know whether the pat in a certain arm can be determined to be a specific variant. But this function runs after typeck and needs the result of typeck, so it cannot be used in the current scenario.

@compiler-errors compiler-errors removed the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jun 19, 2022
@compiler-errors
Copy link
Member

Removing E-easy because this doesn't seem that easy. The fact that match arms can have arbitrary patterns does make this quite difficult to, e.g., suggest changing err => err to Err(x) => Err(x)... Curious what the structured suggestion would ideally look like here.

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-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants