Skip to content

Commit 5a0ea3b

Browse files
committed
power align: ignore repr(C) unions and enums
1 parent 87e8ec8 commit 5a0ea3b

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

compiler/rustc_lint/src/types/improper_ctypes.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,21 @@ fn check_struct_for_power_alignment<'tcx>(
220220
adt_def: AdtDef<'tcx>,
221221
) {
222222
let tcx = cx.tcx;
223-
// repr(C) structs also with packed or aligned representation
224-
// should be ignored.
225-
if adt_def.repr().c()
226-
&& !adt_def.repr().packed()
227-
&& adt_def.repr().align.is_none()
228-
&& tcx.sess.target.os == "aix"
229-
&& !adt_def.all_fields().next().is_none()
230-
{
223+
224+
// Only consider structs (not enums or unions) on AIX.
225+
if tcx.sess.target.os != "aix" || !adt_def.is_struct() {
226+
return;
227+
}
228+
229+
// The struct must be repr(C), but ignore it if it explicitly specifies its alignment with
230+
// either `align(N)` or `packed(N)`.
231+
if adt_def.repr().c() && !adt_def.repr().packed() && adt_def.repr().align.is_none() {
231232
let struct_variant_data = item.expect_struct().2;
232233
for field_def in struct_variant_data.fields().iter().skip(1) {
233234
// Struct fields (after the first field) are checked for the
234235
// power alignment rule, as fields after the first are likely
235236
// to be the fields that are misaligned.
236-
let def_id = field_def.def_id;
237-
let ty = tcx.type_of(def_id).instantiate_identity();
237+
let ty = tcx.type_of(field_def.def_id).instantiate_identity();
238238
if check_arg_for_power_alignment(cx, ty) {
239239
cx.emit_span_lint(USES_POWER_ALIGNMENT, field_def.span, UsesPowerAlignment);
240240
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,19 @@ pub struct M {
173173
b: K,
174174
c: L,
175175
}
176+
177+
// The lint ignores unions
178+
#[repr(C)]
179+
pub union Union {
180+
a: f64,
181+
b: u8,
182+
c: f64,
183+
d: f32,
184+
}
185+
186+
// The lint ignores enums
187+
#[repr(C)]
188+
pub enum Enum {
189+
A { a: f64, b: u8, c: f64, d: f32 },
190+
B,
191+
}

0 commit comments

Comments
 (0)