Skip to content

Non-exhaustive patterns error when matching reference to empty enum #78123

Closed
@HactarCE

Description

@HactarCE

matching a value of an empty enum is valid, however matching a reference to a value of an empty enum produces E0004: non-exhaustive patterns.

I tried this code (playground link):

enum A {}

fn f(a: &A) -> usize {
    match a {}
}

I expected to see this happen: The code should compile with no issue, and the function should never actually execute because A has no variants and thus cannot be instantiated.

Instead, this happened:

error[E0004]: non-exhaustive patterns: type `&A` is non-empty
 --> src/lib.rs:4:11
  |
1 | enum A {}
  | --------- `A` defined here
...
4 |     match a {}
  |           ^
  |
  = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
  = note: the matched value is of type `&A`

Meta

The bug exists on 1.47.0 and 1.49.0-nightly (2020-10-18 b1496c6) running on the playground. I first found this bug on 1.46.0.

rustc --version --verbose:

rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-pc-windows-msvc
release: 1.46.0
LLVM version: 10.0

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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