pub static INVALID_ALIGNMENT: &Lint
Expand description

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

Example

#![feature(const_mut_refs)]
const FOO: () = unsafe {
    let x = &[0_u8; 4];
    let y = x.as_ptr().cast::<u32>();
    let mut z = 123;
    y.copy_to_nonoverlapping(&mut z, 1); // the address of a `u8` array is unknown
    // and thus we don't know if it is aligned enough for copying 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.