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

Message reporting missing patterns for inexhaustive match is misleading #72377

Closed
vandenheuvel opened this issue May 20, 2020 · 4 comments · Fixed by #81897
Closed

Message reporting missing patterns for inexhaustive match is misleading #72377

vandenheuvel opened this issue May 20, 2020 · 4 comments · Fixed by #81897
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@vandenheuvel
Copy link
Contributor

A small demo:

#[derive(PartialEq, Eq, Clone, Copy)]
enum X { A, B, C, }

fn main() {
  match (X::A, Some(X::A)) {
    (_, None) => false,
    (v, Some(w)) if v == w => true,
    (X::B, Some(X::C)) => false,
    (X::B, Some(X::A)) => false,
    (X::A, Some(X::C)) | (X::C, Some(X::A)) => false,
  };
}

Playground.

I expected to see this happen
The usual error message indicating that patterns (A, Some(B)) and (C, Some(B)) are not covered.

Instead, this happened
Patterns (A, Some(A)) and (A, Some(B)) are detected as not covered.

rustc --version --verbose:

rustc 1.45.0-nightly (a74d1862d 2020-05-14)
binary: rustc
commit-hash: a74d1862d4d87a56244958416fd05976c58ca1a8
commit-date: 2020-05-14
host: x86_64-unknown-linux-gnu
release: 1.45.0-nightly
LLVM version: 9.0
@vandenheuvel vandenheuvel added the C-bug Category: This is a bug. label May 20, 2020
@jonas-schievink
Copy link
Contributor

If you add the suggested arms, it will in fact detect the other ones as missing. It is expected that (A, Some(A)) is detected as not covered since the match guard is not taken into account when checking exhaustiveness (and couldn't be, since it's an arbitrary expression).

@jonas-schievink
Copy link
Contributor

Closing as expected behavior.

@vandenheuvel
Copy link
Contributor Author

It is expected that (A, Some(A)) is detected as not covered since the match guard is not taken into account when checking exhaustiveness

Wouldn't you then expect that it mentions (A, Some(A)), (B, Some(B)), (C, Some(C)), (A, Some(B)) and (C, Some(B)) as not covered? Or is the number of outputs always restricted to two? In that case, I think it would be useful to indicate that some patterns might be missing, e.g.:

error[E0004]: non-exhaustive patterns: at least `(A, Some(A))` and `(A, Some(B))` not covered

@jonas-schievink
Copy link
Contributor

Yeah that's true

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels May 22, 2020
@vandenheuvel vandenheuvel changed the title Wrong match pattern is detected as not covered Message reporting missing patterns for inexhaustive match is misleading May 30, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this issue Feb 15, 2021
…ostics_regression_test, r=Mark-Simulacrum

Add match pattern diagnostics regression test

Closes rust-lang#72377 by adding a regression test.

This test case fails on stable but now works on beta and nightly. It *should* have worked already for years, the crucial point whether it is mentioned that some uncovered patterns are not explicitly mentioned.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Feb 15, 2021
…ostics_regression_test, r=Mark-Simulacrum

Add match pattern diagnostics regression test

Closes rust-lang#72377 by adding a regression test.

This test case fails on stable but now works on beta and nightly. It *should* have worked already for years, the crucial point whether it is mentioned that some uncovered patterns are not explicitly mentioned.
@bors bors closed this as completed in cd27820 Feb 16, 2021
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. 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