Skip to content

Commit

Permalink
Merge pull request #7024 from roc-lang/align-1
Browse files Browse the repository at this point in the history
ensure alignment is never 0. Must be at least 1.
  • Loading branch information
bhansconnect committed Aug 25, 2024
2 parents 73a824d + 7997cf9 commit e8c7857
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion crates/compiler/gen_llvm/src/llvm/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions crates/compiler/mono/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>(&self, interner: &I) -> u32
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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::*;
Expand Down

0 comments on commit e8c7857

Please sign in to comment.