-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
safe transmute: support Single
enums
#126358
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,30 +331,63 @@ pub(crate) mod rustc { | |
assert!(def.is_enum()); | ||
let layout = ty_and_layout.layout; | ||
|
||
if let Variants::Multiple { tag_field, .. } = layout.variants() { | ||
// For enums (but not coroutines), the tag field is | ||
// currently always the first field of the layout. | ||
assert_eq!(*tag_field, 0); | ||
} | ||
// Computes the variant of a given index. | ||
let layout_of_variant = |index| { | ||
let tag = cx.tcx.tag_for_variant((ty_and_layout.ty, index)); | ||
let variant_def = Def::Variant(def.variant(index)); | ||
let variant_ty_and_layout = ty_and_layout.for_variant(&cx, index); | ||
Self::from_variant(variant_def, tag, variant_ty_and_layout, layout.size, cx) | ||
}; | ||
|
||
let variants = def.discriminants(cx.tcx()).try_fold( | ||
Self::uninhabited(), | ||
|variants, (idx, ref discriminant)| { | ||
let tag = cx.tcx.tag_for_variant((ty_and_layout.ty, idx)); | ||
let variant_def = Def::Variant(def.variant(idx)); | ||
let variant_ty_and_layout = ty_and_layout.for_variant(&cx, idx); | ||
let variant = Self::from_variant( | ||
variant_def, | ||
tag, | ||
variant_ty_and_layout, | ||
layout.size, | ||
cx, | ||
// We consider three kinds of enums, each demanding a different | ||
// treatment of their layout computation: | ||
// 1. enums that are uninhabited | ||
// 2. enums for which all but one variant is uninhabited | ||
// 3. enums with multiple inhabited variants | ||
match layout.variants() { | ||
compiler-errors marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ if layout.abi.is_uninhabited() => { | ||
// Uninhabited enums are usually (always?) zero-sized. In | ||
// the (unlikely?) event that an uninhabited enum is | ||
// non-zero-sized, this assert will trigger an ICE, and this | ||
// code should be modified such that a `layout.size` amount | ||
// of uninhabited bytes is returned instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tried an enum like |
||
// | ||
// Uninhabited enums are currently implemented such that | ||
// their layout is described with `Variants::Single`, even | ||
// though they don't necessarily have a 'single' variant to | ||
// defer to. That said, we don't bother specifically | ||
// matching on `Variants::Single` in this arm because the | ||
// behavioral principles here remain true even if, for | ||
// whatever reason, the compiler describes an uninhabited | ||
// enum with `Variants::Multiple`. | ||
assert_eq!(layout.size, Size::ZERO); | ||
Ok(Self::uninhabited()) | ||
} | ||
Variants::Single { index } => { | ||
// `Variants::Single` on non-uninhabited enums denotes that | ||
// the enum delegates its layout to the variant at `index`. | ||
layout_of_variant(*index) | ||
} | ||
Variants::Multiple { tag_field, .. } => { | ||
// `Variants::Multiple` denotes an enum with multiple | ||
// inhabited variants. The layout of such an enum is the | ||
// disjunction of the layouts of its tagged variants. | ||
|
||
// For enums (but not coroutines), the tag field is | ||
// currently always the first field of the layout. | ||
assert_eq!(*tag_field, 0); | ||
|
||
let variants = def.discriminants(cx.tcx()).try_fold( | ||
Self::uninhabited(), | ||
|variants, (idx, ref discriminant)| { | ||
let variant = layout_of_variant(idx)?; | ||
Result::<Self, Err>::Ok(variants.or(variant)) | ||
}, | ||
)?; | ||
Result::<Self, Err>::Ok(variants.or(variant)) | ||
}, | ||
)?; | ||
|
||
return Ok(Self::def(Def::Adt(def)).then(variants)); | ||
return Ok(Self::def(Def::Adt(def)).then(variants)); | ||
} | ||
} | ||
} | ||
|
||
/// Constructs a `Tree` from a 'variant-like' layout. | ||
|
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
tests/ui/transmutability/enums/uninhabited_optimization.rs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//@ check-pass | ||
//! Tests that we do not regress rust-lang/rust#125811 | ||
#![feature(transmutability)] | ||
|
||
fn assert_transmutable<T>() | ||
where | ||
(): std::mem::BikeshedIntrinsicFrom<T> | ||
{} | ||
|
||
enum Uninhabited {} | ||
|
||
enum SingleInhabited { | ||
X, | ||
Y(Uninhabited) | ||
} | ||
|
||
enum SingleUninhabited { | ||
X(Uninhabited), | ||
Y(Uninhabited), | ||
} | ||
|
||
fn main() { | ||
assert_transmutable::<Uninhabited>(); | ||
assert_transmutable::<SingleInhabited>(); | ||
assert_transmutable::<SingleUninhabited>(); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this assertion was recommended by @RalfJung in #125811 (comment), and I agree with his assessment that it isn't a load-bearing assertion. An index mismatch here doesn't suggest a bug in computing the tag.
That said, removing this assertion is irrelevant to fixing the bug — the salient changes are all in
compiler/rustc_transmute/src/layout/tree.rs
. Those changes fix the ICE even with this assertion intact.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to replace the assertion by a comment, like