rust-lang / rust-lang/rust

Unable to match non_exhaustive enum tuple variants with rest pattern

Open
#130,891 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug D-confusing T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Tuple enum variants Tuple(i32) from external crates marked with #[non_exhaustive] should be able to be matched by the RestPattern Tuple(val, ..). But it fails to compile.

I tried this code:

// In an external lib crate
#[non_exhaustive]
pub enum ExtNonExhaustiveVariant {
    ExhaustiveUnit,
    #[non_exhaustive]
    Unit,
    #[non_exhaustive]
    Tuple(i32),
    #[non_exhaustive]
    StructNoField {},
    #[non_exhaustive]
    Struct {
        field: i32,
    },
}

// In the bin crate
fn main() {
    match ExtNonExhaustiveVariant::ExhaustiveUnit {
        ExtNonExhaustiveVariant::ExhaustiveUnit => 0, // OK
        ExtNonExhaustiveVariant::Unit { .. } => 0, // OK
        ExtNonExhaustiveVariant::Tuple(val, ..) => val, // FAIL
        ExtNonExhaustiveVariant::StructNoField { .. } => 0, // OK
        ExtNonExhaustiveVariant::Struct { field, .. } => field, // OK
        _ => 0,
    };
}

I expected to see this happen: No compile error.

Instead, this happened:

error[E0603]: tuple variant `Tuple` is private
  --> src/main.rs:9:34
   |
9  |         ExtNonExhaustiveVariant::Tuple(val, ..) => val,
   |                                  ^^^^^ private tuple variant
   |
note: the tuple variant `Tuple` is defined here
  --> /repro/lib/src/lib.rs:14:5
   |
13 |     #[non_exhaustive]
   |     ----------------- cannot be constructed because it is `#[non_exhaustive]`
14 |     Tuple(i32),
   |     ^^^^^

For more information about this error, try `rustc --explain E0603`.
error: could not compile `bin` (bin "bin") due to 1 previous error
Meta

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the error from the external library and binary examples in /repro/lib/src/lib.rs and src/main.rs with the reported rustc version. Trace how the compiler handles matching the non_exhaustive Tuple variant with Tuple(val, ..). Done means the example compiles without the E0603 error and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.