-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
transmute: fix check for whether newtypes have equal size #155418
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -426,13 +426,42 @@ impl<'tcx> SizeSkeleton<'tcx> { | |
| } | ||
|
|
||
| ty::Adt(def, args) => { | ||
| // Only newtypes and enums w/ nullable pointer optimization. | ||
| // Only newtypes and enums w/ nullable pointer optimization (NPO). | ||
| if def.is_union() || def.variants().is_empty() || def.variants().len() > 2 { | ||
| return Err(err); | ||
| } | ||
| // Only default repr types. | ||
| { | ||
| // We can ignore the seed and some particular flags that can never affect the | ||
| // layout of newtypes / NPO types, but we have to check everything else. | ||
| // If you are adding a new field to `ReprOptions`, make sure to extend the check | ||
| // below so that we bail out if it is not at its default value! | ||
| let ReprOptions { int, align, pack, flags, scalable, field_shuffle_seed: _ } = | ||
| def.repr(); | ||
| let mut ignored_flags = ReprFlags::IS_TRANSPARENT | ||
| | ReprFlags::IS_LINEAR | ||
| | ReprFlags::RANDOMIZE_LAYOUT; | ||
| if def.is_struct() { | ||
| // `repr(C)` is only okay for structs, not for enums. | ||
| // Below, the *only* thing we do for structs is propagating | ||
| // `SizeSkeleton::Pointer`. We do *not* assume that `repr(C)` preserved | ||
| // ZST-ness (which might stop being true eventually). | ||
|
Contributor
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. I don't understand this -- why is repr(C) not ok for enums? In general I'm finding this code hard to understand. It's pre-existing, but I'd really like a comment explaining why the logic makes sense.
Member
Author
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.
Because
I have tried to make the comments more clear. Happy to add more details if you have more concrete things you'd like to see clarified. |
||
| ignored_flags |= ReprFlags::IS_C; | ||
| } | ||
| if int.is_some() | ||
| || align.is_some() | ||
| || pack.is_some() | ||
| || flags.difference(ignored_flags) != ReprFlags::default() | ||
| || scalable.is_some() | ||
| { | ||
| return Err(err); | ||
| } | ||
| } | ||
|
|
||
| // Get a zero-sized variant or a pointer newtype. | ||
| let zero_or_ptr_variant = |i| { | ||
| // Returns `Ok(None)` for 1-ZST types, `Ok(Some)` if (ignoring all 1-ZST fields) | ||
| // there's just a single pointer, and `Err` otherwise. | ||
| let zero_or_ptr_variant = |i| -> Result<Option<SizeSkeleton<'tcx>>, _> { | ||
| let i = VariantIdx::from_usize(i); | ||
| let fields = | ||
| def.variant(i).fields.iter().map(|field| { | ||
|
|
@@ -461,7 +490,8 @@ impl<'tcx> SizeSkeleton<'tcx> { | |
| }; | ||
|
|
||
| let v0 = zero_or_ptr_variant(0)?; | ||
| // Newtype. | ||
| // Single-variant case: Check if this is a newtype around a pointer. | ||
| // Such types are themselves pointer-sized. | ||
| if def.variants().len() == 1 { | ||
| if let Some(SizeSkeleton::Pointer { non_zero, tail }) = v0 { | ||
| return Ok(SizeSkeleton::Pointer { non_zero, tail }); | ||
|
|
@@ -471,7 +501,9 @@ impl<'tcx> SizeSkeleton<'tcx> { | |
| } | ||
|
|
||
| let v1 = zero_or_ptr_variant(1)?; | ||
| // Nullable pointer enum optimization. | ||
| // 2-variant case: Check if one variant is a *non-zero* pointer and the other a | ||
| // 1-ZST. Such types are eligible to for the nullable pointer enum optimization, so | ||
| // they are themselves pointer-sized. | ||
| match (v0, v1) { | ||
| (Some(SizeSkeleton::Pointer { non_zero: true, tail }), None) | ||
| | (None, Some(SizeSkeleton::Pointer { non_zero: true, tail })) => { | ||
|
|
||
This file was deleted.
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 think this should have examples of actual Rust types and show why this logic is doing what it's doing.
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.
What kinds of examples are you looking for?