From ea1c733b844a16b4b5755d66420a6fc73d17d428 Mon Sep 17 00:00:00 2001 From: rzvxa Date: Sun, 15 Sep 2024 00:20:34 +0330 Subject: [PATCH] fix(ast_tools): fix miscalculation of enum layouts. --- tasks/ast_tools/src/passes/calc_layout.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tasks/ast_tools/src/passes/calc_layout.rs b/tasks/ast_tools/src/passes/calc_layout.rs index e66d6fc427ad2..80d962fe50c91 100644 --- a/tasks/ast_tools/src/passes/calc_layout.rs +++ b/tasks/ast_tools/src/passes/calc_layout.rs @@ -123,9 +123,14 @@ fn calc_enum_layout(ty: &mut Enum, ctx: &EarlyCtx) -> Result { acc } - let with_tag = |mut acc: KnownLayout| -> KnownLayout { - acc.consume_niches(ty.item.variants.len() as u128, true); - acc + let with_tag = |layout: KnownLayout| -> Result { + let niches = layout.niches(); + let layout = std::alloc::Layout::from_size_align(layout.size(), layout.align()) + .normalize()? + .pad_to_align(); + let mut layout = KnownLayout::new(layout.size(), layout.align(), niches); + layout.consume_niches(ty.item.variants.len() as u128, true); + Ok(layout) }; let layouts = collect_variant_layouts(ty, ctx)?; @@ -137,8 +142,8 @@ fn calc_enum_layout(ty: &mut Enum, ctx: &EarlyCtx) -> Result { .collect::>() .expect("already checked."); - let x32 = with_tag(layouts_x32.into_iter().fold(KnownLayout::default(), fold_layout)); - let x64 = with_tag(layouts_x64.into_iter().fold(KnownLayout::default(), fold_layout)); + let x32 = with_tag(layouts_x32.into_iter().fold(KnownLayout::default(), fold_layout))?; + let x64 = with_tag(layouts_x64.into_iter().fold(KnownLayout::default(), fold_layout))?; Ok(PlatformLayout(Layout::from(x64), Layout::from(x32))) }