From 7997cf95e7f8e3b1d65da0b9ebab7d2bf8f0df0a Mon Sep 17 00:00:00 2001 From: Brendan Hansknecht Date: Sun, 25 Aug 2024 11:48:46 -0700 Subject: [PATCH] ensure alignment is never 0. Must be at least 1. --- crates/compiler/gen_llvm/src/llvm/convert.rs | 3 ++- crates/compiler/mono/src/layout.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/compiler/gen_llvm/src/llvm/convert.rs b/crates/compiler/gen_llvm/src/llvm/convert.rs index 90ad7a56824..73eb20f3440 100644 --- a/crates/compiler/gen_llvm/src/llvm/convert.rs +++ b/crates/compiler/gen_llvm/src/llvm/convert.rs @@ -404,7 +404,8 @@ impl<'ctx> RocUnion<'ctx> { let mut width = self.data_width; // add padding between data and the tag id - width = round_up_to_alignment(width, tag_id_width); + let tag_id_alignment = tag_id_width.max(1); + width = round_up_to_alignment(width, tag_id_alignment); // add tag id width += tag_id_width; diff --git a/crates/compiler/mono/src/layout.rs b/crates/compiler/mono/src/layout.rs index 258b3bed038..bc02bb97d46 100644 --- a/crates/compiler/mono/src/layout.rs +++ b/crates/compiler/mono/src/layout.rs @@ -1027,7 +1027,7 @@ impl<'a> UnionLayout<'a> { tags.iter() .map(|field_layouts| LayoutRepr::struct_(field_layouts).alignment_bytes(interner)) .max() - .unwrap_or(0) + .unwrap_or(1) } pub fn allocation_alignment_bytes(&self, interner: &I) -> u32 @@ -1196,8 +1196,8 @@ impl Discriminant { } } - pub const fn alignment_bytes(&self) -> u32 { - self.stack_size() + pub fn alignment_bytes(&self) -> u32 { + self.stack_size().max(1) } pub const fn layout(&self) -> InLayout<'static> { @@ -2381,9 +2381,9 @@ impl<'a, 'b> Env<'a, 'b> { } } -pub const fn round_up_to_alignment(width: u32, alignment: u32) -> u32 { +pub fn round_up_to_alignment(width: u32, alignment: u32) -> u32 { match alignment { - 0 => width, + 0 => panic!("Alignment invalid: Got 0, but alignment must be at least 1"), 1 => width, _ => { if width % alignment > 0 { @@ -2770,7 +2770,7 @@ impl<'a> LayoutRepr<'a> { .iter() .map(|x| interner.get_repr(*x).alignment_bytes(interner)) .max() - .unwrap_or(0), + .unwrap_or(1), Union(variant) => { use UnionLayout::*;