about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-05-11 22:27:50 +0000
committerGitHub <noreply@github.com>2021-05-11 22:27:50 +0000
commitc6e2ba43bbfef80d4ecabbc9edd5be9d058f6db9 (patch)
tree3fba6b5419e7b39dd922387d12c90e3111845b11
parentacde43f7c945e4f6728b1a3614d517894f6c5579 (diff)
parentbda68e23328ca62a71da348a13c4d13cc8f991f3 (diff)
downloadrust-c6e2ba43bbfef80d4ecabbc9edd5be9d058f6db9.tar.gz
rust-c6e2ba43bbfef80d4ecabbc9edd5be9d058f6db9.zip
Merge #8806
8806: fix: Strip delimiter from fn-like macro invocations r=jonas-schievink a=jonas-schievink

This broke in https://github.com/rust-analyzer/rust-analyzer/pull/8796 (again), the fix is easy though

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
-rw-r--r--crates/hir_expand/src/input.rs31
-rw-r--r--crates/hir_expand/src/lib.rs4
2 files changed, 33 insertions, 2 deletions
diff --git a/crates/hir_expand/src/input.rs b/crates/hir_expand/src/input.rs
index 112216859cf..860aa049ba0 100644
--- a/crates/hir_expand/src/input.rs
+++ b/crates/hir_expand/src/input.rs
@@ -1,8 +1,9 @@
 //! Macro input conditioning.
 
+use parser::SyntaxKind;
 use syntax::{
     ast::{self, AttrsOwner},
-    AstNode, SyntaxNode,
+    AstNode, SyntaxElement, SyntaxNode,
 };
 
 use crate::{
@@ -19,7 +20,33 @@ pub(crate) fn process_macro_input(
     let loc: MacroCallLoc = db.lookup_intern_macro(id);
 
     match loc.kind {
-        MacroCallKind::FnLike { .. } => node,
+        MacroCallKind::FnLike { .. } => {
+            if !loc.def.is_proc_macro() {
+                // MBE macros expect the parentheses as part of their input.
+                return node;
+            }
+
+            // The input includes the `(` + `)` delimiter tokens, so remove them before passing this
+            // to the macro.
+            let node = node.clone_for_update();
+            if let Some(SyntaxElement::Token(tkn)) = node.first_child_or_token() {
+                if matches!(
+                    tkn.kind(),
+                    SyntaxKind::L_BRACK | SyntaxKind::L_PAREN | SyntaxKind::L_CURLY
+                ) {
+                    tkn.detach();
+                }
+            }
+            if let Some(SyntaxElement::Token(tkn)) = node.last_child_or_token() {
+                if matches!(
+                    tkn.kind(),
+                    SyntaxKind::R_BRACK | SyntaxKind::R_PAREN | SyntaxKind::R_CURLY
+                ) {
+                    tkn.detach();
+                }
+            }
+            node
+        }
         MacroCallKind::Derive { derive_attr_index, .. } => {
             let item = match ast::Item::cast(node.clone()) {
                 Some(item) => item,
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 5df11856e90..88cb16ca4d1 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -272,6 +272,10 @@ impl MacroDefId {
         };
         Either::Left(*id)
     }
+
+    pub fn is_proc_macro(&self) -> bool {
+        matches!(self.kind, MacroDefKind::ProcMacro(..))
+    }
 }
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]