Skip to content

Commit e9ae0b7

Browse files
committed
Auto merge of rust-lang#16285 - lnicola:repr-packed-unaligned, r=Veykril
fix: Fix panic on unaligned packed attribute Closes rust-lang#16284
2 parents f8eab9b + d17156a commit e9ae0b7

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

crates/hir-def/src/data/adt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn parse_repr_tt(tt: &Subtree) -> Option<ReprOptions> {
128128
} else {
129129
0
130130
};
131-
let pack = Align::from_bytes(pack).unwrap();
131+
let pack = Align::from_bytes(pack).unwrap_or(Align::ONE);
132132
min_pack =
133133
Some(if let Some(min_pack) = min_pack { min_pack.min(pack) } else { pack });
134134
ReprFlags::empty()

crates/hir-ty/src/layout/tests.rs

+30
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,36 @@ fn recursive() {
220220
);
221221
}
222222

223+
#[test]
224+
fn repr_packed() {
225+
size_and_align! {
226+
#[repr(packed)]
227+
struct Goal;
228+
}
229+
size_and_align! {
230+
#[repr(packed(2))]
231+
struct Goal;
232+
}
233+
size_and_align! {
234+
#[repr(packed(4))]
235+
struct Goal;
236+
}
237+
size_and_align! {
238+
#[repr(packed)]
239+
struct Goal(i32);
240+
}
241+
size_and_align! {
242+
#[repr(packed(2))]
243+
struct Goal(i32);
244+
}
245+
size_and_align! {
246+
#[repr(packed(4))]
247+
struct Goal(i32);
248+
}
249+
250+
check_size_and_align("#[repr(packed(5))] struct Goal(i32);", "", 4, 1);
251+
}
252+
223253
#[test]
224254
fn generic() {
225255
size_and_align! {

0 commit comments

Comments
 (0)