about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAmy Kwan <amy.kwan1@ibm.com>2025-03-08 00:23:46 -0500
committerAmy Kwan <amy.kwan1@ibm.com>2025-03-08 00:23:46 -0500
commitf86a71dfbf35b36b1ce439f65dfe51f591688393 (patch)
tree9ea17ebd0b01bad70582d563ba57ef9b37a0e873
parentf5a1ef7121ad661b5a21a1d02941c8064d54ee0b (diff)
downloadrust-f86a71dfbf35b36b1ce439f65dfe51f591688393.tar.gz
rust-f86a71dfbf35b36b1ce439f65dfe51f591688393.zip
[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.
-rw-r--r--compiler/rustc_lint/src/types.rs6
-rw-r--r--tests/ui/layout/reprc-power-alignment.rs26
2 files changed, 31 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs
index fcadbfc3c4a..71116d0bfe4 100644
--- a/compiler/rustc_lint/src/types.rs
+++ b/compiler/rustc_lint/src/types.rs
@@ -1638,6 +1638,9 @@ impl ImproperCTypesDefinitions {
             return true;
         } else if let Adt(adt_def, _) = ty.kind()
             && adt_def.is_struct()
+            && adt_def.repr().c()
+            && !adt_def.repr().packed()
+            && adt_def.repr().align.is_none()
         {
             let struct_variant = adt_def.variant(VariantIdx::ZERO);
             // Within a nested struct, all fields are examined to correctly
@@ -1659,8 +1662,11 @@ impl ImproperCTypesDefinitions {
         item: &'tcx hir::Item<'tcx>,
     ) {
         let adt_def = cx.tcx.adt_def(item.owner_id.to_def_id());
+        // repr(C) structs also with packed or aligned representation
+        // should be ignored.
         if adt_def.repr().c()
             && !adt_def.repr().packed()
+            && adt_def.repr().align.is_none()
             && cx.tcx.sess.target.os == "aix"
             && !adt_def.all_fields().next().is_none()
         {
diff --git a/tests/ui/layout/reprc-power-alignment.rs b/tests/ui/layout/reprc-power-alignment.rs
index f6c1df55988..f144094d43f 100644
--- a/tests/ui/layout/reprc-power-alignment.rs
+++ b/tests/ui/layout/reprc-power-alignment.rs
@@ -148,5 +148,29 @@ pub struct I {
     d: f32,
     e: f64,
 }
-
+#[repr(C)]
+pub struct J {
+    a: u8,
+    b: I,
+}
+// The lint also ignores diagnosing #[repr(align(n))].
+#[repr(C, align(8))]
+pub struct K {
+    a: u8,
+    b: u8,
+    c: f64,
+    d: f32,
+    e: f64,
+}
+#[repr(C)]
+pub struct L {
+    a: u8,
+    b: K,
+}
+#[repr(C, align(8))]
+pub struct M {
+    a: u8,
+    b: K,
+    c: L,
+}
 fn main() { }