Closed
Description
Code
#[repr(packed)]
pub struct Foo {
a: usize,
b: [u16; 1],
}
pub fn foo(f: &Foo) {
bar(f.b.as_ptr());
}
pub fn bar(_ptr: *const u16) {}
#[repr(C, align(8))]
pub struct ByteThenFoo { a: u8, b: Foo }
fn main() {
let mut v = vec![];
v.push(ByteThenFoo {
a: 1,
b: Foo { a: 123, b: [2] },
});
let x = v.last().unwrap();
foo(&x.b);
}
Current output
error[E0793]: reference to packed field is unaligned
--> src/main.rs:8:9
|
8 | bar(f.b.as_ptr());
| ^^^^^^^^^^^^
|
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
For more information about this error, try `rustc --explain E0793`.
error: could not compile `playground` (bin "playground") due to previous error
Desired output
much like the above, but point out that #[packed]
makes the struct itself have lower alignment
Rationale and extra context
Spawned off of #109745 (comment)
Its not intuitive to many that #[packed]
will imply a lower #[align]
ment for the whole struct.
Other cases
No response
Anything else?
No response
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Relevant to the compiler team, which will review and decide on the PR/issue.