Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove exception WRT same-crate non_exhaustive reads
When matching against a single-variant enum marked `non_exhaustive`
and defined in a different crate, we pretend that there might be
multiple variants and force a read of the discriminant.  In [Rust PR
150681], we decided to remove the same-crate exception.  Now matching
against a single-variant `non_exhaustive` enum in the same crate will
also cause a discriminant read.

Separately, prior to [Rust PR 150681], `rustc` was eliding the
discriminant read when an enum has only one *inhabited* variant.  This
contradicts what the Reference says in:

> r[type.closure.capture.precision.discriminants.uninhabited-variants]
>
> Even if all variants but the one being matched against are
> uninhabited, making the pattern [irrefutable][patterns.refutable],
> the discriminant is still read if it otherwise would be.

We decided with our lang FCP to confirm the behavior already encoded
in the Reference, so no change is needed there.

[Rust PR 150681]: rust-lang/rust#150681
  • Loading branch information
traviscross committed Feb 5, 2026
commit d2faa486394799bf8947b168dec0b61b7c6d8c50
2 changes: 1 addition & 1 deletion src/types/closure.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ c();
```

r[type.closure.capture.precision.discriminants.non_exhaustive]
If [`#[non_exhaustive]`][attributes.type-system.non_exhaustive] is applied to an enum defined in an external crate, the enum is treated as having multiple variants for the purpose of deciding whether a read occurs, even if it actually has only one variant.
If [`#[non_exhaustive]`][attributes.type-system.non_exhaustive] is applied to an enum, the enum is treated as having multiple variants for the purpose of deciding whether a read occurs, even if it actually has only one variant.

r[type.closure.capture.precision.discriminants.uninhabited-variants]
Even if all variants but the one being matched against are uninhabited, making the pattern [irrefutable][patterns.refutable], the discriminant is still read if it otherwise would be.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, I feel like the "if it otherwise would be" here can be kind of confusing... do you have any ideas on how to rephrase this? Maybe this would be an improvement?

Suggested change
Even if all variants but the one being matched against are uninhabited, making the pattern [irrefutable][patterns.refutable], the discriminant is still read if it otherwise would be.
Even if all variants but the one being matched against are uninhabited, making the pattern [irrefutable][patterns.refutable], the discriminant is still read when it otherwise would be.

Expand Down