Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions compiler/rustc_lint/src/types/improper_ctypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,21 @@ fn check_struct_for_power_alignment<'tcx>(
adt_def: AdtDef<'tcx>,
) {
let tcx = cx.tcx;
// 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()
&& tcx.sess.target.os == "aix"
&& !adt_def.all_fields().next().is_none()
{

// Only consider structs (not enums or unions) on AIX.
if tcx.sess.target.os != "aix" || !adt_def.is_struct() {
return;
}

// The struct must be repr(C), but ignore it if it explicitly specifies its alignment with
// either `align(N)` or `packed(N)`.
if adt_def.repr().c() && !adt_def.repr().packed() && adt_def.repr().align.is_none() {
let struct_variant_data = item.expect_struct().2;
for field_def in struct_variant_data.fields().iter().skip(1) {
// Struct fields (after the first field) are checked for the
// power alignment rule, as fields after the first are likely
// to be the fields that are misaligned.
let def_id = field_def.def_id;
let ty = tcx.type_of(def_id).instantiate_identity();
let ty = tcx.type_of(field_def.def_id).instantiate_identity();
if check_arg_for_power_alignment(cx, ty) {
cx.emit_span_lint(USES_POWER_ALIGNMENT, field_def.span, UsesPowerAlignment);
}
Expand Down
41 changes: 28 additions & 13 deletions tests/ui/layout/reprc-power-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#![feature(no_core)]
#![no_core]
#![no_std]
#![crate_type = "lib"]

extern crate minicore;
use minicore::*;

#[warn(uses_power_alignment)]

#[repr(C)]
pub struct Floats {
a: f64,
Expand Down Expand Up @@ -96,32 +96,32 @@ pub struct FloatAgg7 {

#[repr(C)]
pub struct A {
d: f64,
d: f64,
}
#[repr(C)]
pub struct B {
a: A,
f: f32,
d: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
a: A,
f: f32,
d: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
}
#[repr(C)]
pub struct C {
c: u8,
b: B, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
c: u8,
b: B, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
}
#[repr(C)]
pub struct D {
x: f64,
x: f64,
}
#[repr(C)]
pub struct E {
x: i32,
d: D, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
x: i32,
d: D, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
}
#[repr(C)]
pub struct F {
a: u8,
b: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
a: u8,
b: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
}
#[repr(C)]
pub struct G {
Expand Down Expand Up @@ -173,4 +173,19 @@ pub struct M {
b: K,
c: L,
}
fn main() { }

// The lint ignores unions
#[repr(C)]
pub union Union {
a: f64,
b: u8,
c: f64,
d: f32,
}

// The lint ignores enums
#[repr(C)]
pub enum Enum {
A { a: f64, b: u8, c: f64, d: f32 },
B,
}
26 changes: 13 additions & 13 deletions tests/ui/layout/reprc-power-alignment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | c: f64,
| ^^^^^^
|
note: the lint level is defined here
--> $DIR/reprc-power-alignment.rs:12:8
--> $DIR/reprc-power-alignment.rs:13:8
|
LL | #[warn(uses_power_alignment)]
| ^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -73,28 +73,28 @@ LL | y: Floats,
| ^^^^^^^^^

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

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

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

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

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