Skip to content

Commit

Permalink
Auto merge of #16314 - Urhengulas:macro_rule-as-macro-name, r=Veykril
Browse files Browse the repository at this point in the history
`macro_rules` as macro name

This PR makes RA parse `macro_rules! {}` (note the missing identifier) as a `MACRO_CALL` instead of `MACRO_RULES`.

Fixes #15969.
  • Loading branch information
bors committed Jan 17, 2024
2 parents 2b02df2 + 76c67dd commit c820980
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
26 changes: 24 additions & 2 deletions crates/parser/src/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@ pub(super) fn item_or_macro(p: &mut Parser<'_>, stop_on_r_curly: bool) {
Err(m) => m,
};

if paths::is_use_path_start(p) {
// test macro_rules_as_macro_name
// macro_rules! {}
// macro_rules! ();
// macro_rules! [];
// fn main() {
// let foo = macro_rules!();
// }

// test_err macro_rules_as_macro_name
// macro_rules! {};
// macro_rules! ()
// macro_rules! []
if paths::is_use_path_start(p)
|| (p.at_contextual_kw(T![macro_rules]) && p.nth_at(1, BANG) && !p.nth_at(2, IDENT))
{
match macro_call(p) {
BlockLike::Block => (),
BlockLike::NotBlock => {
Expand Down Expand Up @@ -228,7 +242,15 @@ fn opt_item_without_modifiers(p: &mut Parser<'_>, m: Marker) -> Result<(), Marke
IDENT if p.at_contextual_kw(T![union]) && p.nth(1) == IDENT => adt::union(p, m),

T![macro] => macro_def(p, m),
IDENT if p.at_contextual_kw(T![macro_rules]) && p.nth(1) == BANG => macro_rules(p, m),
// check if current token is "macro_rules" followed by "!" followed by an identifier or "try"
// try is keyword since the 2018 edition and the parser is not edition aware (yet!)
IDENT
if p.at_contextual_kw(T![macro_rules])
&& p.nth_at(1, BANG)
&& (p.nth_at(2, IDENT) || p.nth_at(2, T![try])) =>
{
macro_rules(p, m)
}

T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::konst(p, m),
T![static] if (la == IDENT || la == T![_] || la == T![mut]) => consts::static_(p, m),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
SOURCE_FILE
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "macro_rules"
BANG "!"
WHITESPACE " "
TOKEN_TREE
L_CURLY "{"
R_CURLY "}"
ERROR
SEMICOLON ";"
WHITESPACE "\n"
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "macro_rules"
BANG "!"
WHITESPACE " "
TOKEN_TREE
L_PAREN "("
R_PAREN ")"
WHITESPACE "\n"
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "macro_rules"
BANG "!"
WHITESPACE " "
TOKEN_TREE
L_BRACK "["
R_BRACK "]"
WHITESPACE "\n"
error 15: expected an item
error 32: expected SEMICOLON
error 48: expected SEMICOLON
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
macro_rules! {};
macro_rules! ()
macro_rules! []
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
SOURCE_FILE
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "macro_rules"
BANG "!"
WHITESPACE " "
TOKEN_TREE
L_CURLY "{"
R_CURLY "}"
WHITESPACE "\n"
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "macro_rules"
BANG "!"
WHITESPACE " "
TOKEN_TREE
L_PAREN "("
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "macro_rules"
BANG "!"
WHITESPACE " "
TOKEN_TREE
L_BRACK "["
R_BRACK "]"
SEMICOLON ";"
WHITESPACE "\n"
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "main"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
IDENT_PAT
NAME
IDENT "foo"
WHITESPACE " "
EQ "="
WHITESPACE " "
MACRO_EXPR
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF
IDENT "macro_rules"
BANG "!"
TOKEN_TREE
L_PAREN "("
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
macro_rules! {}
macro_rules! ();
macro_rules! [];
fn main() {
let foo = macro_rules!();
}

0 comments on commit c820980

Please sign in to comment.