Skip to content

Commit 4fa824b

Browse files
committed
Auto merge of #147375 - folkertdev:power-align-union, r=RalfJung
ignore power alignment rule on unions fixes #147348 The power alignment rule only applies to the non-first field of a struct, and so should not apply to unions at all. The current code also does not consider enums (whose fields might be, morally, structs). Given that C does not actually have ADTs like this it's probably fine from a compatibility perspective, but I'll leave that to the powerpc folks. cc `@daltenty` `@gilamn5tr` r? compiler
2 parents 8392220 + 5a0ea3b commit 4fa824b

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
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: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#![feature(no_core)]
66
#![no_core]
77
#![no_std]
8+
#![crate_type = "lib"]
89

910
extern crate minicore;
1011
use minicore::*;
1112

1213
#[warn(uses_power_alignment)]
13-
1414
#[repr(C)]
1515
pub struct Floats {
1616
a: f64,
@@ -96,32 +96,32 @@ pub struct FloatAgg7 {
9696

9797
#[repr(C)]
9898
pub struct A {
99-
d: f64,
99+
d: f64,
100100
}
101101
#[repr(C)]
102102
pub struct B {
103-
a: A,
104-
f: f32,
105-
d: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
103+
a: A,
104+
f: f32,
105+
d: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
106106
}
107107
#[repr(C)]
108108
pub struct C {
109-
c: u8,
110-
b: B, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
109+
c: u8,
110+
b: B, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
111111
}
112112
#[repr(C)]
113113
pub struct D {
114-
x: f64,
114+
x: f64,
115115
}
116116
#[repr(C)]
117117
pub struct E {
118-
x: i32,
119-
d: D, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
118+
x: i32,
119+
d: D, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
120120
}
121121
#[repr(C)]
122122
pub struct F {
123-
a: u8,
124-
b: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
123+
a: u8,
124+
b: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
125125
}
126126
#[repr(C)]
127127
pub struct G {
@@ -173,4 +173,19 @@ pub struct M {
173173
b: K,
174174
c: L,
175175
}
176-
fn main() { }
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+
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | c: f64,
55
| ^^^^^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/reprc-power-alignment.rs:12:8
8+
--> $DIR/reprc-power-alignment.rs:13:8
99
|
1010
LL | #[warn(uses_power_alignment)]
1111
| ^^^^^^^^^^^^^^^^^^^^
@@ -73,28 +73,28 @@ LL | y: Floats,
7373
| ^^^^^^^^^
7474

7575
warning: repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
76-
--> $DIR/reprc-power-alignment.rs:105:3
76+
--> $DIR/reprc-power-alignment.rs:105:5
7777
|
78-
LL | d: f64,
79-
| ^^^^^^
78+
LL | d: f64,
79+
| ^^^^^^
8080

8181
warning: repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
82-
--> $DIR/reprc-power-alignment.rs:110:3
82+
--> $DIR/reprc-power-alignment.rs:110:5
8383
|
84-
LL | b: B,
85-
| ^^^^
84+
LL | b: B,
85+
| ^^^^
8686

8787
warning: repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
88-
--> $DIR/reprc-power-alignment.rs:119:3
88+
--> $DIR/reprc-power-alignment.rs:119:5
8989
|
90-
LL | d: D,
91-
| ^^^^
90+
LL | d: D,
91+
| ^^^^
9292

9393
warning: repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
94-
--> $DIR/reprc-power-alignment.rs:124:3
94+
--> $DIR/reprc-power-alignment.rs:124:5
9595
|
96-
LL | b: f64,
97-
| ^^^^^^
96+
LL | b: f64,
97+
| ^^^^^^
9898

9999
warning: repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
100100
--> $DIR/reprc-power-alignment.rs:130:5

0 commit comments

Comments
 (0)