Closed
Description
I tried this code:
// run-pass
#![allow(dead_code)]
#[repr(packed)]
pub struct Good {
data: &'static u32,
data2: [&'static u32; 2],
aligned: [u8; 32],
}
// kill this test when that turns to a hard error
#[allow(unaligned_references)]
fn main() {
let good = Good { data: &0, data2: [&0, &0], aligned: [0; 32] };
let _ = &good.data; // ok
let _ = &good.data2[0]; // ok
let _ = &good.data;
let _ = &good.data2[0];
let _ = &*good.data; // ok, behind a pointer
let _ = &good.aligned; // ok, has align 1
let _ = &good.aligned[2]; // ok, has align 1
}
error: Undefined Behavior: type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2)
--> src/main.rs:15:13
|
15 | let _ = &good.data; // ok
| ^^^^^^^^^^ type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2)
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
rustc 1.62.0-nightly (ec77f2524 2022-04-17)
binary: rustc
commit-hash: ec77f252434a532fdb5699ae4f21a3072d211edd
commit-date: 2022-04-17
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.0