Skip to content

Commit d2a6a5e

Browse files
committed
macros: Allow any delimiters for invocation
It is not necessary for macro invocations to match the delimiters used in the matcher. A matcher using parentheses can be invoked with curlies or brackets, as well as any other combination(curlies matcher can be invoked with parentheses or brackets)
1 parent 865b609 commit d2a6a5e

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

gcc/rust/expand/rust-macro-expand.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,26 +3493,30 @@ MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
34933493
return false;
34943494
}
34953495

3496+
auto delimiter = parser.peek_current_token ();
3497+
34963498
// this is used so we can check that we delimit the stream correctly.
3497-
switch (matcher.get_delim_type ())
3499+
switch (delimiter->get_id ())
34983500
{
3499-
case AST::DelimType::PARENS: {
3501+
case LEFT_PAREN: {
35003502
if (!parser.skip_token (LEFT_PAREN))
35013503
return false;
35023504
}
35033505
break;
35043506

3505-
case AST::DelimType::SQUARE: {
3507+
case LEFT_SQUARE: {
35063508
if (!parser.skip_token (LEFT_SQUARE))
35073509
return false;
35083510
}
35093511
break;
35103512

3511-
case AST::DelimType::CURLY: {
3513+
case LEFT_CURLY: {
35123514
if (!parser.skip_token (LEFT_CURLY))
35133515
return false;
35143516
}
35153517
break;
3518+
default:
3519+
gcc_unreachable ();
35163520
}
35173521

35183522
const MacroInvocLexer &source = parser.get_token_source ();
@@ -3566,25 +3570,27 @@ MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
35663570
}
35673571
}
35683572

3569-
switch (matcher.get_delim_type ())
3573+
switch (delimiter->get_id ())
35703574
{
3571-
case AST::DelimType::PARENS: {
3575+
case LEFT_PAREN: {
35723576
if (!parser.skip_token (RIGHT_PAREN))
35733577
return false;
35743578
}
35753579
break;
35763580

3577-
case AST::DelimType::SQUARE: {
3581+
case LEFT_SQUARE: {
35783582
if (!parser.skip_token (RIGHT_SQUARE))
35793583
return false;
35803584
}
35813585
break;
35823586

3583-
case AST::DelimType::CURLY: {
3587+
case LEFT_CURLY: {
35843588
if (!parser.skip_token (RIGHT_CURLY))
35853589
return false;
35863590
}
35873591
break;
3592+
default:
3593+
gcc_unreachable ();
35883594
}
35893595

35903596
return true;

gcc/testsuite/rust/compile/macro10.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// { dg-additional-options "-w" }
2+
macro_rules! foo {
3+
{} => {
4+
15
5+
};
6+
}
7+
8+
fn main() {
9+
let a = foo!();
10+
let b = foo![];
11+
}

0 commit comments

Comments
 (0)