-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
‘reference to packed field is unaligned’ in nested field of match
pattern
#137250
Comments
What do you mean by "Moving Packed(4) into a local variable works"? Could you provide example code? |
Sure, what I mean is that if I modify fn f(x: Y) {
let foo = Packed(4);
match &x {
Y::Foo { id: foo, .. } => {},
Y::Bar(s) if pred(s) => {},
_ => {}
}
} But if I then replace the |
This modified code you provided does not do what you want. It declares a second variable also named |
Right, because the behaviour of that syntax is different depending on whether the identifier to the right of the colon names a constant or not; not the first time this has confused me. Well, I suppose that explains why that ‘fixes’ it, but it’s still weird that it errors when I try to match against a constant. |
Smaller reproduction of this issue: #[repr(packed)]
pub struct Packed(i32);
fn f(x: Packed) {
match &x {
Packed(4) => {},
_ if true => {},
_ => {}
}
} Yes, the Even changing the code to #[repr(packed)]
pub struct Packed(i32);
fn f(x: Packed) {
match x {
Packed(4) => {},
_ if true => {},
_ => {}
}
} |
…ked-field, r=oli-obk Ignore fake borrows for packed field check We should not emit unaligned packed field reference errors for the fake borrows that we generate during match lowering. These fake borrows are there to ensure in *borrow-checking* that we don't modify the value being matched (which is why this only occurs when there's a match guard, in this case `if true`), but they are removed after the MIR is processed by `CleanupPostBorrowck`, since they're really just there to cause borrowck errors if necessary. I modified `PlaceContext::is_borrow` since that's used by the packed field check: https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_transform/src/check_packed_ref.rs#L40 It's only used in one other place, in the SROA optimization (by which fake borrows are removed, so it doesn't matter): https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_dataflow/src/value_analysis.rs#L922 Fixes rust-lang#137250
…ked-field, r=oli-obk Ignore fake borrows for packed field check We should not emit unaligned packed field reference errors for the fake borrows that we generate during match lowering. These fake borrows are there to ensure in *borrow-checking* that we don't modify the value being matched (which is why this only occurs when there's a match guard, in this case `if true`), but they are removed after the MIR is processed by `CleanupPostBorrowck`, since they're really just there to cause borrowck errors if necessary. I modified `PlaceContext::is_borrow` since that's used by the packed field check: https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_transform/src/check_packed_ref.rs#L40 It's only used in one other place, in the SROA optimization (by which fake borrows are removed, so it doesn't matter): https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_dataflow/src/value_analysis.rs#L922 Fixes rust-lang#137250
Rollup merge of rust-lang#137257 - compiler-errors:fake-borrow-of-packed-field, r=oli-obk Ignore fake borrows for packed field check We should not emit unaligned packed field reference errors for the fake borrows that we generate during match lowering. These fake borrows are there to ensure in *borrow-checking* that we don't modify the value being matched (which is why this only occurs when there's a match guard, in this case `if true`), but they are removed after the MIR is processed by `CleanupPostBorrowck`, since they're really just there to cause borrowck errors if necessary. I modified `PlaceContext::is_borrow` since that's used by the packed field check: https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_transform/src/check_packed_ref.rs#L40 It's only used in one other place, in the SROA optimization (by which fake borrows are removed, so it doesn't matter): https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_dataflow/src/value_analysis.rs#L922 Fixes rust-lang#137250
Consider (https://godbolt.org/z/x39GzMKz4):
This results in an error:
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. MovingPacked(4)
into a local variable works, but if I declare it as aconst
variable instead the error resurfaces. Alternatively, removing theY::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.
The text was updated successfully, but these errors were encountered: