From fd5be23a96274cb1d1f83acdd8f7370ee15e00fa Mon Sep 17 00:00:00 2001 From: ouz-a Date: Sat, 15 Jan 2022 15:03:38 +0300 Subject: [PATCH] fix for the issue #92464 --- compiler/rustc_middle/src/ty/layout.rs | 5 ++++- src/test/ui/aligned_enum_cast.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/aligned_enum_cast.rs diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 4e6b2acb67f..f98261e5e5e 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -1310,7 +1310,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { }, }; let mut abi = Abi::Aggregate { sized: true }; - if tag.value.size(dl) == size { + + // Without latter check aligned enums with custom discriminant values + // Would result in ICE see the issue #92464 for more info + if tag.value.size(dl) == size || variants.iter().all(|layout| layout.is_empty()) { abi = Abi::Scalar(tag); } else { // Try to use a ScalarPair for all tagged enums. diff --git a/src/test/ui/aligned_enum_cast.rs b/src/test/ui/aligned_enum_cast.rs new file mode 100644 index 00000000000..4b5776a6aa8 --- /dev/null +++ b/src/test/ui/aligned_enum_cast.rs @@ -0,0 +1,15 @@ +// run-pass +// allows aligned custom discriminant enums to cast into other types +// See the issue #92464 for more info +#[allow(dead_code)] +#[repr(align(8))] +enum Aligned { + Zero = 0, + One = 1, +} + +fn main() { + let aligned = Aligned::Zero; + let fo = aligned as u8; + println!("foo {}",fo); +}