Skip to content

Commit 065d903

Browse files
committed
Generate only align(N) when packed aligned
1 parent e8168ce commit 065d903

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/codegen/mod.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,16 @@ impl CodeGenerator for CompInfo {
20122012
if let Some(comment) = item.comment(ctx) {
20132013
attributes.push(attributes::doc(comment));
20142014
}
2015-
if packed && !is_opaque {
2015+
2016+
// We can't specify both packed(N) and align(N), but the align()
2017+
// should be redundant in this case.
2018+
let explicit_align = if ctx.options().rust_features().repr_align {
2019+
explicit_align
2020+
} else {
2021+
None
2022+
};
2023+
2024+
if packed && !is_opaque && explicit_align.is_none() {
20162025
let n = layout.map_or(1, |l| l.align);
20172026
assert!(ctx.options().rust_features().repr_packed_n || n == 1);
20182027
let packed_repr = if n == 1 {
@@ -2025,17 +2034,13 @@ impl CodeGenerator for CompInfo {
20252034
attributes.push(attributes::repr("C"));
20262035
}
20272036

2028-
if ctx.options().rust_features().repr_align && !packed {
2029-
// We can't specify both packed(N) and align(N), but the align()
2030-
// should be redundant in this case.
2031-
if let Some(explicit) = explicit_align {
2032-
// Ensure that the struct has the correct alignment even in
2033-
// presence of alignas.
2034-
let explicit = helpers::ast_ty::int_expr(explicit as i64);
2035-
attributes.push(quote! {
2036-
#[repr(align(#explicit))]
2037-
});
2038-
}
2037+
if let Some(explicit) = explicit_align {
2038+
// Ensure that the struct has the correct alignment even in
2039+
// presence of alignas.
2040+
let explicit = helpers::ast_ty::int_expr(explicit as i64);
2041+
attributes.push(quote! {
2042+
#[repr(align(#explicit))]
2043+
});
20392044
}
20402045

20412046
let derivable_traits = derives_of_item(item, ctx, packed);

tests/expectations/tests/packed-align-conflict.rs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)