From 1969aeb3d708237327239fdfb7da3d575601199e Mon Sep 17 00:00:00 2001 From: Austin Hicks Date: Fri, 18 Nov 2016 13:41:21 -0500 Subject: [PATCH] Make constant field access account for field reordering. --- src/librustc_trans/adt.rs | 11 ++++------- src/test/run-pass/nonzero-enum.rs | 4 +--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/librustc_trans/adt.rs b/src/librustc_trans/adt.rs index 0690bea6c2f8..c7a177196ffe 100644 --- a/src/librustc_trans/adt.rs +++ b/src/librustc_trans/adt.rs @@ -813,14 +813,11 @@ pub fn const_get_field<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, let l = ccx.layout_of(t); match *l { layout::CEnum { .. } => bug!("element access in C-like enum const"), - layout::Univariant { .. } | layout::Vector { .. } => const_struct_field(val, ix), + layout::Univariant { ref variant, .. } => { + const_struct_field(val, variant.gep_index[ix] as usize) + } + layout::Vector { .. } => const_struct_field(val, ix), layout::UntaggedUnion { .. } => const_struct_field(val, 0), - layout::General { .. } => const_struct_field(val, ix + 1), - layout::RawNullablePointer { .. } => { - assert_eq!(ix, 0); - val - }, - layout::StructWrappedNullablePointer{ .. } => const_struct_field(val, ix), _ => bug!("{} does not have fields.", t) } } diff --git a/src/test/run-pass/nonzero-enum.rs b/src/test/run-pass/nonzero-enum.rs index 2cd3136b0ebe..fc92c9df9f7e 100644 --- a/src/test/run-pass/nonzero-enum.rs +++ b/src/test/run-pass/nonzero-enum.rs @@ -26,9 +26,7 @@ fn main() { assert_eq!(size_of::(), 1); assert_eq!(size_of::>(), 1); assert_eq!(size_of::>(), 1); - // The next asserts are correct given the currently dumb field reordering algorithm, which actually makes this struct larger. - assert_eq!(size_of::(), 6); - assert_eq!(size_of::>(), 6); + assert_eq!(size_of::>(), size_of::()); let enone = None::; let esome = Some(E::A); if let Some(..) = enone {