Closed
Description
Consider (https://godbolt.org/z/x39GzMKz4):
#[repr(packed)]
#[derive(PartialEq)]
pub struct Packed(i32);
enum Y {
Foo { id: Packed, },
Bar(i32)
}
fn pred(_: &i32) -> bool {
return true;
}
fn f(x: Y) {
match &x {
Y::Foo { id: Packed(4), .. } => {},
Y::Bar(s) if pred(s) => {},
_ => {}
}
}
This results in an error:
error[E0793]: reference to packed field is unaligned
--> <source>:14:11
|
14 | match &x {
| ^^
|
I don’t really know what the problem is here, but it might be the match on the id
field in the first match arm that triggers the error. Moving Packed(4)
into a local variable works, but if I declare it as a const
variable instead the error resurfaces. Alternatively, removing the Y::Bar
match arm for some reason also makes the error go away.
This currently errors on nightly and some testing on godbolt shows that this code results in an error since Rust 1.62. Before that, we still get the same diagnostic; it’s just treated as a warning instead.