about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-10 08:59:51 +0000
committerbors <bors@rust-lang.org>2024-02-10 08:59:51 +0000
commitaa97edb21490ff543740c4e97c7df78642a782fa (patch)
treeeb2d8e9181fcb5ec0e5e55f9f2e77a7072869df8
parent7e9265506df75c4be77cb0624ca9fbc9f69a0901 (diff)
parent18be556b37fa89bea50a40bd980c392346df838a (diff)
downloadrust-aa97edb21490ff543740c4e97c7df78642a782fa.tar.gz
rust-aa97edb21490ff543740c4e97c7df78642a782fa.zip
Auto merge of #16497 - evertedsphere:swann/fix-inline-for-macro-generated-method, r=Veykril
Fix incorrect inlining of functions that come from MBE macros

Partial fix for https://github.com/rust-lang/rust-analyzer/issues/16471.

As a reminder, there are two issues there:
1. missing whitespace in parameter types (the first test)
2. the `self` parameter not being replaced by `this` in the function body (the second test)

The first part is fixed in this PR. See [this comment](https://github.com/rust-lang/rust-analyzer/pull/16497#issuecomment-1934243409) for the second.
-rw-r--r--crates/ide-assists/src/handlers/inline_call.rs62
1 files changed, 61 insertions, 1 deletions
diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs
index 4ba33ada48c..11b22b65205 100644
--- a/crates/ide-assists/src/handlers/inline_call.rs
+++ b/crates/ide-assists/src/handlers/inline_call.rs
@@ -415,7 +415,24 @@ fn inline(
         let expr: &ast::Expr = expr;
 
         let mut insert_let_stmt = || {
-            let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone());
+            let param_ty = match param_ty {
+                None => None,
+                Some(param_ty) => {
+                    if sema.hir_file_for(param_ty.syntax()).is_macro() {
+                        if let Some(param_ty) =
+                            ast::Type::cast(insert_ws_into(param_ty.syntax().clone()))
+                        {
+                            Some(param_ty)
+                        } else {
+                            Some(param_ty.clone_for_update())
+                        }
+                    } else {
+                        Some(param_ty.clone_for_update())
+                    }
+                }
+            };
+            let ty: Option<syntax::ast::Type> =
+                sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty);
 
             let is_self = param
                 .name(sema.db)
@@ -1735,4 +1752,47 @@ pub fn main() {
 "#,
         )
     }
+
+    #[test]
+    fn inline_call_with_reference_in_macros() {
+        check_assist(
+            inline_call,
+            r#"
+fn _write_u64(s: &mut u64, x: u64) {
+    *s += x;
+}
+macro_rules! impl_write {
+    ($(($ty:ident, $meth:ident),)*) => {$(
+        fn _hash(inner_self_: &u64, state: &mut u64) {
+            $meth(state, *inner_self_)
+        }
+    )*}
+}
+impl_write! { (u64, _write_u64), }
+fn _hash2(self_: &u64, state: &mut u64) {
+    $0_hash(&self_, state);
+}
+"#,
+            r#"
+fn _write_u64(s: &mut u64, x: u64) {
+    *s += x;
+}
+macro_rules! impl_write {
+    ($(($ty:ident, $meth:ident),)*) => {$(
+        fn _hash(inner_self_: &u64, state: &mut u64) {
+            $meth(state, *inner_self_)
+        }
+    )*}
+}
+impl_write! { (u64, _write_u64), }
+fn _hash2(self_: &u64, state: &mut u64) {
+    {
+        let inner_self_: &u64 = &self_;
+        let state: &mut u64 = state;
+      _write_u64(state, *inner_self_)
+    };
+}
+"#,
+        )
+    }
 }