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

Exhaustiveness checker errors on unconstructible enum variant #69696

Closed
matprec opened this issue Mar 4, 2020 · 4 comments
Closed

Exhaustiveness checker errors on unconstructible enum variant #69696

matprec opened this issue Mar 4, 2020 · 4 comments
Labels
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. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@matprec
Copy link
Contributor

matprec commented Mar 4, 2020

use std::marker::PhantomData;

enum Void {}

enum Foo<T=()> {
    Bar,
    Baz,
    Phantom(PhantomData<T>, Void)
}

pub fn test(f: Foo) {
    match f {
        Foo::Bar => println!("Bar"),
        Foo::Baz => println!("Baz"),
    }
}

Playground

I expect this to work, since the Phantom case is not constructible, since it contains a Void type.

The exhaustiveness checker instead errors because it thinks that the Phantom case should be covered as well:

error[E0004]: non-exhaustive patterns: `Phantom(_, _)` not covered
  --> src/lib.rs:12:11
   |
5  | / enum Foo<T=()> {
6  | |     Bar,
7  | |     Baz,
8  | |     Phantom(PhantomData<T>, Void)
   | |     ------- not covered
9  | | }
   | |_- `Foo` defined here
...
12 |       match f {
   |             ^ pattern `Phantom(_, _)` not covered
   |
   = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error: aborting due to previous error

Meta

Happens on stable and nightly as of 2020-03-04

@matprec matprec added the C-bug Category: This is a bug. label Mar 4, 2020
@Alexendoo Alexendoo added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Mar 4, 2020
@hellow554
Copy link
Contributor

hellow554 commented Mar 4, 2020

But it's correct. Although it can't be constructed, you have to use _ => {} for an exhaustive match.
This construct can be used as a workaround for older compiler, that don't support the #[non_exhaustive] attribute.
So I think the complain from the compiler is valid?!

@matprec
Copy link
Contributor Author

matprec commented Mar 4, 2020

@hellow554 I don't think that this is a proper workaround for the #[non_exhaustive] attribute, since it doesn't forbid matching explicitly against the Phantom variant. The RFC cites #[doc(hidden)] in the How we do this today section.

@matprec
Copy link
Contributor Author

matprec commented Mar 4, 2020

I'd argue that this should be valid, since the error states its intention as "help: ensure that all possible cases are being handled" (emphasis mine), yet the Phantom variant is not "possible".

Of course, the error message could simply be adjusted, but i find it unintuitive having to create a case for a variant that simply cannot occur at runtime.

@Centril
Copy link
Contributor

Centril commented Mar 4, 2020

This compiles if you use #![feature(exhaustive_patterns)], so I'm closing in favor of #51085.

@Centril Centril closed this as completed Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants