ensure alignment is never 0. Must be at least 1.

This commit is contained in:
Brendan Hansknecht 2024-08-25 11:48:46 -07:00
parent e35e564aa6
commit 7997cf95e7
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
2 changed files with 8 additions and 7 deletions

View file

@ -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;

View file

@ -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
@ -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::*;