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

Unable to match non_exhaustive enum tuple variants with rest pattern #130891

Open
SpriteOvO opened this issue Sep 26, 2024 · 1 comment
Open

Unable to match non_exhaustive enum tuple variants with rest pattern #130891

SpriteOvO opened this issue Sep 26, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

@SpriteOvO
Copy link
Contributor

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
@SpriteOvO SpriteOvO added the C-bug Category: This is a bug. label Sep 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 26, 2024
@ehuss
Copy link
Contributor

ehuss commented Sep 26, 2024

Thanks for the report! This behavior is (currently?) intentional. It has to do with privacy controls (you can read some of the gritty details in the RFC discussions at rust-lang/rfcs#2008). For now, you should be able to do ExtNonExhaustiveVariant::Tuple{0: val, ..}. Perhaps the diagnostics could suggest that?

@lolbinarycat lolbinarycat added A-diagnostics Area: Messages for errors, warnings, and lints T-lang Relevant to the language team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 27, 2024
@bjorn3 bjorn3 marked this as a duplicate of #134167 Dec 12, 2024
@bjorn3 bjorn3 marked this as not a duplicate of #134167 Dec 12, 2024
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-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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