Skip to content

Commit fa62fbe

Browse files
authored
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
2 parents 890c4d2 + 0713bbc commit fa62fbe

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

compiler/rustc_middle/src/mir/visit.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1364,13 +1364,13 @@ impl PlaceContext {
13641364
matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
13651365
}
13661366

1367-
/// Returns `true` if this place context represents a borrow.
1367+
/// Returns `true` if this place context represents a borrow, excluding fake borrows
1368+
/// (which are an artifact of borrowck and not actually borrows in runtime MIR).
13681369
pub fn is_borrow(self) -> bool {
13691370
matches!(
13701371
self,
1371-
PlaceContext::NonMutatingUse(
1372-
NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::FakeBorrow
1373-
) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1372+
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
1373+
| PlaceContext::MutatingUse(MutatingUseContext::Borrow)
13741374
)
13751375
}
13761376

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ check-pass
2+
3+
// Regression test for <https://github.com/rust-lang/rust/issues/137250>.
4+
5+
// Ensure that we don't emit unaligned packed field reference errors for the fake
6+
// borrows that we generate during match lowering. These fake borrows are there to
7+
// ensure in *borrow-checking* that we don't modify the value being matched, but
8+
// they are removed after the MIR is processed by `CleanupPostBorrowck`.
9+
10+
#[repr(packed)]
11+
pub struct Packed(i32);
12+
13+
fn f(x: Packed) {
14+
match &x {
15+
Packed(4) => {},
16+
_ if true => {},
17+
_ => {}
18+
}
19+
20+
match x {
21+
Packed(4) => {},
22+
_ if true => {},
23+
_ => {}
24+
}
25+
}
26+
27+
fn main() {}

0 commit comments

Comments
 (0)