Skip to content

Commit

Permalink
Merge #8806
Browse files Browse the repository at this point in the history
8806: fix: Strip delimiter from fn-like macro invocations r=jonas-schievink a=jonas-schievink

This broke in #8796 (again), the fix is easy though

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
  • Loading branch information
bors[bot] and jonas-schievink committed May 11, 2021
2 parents acde43f + bda68e2 commit c6e2ba4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 29 additions & 2 deletions crates/hir_expand/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Macro input conditioning.

use parser::SyntaxKind;
use syntax::{
ast::{self, AttrsOwner},
AstNode, SyntaxNode,
AstNode, SyntaxElement, SyntaxNode,
};

use crate::{
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions crates/hir_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit c6e2ba4

Please sign in to comment.