Skip to content

Commit 0713bbc

Browse files
Ignore fake borrows for packed field check
1 parent 9f48ded commit 0713bbc

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Diff for: 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

Diff for: tests/ui/lint/unaligned_references_fake_borrow.rs

+27
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)