Skip to content

Commit 912b042

Browse files
committed
macros: Check follow-set restrictions on matcher's first delimiter
1 parent 8283724 commit 912b042

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

gcc/rust/parse/rust-parse.cc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,25 @@ peculiar_fragment_match_compatible (AST::MacroMatchFragment &last_match,
202202
}
203203
case AST::MacroMatch::Matcher: {
204204
auto matcher = static_cast<AST::MacroMatcher *> (&match);
205-
auto &matches = matcher->get_matches ();
206-
if (!matches.empty ())
207-
error_locus = matches.front ()->get_match_locus ();
205+
auto first_token = matcher->get_delim_type ();
206+
TokenId delim_id;
207+
switch (first_token)
208+
{
209+
case AST::PARENS:
210+
delim_id = LEFT_PAREN;
211+
break;
212+
case AST::SQUARE:
213+
delim_id = LEFT_SQUARE;
214+
break;
215+
case AST::CURLY:
216+
delim_id = LEFT_CURLY;
217+
break;
218+
}
219+
if (contains (allowed_toks, delim_id))
220+
return true;
221+
kind_str = "token `" + std::string (get_token_description (delim_id))
222+
+ "` at start of matcher";
223+
error_locus = matcher->get_match_locus ();
208224
break;
209225
}
210226
case AST::MacroMatch::Fragment: {

gcc/testsuite/rust/compile/macro39.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
macro_rules! m {
2+
($e:expr (, parenthesis_forbidden)) => {{}}; // { dg-error "token .\\(. at start of matcher is not allowed after .expr. fragment" }
3+
// { dg-error "required first macro rule" "" { target *-*-* } .-1 }
4+
// { dg-error "failed to parse item in crate" "" { target *-*-* } .-2 }
5+
}

0 commit comments

Comments
 (0)