aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2025-04-08 14:41:16 +0200
committerCohenArthur <arthur.cohen@embecosm.com>2025-04-14 08:03:49 +0000
commit780ebba8a9152598e0fde0528c2fc63d9f474fcd (patch)
treebf59145420917214e7efc7a30ef6bd9e11e56f60 /gcc/rust
parent688b8d8b0e24aa7e2a7f741355d696f60337f956 (diff)
downloadgcc-780ebba8a9152598e0fde0528c2fc63d9f474fcd.zip
gcc-780ebba8a9152598e0fde0528c2fc63d9f474fcd.tar.gz
gcc-780ebba8a9152598e0fde0528c2fc63d9f474fcd.tar.bz2
expansion: Only add fragments if the matcher succeeded
gcc/rust/ChangeLog: * expand/rust-macro-expand.cc (MacroExpander::match_n_matches): Do not insert fragments and substack fragments if the matcher failed. gcc/testsuite/ChangeLog: * rust/compile/macros/mbe/macro-issue3708.rs: New test.
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc15
1 files changed, 8 insertions, 7 deletions
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 7829757..c22db1c 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -621,9 +621,10 @@ MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser,
// matched fragment get the offset in the token stream
size_t offs_end = source.get_offs ();
- sub_stack.insert_metavar (
- MatchedFragment (fragment->get_ident ().as_string (),
- offs_begin, offs_end));
+ if (valid_current_match)
+ sub_stack.insert_metavar (
+ MatchedFragment (fragment->get_ident ().as_string (),
+ offs_begin, offs_end));
}
break;
@@ -650,15 +651,15 @@ MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser,
}
auto old_stack = sub_stack.pop ();
- // nest metavars into repetitions
- for (auto &ent : old_stack)
- sub_stack.append_fragment (ent.first, std::move (ent.second));
-
// If we've encountered an error once, stop trying to match more
// repetitions
if (!valid_current_match)
break;
+ // nest metavars into repetitions
+ for (auto &ent : old_stack)
+ sub_stack.append_fragment (ent.first, std::move (ent.second));
+
match_amount++;
// Break early if we notice there's too many expressions already