-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Treat repr(Rust) univariant fieldless enums as ZSTs #49513
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b7c8cc4
Properly look for uninhabitedness when handling discriminants
nox 1839ec5
Consistently use C_uint_big for discriminants
nox 2807f4f
Properly evaluate zst enum
oli-obk 7bfe3ae
Add a test for casts of univariant C-like enums
nox 8f36804
Treat repr(Rust) univariant fieldless enums as a ZST (fixes #15747)
nox 1c09977
Mark SingleVariant as repr(u8) in c-style-enum
nox 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 |
---|---|---|
|
@@ -669,6 +669,23 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M | |
(Value::ByVal(_), _) => bug!("expected fat ptr"), | ||
} | ||
} else { | ||
let src_layout = self.layout_of(src.ty)?; | ||
match src_layout.variants { | ||
layout::Variants::Single { index } => { | ||
if let Some(def) = src.ty.ty_adt_def() { | ||
let discr_val = def | ||
.discriminant_for_variant(*self.tcx, index) | ||
.val; | ||
return self.write_primval( | ||
dest, | ||
PrimVal::Bytes(discr_val), | ||
dest_ty); | ||
} | ||
} | ||
layout::Variants::Tagged { .. } | | ||
layout::Variants::NicheFilling { .. } => {}, | ||
} | ||
|
||
let src_val = self.value_to_primval(src)?; | ||
let dest_val = self.cast_primval(src_val, src.ty, dest_ty)?; | ||
let valty = ValTy { | ||
|
@@ -850,10 +867,16 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M | |
) -> EvalResult<'tcx, u128> { | ||
let layout = self.layout_of(ty)?; | ||
trace!("read_discriminant_value {:#?}", layout); | ||
if layout.abi == layout::Abi::Uninhabited { | ||
return Ok(0); | ||
} | ||
|
||
match layout.variants { | ||
layout::Variants::Single { index } => { | ||
return Ok(index as u128); | ||
let discr_val = ty.ty_adt_def().map_or( | ||
index as u128, | ||
|def| def.discriminant_for_variant(*self.tcx, index).val); | ||
return Ok(discr_val); | ||
} | ||
layout::Variants::Tagged { .. } | | ||
layout::Variants::NicheFilling { .. } => {}, | ||
|
@@ -1316,6 +1339,15 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M | |
pub fn try_read_value(&self, ptr: Pointer, ptr_align: Align, ty: Ty<'tcx>) -> EvalResult<'tcx, Option<Value>> { | ||
use syntax::ast::FloatTy; | ||
|
||
let layout = self.layout_of(ty)?; | ||
// do the strongest layout check of the two | ||
let align = layout.align.max(ptr_align); | ||
self.memory.check_align(ptr, align)?; | ||
|
||
if layout.size.bytes() == 0 { | ||
return Ok(Some(Value::ByVal(PrimVal::Undef))); | ||
} | ||
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. What is this, @oli-obk? 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. This prevents this function from returning |
||
|
||
let ptr = ptr.to_ptr()?; | ||
let val = match ty.sty { | ||
ty::TyBool => { | ||
|
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
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
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
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.
I feel like this should be using
read_discriminant_value
, just to get the correct value withoutdiscriminant_for_variant
being called in multiple places.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 don't have a
Place
there, so I can't useread_discriminant_value
.