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.

The power alignment rule for structs needed for C compatibility is unimplementable within repr(C) in the compiler without building in handling of references to packed fields and infectious nested layouts, so a warning is produced in these situations.

§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. Thus, a warning should be produced for the above struct in this case.