pub static INVALID_ALIGNMENT: &'static Lint
Expand description

The invalid_alignment lint detects dereferences of misaligned pointers during constant evluation.

Example

#![feature(const_ptr_read)]
const FOO: () = unsafe {
    let x = &[0_u8; 4];
    let y = x.as_ptr().cast::<u32>();
    y.read(); // the address of a `u8` array is unknown and thus we don't know if
    // it is aligned enough for reading a `u32`.
};

{{produces}}

Explanation

The compiler allowed dereferencing raw pointers irrespective of alignment during const eval due to the const evaluator at the time not making it easy or cheap to check. Now that it is both, this is not accepted anymore.

Since it was undefined behaviour to begin with, this breakage does not violate Rust’s stability guarantees. Using undefined behaviour can cause arbitrary behaviour, including failure to build.