about summary refs log tree commit diff
path: root/compiler/rustc_mir_build/src/builder
diff options
context:
space:
mode:
authordianne <diannes.gm@gmail.com>2025-04-16 18:41:52 -0700
committerdianne <diannes.gm@gmail.com>2025-04-24 14:25:27 -0700
commit0eb3b110f0b14b21de2e7c283ea21bc86e0d311d (patch)
tree5b2040e453a20c590afc7998d12aec5feb882139 /compiler/rustc_mir_build/src/builder
parent64c8d5d7f53d7ca053335eeb3ef5756c1bbdb5f2 (diff)
downloadrust-0eb3b110f0b14b21de2e7c283ea21bc86e0d311d.tar.gz
rust-0eb3b110f0b14b21de2e7c283ea21bc86e0d311d.zip
lower deref patterns on boxes using built-in derefs
This allows deref patterns to move out of boxes.

Implementation-wise, I've opted to put the information of whether a
deref pattern uses a built-in deref or a method call in the THIR. It'd
be a bit less code to check `.is_box()` everywhere, but I think this way
feels more robust (and we don't have a `mutability` field in the THIR
that we ignore when the smart pointer's a box). I'm not sure about the
naming (or using `ByRef`), though.
Diffstat (limited to 'compiler/rustc_mir_build/src/builder')
-rw-r--r--compiler/rustc_mir_build/src/builder/matches/match_pair.rs11
1 files changed, 9 insertions, 2 deletions
diff --git a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs
index d66b38c5b00..3a7854a5e11 100644
--- a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs
+++ b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs
@@ -1,5 +1,6 @@
 use std::sync::Arc;
 
+use rustc_hir::ByRef;
 use rustc_middle::mir::*;
 use rustc_middle::thir::*;
 use rustc_middle::ty::{self, Ty, TypeVisitableExt};
@@ -260,7 +261,13 @@ impl<'tcx> MatchPairTree<'tcx> {
                 None
             }
 
-            PatKind::Deref { ref subpattern } => {
+            PatKind::Deref { ref subpattern }
+            | PatKind::DerefPattern { ref subpattern, borrow: ByRef::No } => {
+                if cfg!(debug_assertions) && matches!(pattern.kind, PatKind::DerefPattern { .. }) {
+                    // Only deref patterns on boxes can be lowered using a built-in deref.
+                    debug_assert!(pattern.ty.is_box());
+                }
+
                 MatchPairTree::for_pattern(
                     place_builder.deref(),
                     subpattern,
@@ -271,7 +278,7 @@ impl<'tcx> MatchPairTree<'tcx> {
                 None
             }
 
-            PatKind::DerefPattern { ref subpattern, mutability } => {
+            PatKind::DerefPattern { ref subpattern, borrow: ByRef::Yes(mutability) } => {
                 // Create a new temporary for each deref pattern.
                 // FIXME(deref_patterns): dedup temporaries to avoid multiple `deref()` calls?
                 let temp = cx.temp(