rust/tests/ui/mir/alignment/borrow_aligned_field_projection.rs
Bastian Kersting a3e7c699bc Extend the alignment check to borrows
The current alignment check does not include checks for creating
misaligned references from raw pointers, which is now added in this
patch.

When inserting the check we need to be careful with references to
field projections (e.g. `&(*ptr).a`), in which case the resulting
reference must be aligned according to the field type and not the
type of the pointer.
2025-04-25 12:16:40 +00:00

16 lines
287 B
Rust

//@ run-pass
//@ compile-flags: -C debug-assertions
#[repr(align(8))]
struct Misalignment {
a: u8,
}
fn main() {
let mem = 0u64;
let ptr = &mem as *const u64 as *const Misalignment;
unsafe {
let ptr = ptr.byte_add(1);
let _ref: &u8 = &(*ptr).a;
}
}