When matching on enums, read the discriminant even if there's only a single variant#154756
Open
meithecatte wants to merge 3 commits intorust-lang:mainfrom
Open
When matching on enums, read the discriminant even if there's only a single variant#154756meithecatte wants to merge 3 commits intorust-lang:mainfrom
meithecatte wants to merge 3 commits intorust-lang:mainfrom
Conversation
Collaborator
|
The Miri subtree was changed cc @rust-lang/miri Some changes occurred in match lowering cc @Nadrieril |
Collaborator
|
r? @mati865 rustbot has assigned @mati865. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a follow-up to #150681, where it has been decided that this change should land in a separate PR.
This PR removes a special case, so that matching on enums that only have a single variant still emits a discriminant read in MIR. This is motivated by some weirdness being caused by this special case:
If a single-variant enum is marked with
#[repr(u8)], it will get an actual discriminant that's there in memory, and if it is set to an invalid value or left uninitialized, we would like to say that's UB. Recall the example from the previous PR:The presence of the discriminant read changes the behavior of closure captures, and can therefore be observed through the borrow checker and drop order. As such,
#[non_exhaustive]on a single-variant enum must force a discriminant read, just like if the enum had multiple variants. However, having the runtime semantics depend on the presence of the#[non_exhaustive]attribute is not ideal. Moreover, it creates edge cases where removing a#[non_exhaustive]attribute from a public enum is a breaking change.Fixes #147722.
Fixes #151786.
Fixes rust-lang/cargo#16417.
From a previous attempt at this change, we have had a couple of regressions on crater – though some of those already got addressed. A preliminary glance suggests that none of the issues should be hard to fix though.