Auto merge of #16285 - lnicola:repr-packed-unaligned, r=Veykril

fix: Fix panic on unaligned packed attribute

Closes #16284
This commit is contained in:
bors 2024-01-06 20:35:56 +00:00
commit e9ae0b7ab4
2 changed files with 31 additions and 1 deletions

View file

@ -128,7 +128,7 @@ fn parse_repr_tt(tt: &Subtree) -> Option<ReprOptions> {
} else { } else {
0 0
}; };
let pack = Align::from_bytes(pack).unwrap(); let pack = Align::from_bytes(pack).unwrap_or(Align::ONE);
min_pack = min_pack =
Some(if let Some(min_pack) = min_pack { min_pack.min(pack) } else { pack }); Some(if let Some(min_pack) = min_pack { min_pack.min(pack) } else { pack });
ReprFlags::empty() ReprFlags::empty()

View file

@ -220,6 +220,36 @@ fn recursive() {
); );
} }
#[test]
fn repr_packed() {
size_and_align! {
#[repr(packed)]
struct Goal;
}
size_and_align! {
#[repr(packed(2))]
struct Goal;
}
size_and_align! {
#[repr(packed(4))]
struct Goal;
}
size_and_align! {
#[repr(packed)]
struct Goal(i32);
}
size_and_align! {
#[repr(packed(2))]
struct Goal(i32);
}
size_and_align! {
#[repr(packed(4))]
struct Goal(i32);
}
check_size_and_align("#[repr(packed(5))] struct Goal(i32);", "", 4, 1);
}
#[test] #[test]
fn generic() { fn generic() {
size_and_align! { size_and_align! {