Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept macro_rules name for a macro rule. #2675

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,18 @@ Parser<ManagedTokenSource>::parse_token_tree ()
}
}

template <typename ManagedTokenSource>
bool
Parser<ManagedTokenSource>::is_macro_rules_def (const_TokenPtr t)
{
auto macro_name = lexer.peek_token (2)->get_id ();

bool allowed_macro_name = (macro_name == IDENTIFIER || macro_name == TRY);

return t->get_str () == "macro_rules"
&& lexer.peek_token (1)->get_id () == EXCLAM && allowed_macro_name;
}

// Parses a single item
template <typename ManagedTokenSource>
std::unique_ptr<AST::Item>
Expand Down Expand Up @@ -1140,7 +1152,7 @@ Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
"default", "impl"));
return nullptr;
}
else if (t->get_str () == "macro_rules")
else if (is_macro_rules_def (t))
{
// macro_rules! macro item
return parse_macro_rules_def (std::move (outer_attrs));
Expand Down Expand Up @@ -6227,8 +6239,7 @@ Parser<ManagedTokenSource>::parse_stmt (ParseRestrictions restrictions)
return parse_vis_item (std::move (outer_attrs));
// or should this go straight to parsing union?
}
else if (t->get_str () == "macro_rules"
&& lexer.peek_token (1)->get_id () == EXCLAM)
else if (is_macro_rules_def (t))
{
// macro_rules! macro item
return parse_macro_rules_def (std::move (outer_attrs));
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/parse/rust-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ template <typename ManagedTokenSource> class Parser
parse_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
location_t pratt_parsed_loc = UNKNOWN_LOCATION);

bool is_macro_rules_def (const_TokenPtr t);
std::unique_ptr<AST::Item> parse_item (bool called_from_statement);
std::unique_ptr<AST::Pattern> parse_pattern ();
std::unique_ptr<AST::Pattern> parse_pattern_no_alt ();
Expand Down
13 changes: 13 additions & 0 deletions gcc/testsuite/rust/compile/macro57.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
macro_rules! macro_rules {
() => {};
}

macro_rules! foo {
() => {};
}

foo!();

fn main() {}

macro_rules!();
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/macro_rules_macro_rules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
macro_rules! macro_rules {
() => {
struct S;
};
}
macro_rules! {} // calls the macro defined above

fn main() {
let _s = S;
}