Skip to content

E0603 is misleading on non-exhaustive enums #82788

Open
@jhpratt

Description

@jhpratt

Given the following code: (no link, as not reproducible in a single file)

Library:

pub enum Foo {
    Alpha,
    #[non_exhaustive]
    Beta,
}

impl Foo {
    pub fn beta() -> Self {
        Self::Beta
    }
}

External user (in this case a test):

use example::Foo;

#[test]
fn demo() {
    assert!(matches!(
        Foo::beta(),
        Foo::Beta
    ));
}

The current output is:

error[E0603]: unit variant `Beta` is private
 --> tests/demo.rs:7:14
  |
7 |         Foo::Beta
  |              ^^^^ private unit variant
  |
note: the unit variant `Beta` is defined here
 --> /home/jhpratt/example/src/lib.rs:4:5
  |
4 |     Beta,
  |     ^^^^

Ideally the output should look like:

error[E0638]: `..` required with variant marked as non-exhaustive
 --> tests/demo.rs:7:9
  |
7 |         Foo::Beta
  |         ^^^^^^^^^
  |
help: add `..` at the end of the field list to ignore all other fields
  |
7 |         Foo::Beta { .. }

Right now, the cause of the error is misleading, as it seems to imply that an enum variant is private (which is impossible). The error message should be updated to match the error provided when there are some fields present, but not the .. as required. The exact wording might vary a bit, but I copied it verbatim here. Given the explanation of E0603, I believe that this is the wrong error code altogether.

@rustbot modify labels to +A-diagnostics +T-compiler +D-confusing +D-incorrect

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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