about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/util/find_self_call.rs
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-01-06 03:17:04 +0000
committerMichael Goulet <michael@errs.io>2025-01-06 03:17:04 +0000
commitcd65cd27db8a8fc11f3191d3e461d331ed0db968 (patch)
treebe633afbccffdd6805cfb66f81adb3e89862bfdc /compiler/rustc_middle/src/util/find_self_call.rs
parent3560a2b399ea30b8cd62d9c91a326f03a728e92a (diff)
downloadrust-cd65cd27db8a8fc11f3191d3e461d331ed0db968.tar.gz
rust-cd65cd27db8a8fc11f3191d3e461d331ed0db968.zip
Improve find_self_call with reborrowed receiver
Diffstat (limited to 'compiler/rustc_middle/src/util/find_self_call.rs')
-rw-r--r--compiler/rustc_middle/src/util/find_self_call.rs41
1 files changed, 22 insertions, 19 deletions
diff --git a/compiler/rustc_middle/src/util/find_self_call.rs b/compiler/rustc_middle/src/util/find_self_call.rs
index ec6051d0a77..0fdd3520738 100644
--- a/compiler/rustc_middle/src/util/find_self_call.rs
+++ b/compiler/rustc_middle/src/util/find_self_call.rs
@@ -17,26 +17,29 @@ pub fn find_self_call<'tcx>(
     debug!("find_self_call(local={:?}): terminator={:?}", local, body[block].terminator);
     if let Some(Terminator { kind: TerminatorKind::Call { func, args, .. }, .. }) =
         &body[block].terminator
+        && let Operand::Constant(box ConstOperand { const_, .. }) = func
+        && let ty::FnDef(def_id, fn_args) = *const_.ty().kind()
+        && let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) =
+            tcx.opt_associated_item(def_id)
+        && let [Spanned { node: Operand::Move(self_place) | Operand::Copy(self_place), .. }, ..] =
+            **args
     {
-        debug!("find_self_call: func={:?}", func);
-        if let Operand::Constant(box ConstOperand { const_, .. }) = func {
-            if let ty::FnDef(def_id, fn_args) = *const_.ty().kind() {
-                if let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) =
-                    tcx.opt_associated_item(def_id)
-                {
-                    debug!("find_self_call: args={:?}", fn_args);
-                    if let [
-                        Spanned {
-                            node: Operand::Move(self_place) | Operand::Copy(self_place), ..
-                        },
-                        ..,
-                    ] = **args
-                    {
-                        if self_place.as_local() == Some(local) {
-                            return Some((def_id, fn_args));
-                        }
-                    }
-                }
+        if self_place.as_local() == Some(local) {
+            return Some((def_id, fn_args));
+        }
+
+        // Handle the case where `self_place` gets reborrowed.
+        // This happens when the receiver is `&T`.
+        for stmt in &body[block].statements {
+            if let StatementKind::Assign(box (place, rvalue)) = &stmt.kind
+                && let Some(reborrow_local) = place.as_local()
+                && self_place.as_local() == Some(reborrow_local)
+                && let Rvalue::Ref(_, _, deref_place) = rvalue
+                && let PlaceRef { local: deref_local, projection: [ProjectionElem::Deref] } =
+                    deref_place.as_ref()
+                && deref_local == local
+            {
+                return Some((def_id, fn_args));
             }
         }
     }