Static USES_POWER_ALIGNMENT

Source
static USES_POWER_ALIGNMENT: &Lint
Expand description

The uses_power_alignment lint detects specific repr(C) aggregates on AIX. In its platform C ABI, AIX uses the “power” (as in PowerPC) alignment rule (detailed in https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=data-using-alignment-modes#alignment), which can also be set for XLC by #pragma align(power) or -qalign=power. Aggregates with a floating-point type as the recursively first field (as in “at offset 0”) modify the layout of subsequent fields of the associated structs to use an alignment value where the floating-point type is aligned on a 4-byte boundary.

Effectively, subsequent floating-point fields act as-if they are repr(packed(4)). This would be unsound to do in a repr(C) type without all the restrictions that come with repr(packed). Rust instead chooses a layout that maintains soundness of Rust code, at the expense of incompatibility with C code.

§Example

#[repr(C)]
pub struct Floats {
    a: f64,
    b: u8,
    c: f64,
}

This will produce:

warning: repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
 --> <source>:5:3
  |
5 |   c: f64,
  |   ^^^^^^
  |
  = note: `#[warn(uses_power_alignment)]` on by default

§Explanation

The power alignment rule specifies that the above struct has the following alignment:

  • offset_of!(Floats, a) == 0
  • offset_of!(Floats, b) == 8
  • offset_of!(Floats, c) == 12

However, Rust currently aligns c at offset_of!(Floats, c) == 16. Using offset 12 would be unsound since f64 generally must be 8-aligned on this target. Thus, a warning is produced for the above struct.