Skip to content

Commit f86a71d

Browse files
committed
[AIX] Ignore linting on repr(C) structs with repr(packed) or repr(align(n))
This PR updates the lint added in 9b40bd7 to ignore repr(C) structs that also have repr(packed) or repr(align(n)). As these representations can be modifiers on repr(C), it is assumed that users that add these should know what they are doing, and thus the the lint should not warn on the respective structs. For example, for the time being, using repr(packed) and manually padding a repr(C) struct can be done to correctly align struct members on AIX.
1 parent f5a1ef7 commit f86a71d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

compiler/rustc_lint/src/types.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,9 @@ impl ImproperCTypesDefinitions {
16381638
return true;
16391639
} else if let Adt(adt_def, _) = ty.kind()
16401640
&& adt_def.is_struct()
1641+
&& adt_def.repr().c()
1642+
&& !adt_def.repr().packed()
1643+
&& adt_def.repr().align.is_none()
16411644
{
16421645
let struct_variant = adt_def.variant(VariantIdx::ZERO);
16431646
// Within a nested struct, all fields are examined to correctly
@@ -1659,8 +1662,11 @@ impl ImproperCTypesDefinitions {
16591662
item: &'tcx hir::Item<'tcx>,
16601663
) {
16611664
let adt_def = cx.tcx.adt_def(item.owner_id.to_def_id());
1665+
// repr(C) structs also with packed or aligned representation
1666+
// should be ignored.
16621667
if adt_def.repr().c()
16631668
&& !adt_def.repr().packed()
1669+
&& adt_def.repr().align.is_none()
16641670
&& cx.tcx.sess.target.os == "aix"
16651671
&& !adt_def.all_fields().next().is_none()
16661672
{

tests/ui/layout/reprc-power-alignment.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,29 @@ pub struct I {
148148
d: f32,
149149
e: f64,
150150
}
151-
151+
#[repr(C)]
152+
pub struct J {
153+
a: u8,
154+
b: I,
155+
}
156+
// The lint also ignores diagnosing #[repr(align(n))].
157+
#[repr(C, align(8))]
158+
pub struct K {
159+
a: u8,
160+
b: u8,
161+
c: f64,
162+
d: f32,
163+
e: f64,
164+
}
165+
#[repr(C)]
166+
pub struct L {
167+
a: u8,
168+
b: K,
169+
}
170+
#[repr(C, align(8))]
171+
pub struct M {
172+
a: u8,
173+
b: K,
174+
c: L,
175+
}
152176
fn main() { }

0 commit comments

Comments
 (0)