about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_middle/src/mir/visit.rs8
-rw-r--r--tests/ui/lint/unaligned_references_fake_borrow.rs27
2 files changed, 31 insertions, 4 deletions
diff --git a/compiler/rustc_middle/src/mir/visit.rs b/compiler/rustc_middle/src/mir/visit.rs
index 98e8f269c57..af09a6d570b 100644
--- a/compiler/rustc_middle/src/mir/visit.rs
+++ b/compiler/rustc_middle/src/mir/visit.rs
@@ -1364,13 +1364,13 @@ impl PlaceContext {
         matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
     }
 
-    /// Returns `true` if this place context represents a borrow.
+    /// Returns `true` if this place context represents a borrow, excluding fake borrows
+    /// (which are an artifact of borrowck and not actually borrows in runtime MIR).
     pub fn is_borrow(self) -> bool {
         matches!(
             self,
-            PlaceContext::NonMutatingUse(
-                NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::FakeBorrow
-            ) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
+            PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
+                | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
         )
     }
 
diff --git a/tests/ui/lint/unaligned_references_fake_borrow.rs b/tests/ui/lint/unaligned_references_fake_borrow.rs
new file mode 100644
index 00000000000..b0ef8b471ca
--- /dev/null
+++ b/tests/ui/lint/unaligned_references_fake_borrow.rs
@@ -0,0 +1,27 @@
+//@ check-pass
+
+// Regression test for <https://github.com/rust-lang/rust/issues/137250>.
+
+// Ensure that we don't 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, but
+// they are removed after the MIR is processed by `CleanupPostBorrowck`.
+
+#[repr(packed)]
+pub struct Packed(i32);
+
+fn f(x: Packed) {
+    match &x {
+        Packed(4) => {},
+        _ if true => {},
+        _ => {}
+    }
+
+    match x {
+        Packed(4) => {},
+        _ if true => {},
+        _ => {}
+    }
+}
+
+fn main() {}