Skip to content

Pattern-matching on #[non_exhaustive] unit/tuple structs and variants causes confusing diagnostic #107165

Open
@obi1kenobi

Description

@obi1kenobi

Code

In crate A:

pub enum Foo {
    #[non_exhaustive]
    Unit,

    #[non_exhaustive]
    Tuple(i64),
}

In crate B:

use A::Foo;

fn use_foo(val: Foo) {
    match val {
        Foo::Unit => todo!(),
        Foo::Tuple(..) => todo!(),
    }
}

Current output

error[E0603]: unit variant `Unit` is private
   --> crate_b/src/main.rs:150:30
    |
150 |         Foo::Unit => todo!(),
    |              ^^^^ private unit variant
    |
note: the unit variant `Unit` is defined here
   --> /.../crate_a/src/lib.rs:22:5
    |
22  |     Unit,
    |     ^^^^

error[E0603]: tuple variant `Tuple` is private
   --> crate_b/src/main.rs:151:30
    |
151 |         Foo::Tuple(..) => todo!(),
    |              ^^^^^ private tuple variant
    |
note: the tuple variant `Tuple` is defined here
   --> /.../crate_a/src/lib.rs:25:5
    |
25  |     Tuple(i64),
    |     ^^^^^

For more information about this error, try `rustc --explain E0603`.
error: could not compile `crate_b` due to 2 previous errors

Desired output

The error message should reference #[non_exhaustive] rather than being implementation-centric. The "variant is private" message is unexpected, confusing the average user since variants don't take pub or other visibility specifiers.

The output should also suggest the fix: use a struct pattern instead, like Foo::Unit { .. } and Foo::Tuple{ 0: val, .. }.

Rationale and extra context

Originally a conversation on Mastodon, @estebank suggested I file an issue:
https://hachyderm.io/@predrag/109723971447778736

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-patternsRelating to patterns and pattern matchingD-papercutDiagnostics: An error or lint that needs small tweaks.E-help-wantedCall for participation: Help is requested to fix this issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions