public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macros: Allow repetitions of tokens in follow-set in follow-set
@ 2022-06-08 12:23 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:23 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:d859ab0146cd0002afc640d406997efb12a50d98

commit d859ab0146cd0002afc640d406997efb12a50d98
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Mar 23 16:50:47 2022 +0100

    macros: Allow repetitions of tokens in follow-set in follow-set
    
    When checking if a follow-up is valid, we previously always returned
    false when comparing with MacroMatchRepetitions. This is however
    invalid, as we should be comparing with the first match of the
    repetition to be sure.

Diff:
---
 gcc/rust/parse/rust-parse.cc                    | 53 +++++++++++++------------
 gcc/testsuite/rust/compile/macro-issue1053-2.rs |  5 +++
 gcc/testsuite/rust/compile/macro-issue1053.rs   |  3 ++
 gcc/testsuite/rust/compile/macro28.rs           |  2 +-
 4 files changed, 36 insertions(+), 27 deletions(-)

diff --git a/gcc/rust/parse/rust-parse.cc b/gcc/rust/parse/rust-parse.cc
index 0153b37ddf6..50d2efe4fc6 100644
--- a/gcc/rust/parse/rust-parse.cc
+++ b/gcc/rust/parse/rust-parse.cc
@@ -100,6 +100,30 @@ contains (std::vector<T> &vec, T elm)
   return std::find (vec.begin (), vec.end (), elm) != vec.end ();
 }
 
+/**
+ * Avoid UB by calling .front() and .back() on empty containers...
+ */
+
+template <typename T>
+static T *
+get_back_ptr (std::vector<std::unique_ptr<T>> &values)
+{
+  if (values.empty ())
+    return nullptr;
+
+  return values.back ().get ();
+}
+
+template <typename T>
+static T *
+get_front_ptr (std::vector<std::unique_ptr<T>> &values)
+{
+  if (values.empty ())
+    return nullptr;
+
+  return values.front ().get ();
+}
+
 static bool
 peculiar_fragment_match_compatible_fragment (
   const AST::MacroFragSpec &last_spec, const AST::MacroFragSpec &spec,
@@ -196,8 +220,9 @@ peculiar_fragment_match_compatible (AST::MacroMatchFragment &last_match,
       case AST::MacroMatch::Repetition: {
 	auto repetition = static_cast<AST::MacroMatchRepetition *> (&match);
 	auto &matches = repetition->get_matches ();
-	if (!matches.empty ())
-	  error_locus = matches.front ()->get_match_locus ();
+	auto first_frag = get_front_ptr (matches);
+	if (first_frag)
+	  return peculiar_fragment_match_compatible (last_match, *first_frag);
 	break;
       }
       case AST::MacroMatch::Matcher: {
@@ -231,30 +256,6 @@ peculiar_fragment_match_compatible (AST::MacroMatchFragment &last_match,
   return false;
 }
 
-/**
- * Avoid UB by calling .front() and .back() on empty containers...
- */
-
-template <typename T>
-static T *
-get_back_ptr (std::vector<std::unique_ptr<T>> &values)
-{
-  if (values.empty ())
-    return nullptr;
-
-  return values.back ().get ();
-}
-
-template <typename T>
-static T *
-get_front_ptr (std::vector<std::unique_ptr<T>> &values)
-{
-  if (values.empty ())
-    return nullptr;
-
-  return values.front ().get ();
-}
-
 bool
 is_match_compatible (AST::MacroMatch &last_match, AST::MacroMatch &match)
 {
diff --git a/gcc/testsuite/rust/compile/macro-issue1053-2.rs b/gcc/testsuite/rust/compile/macro-issue1053-2.rs
new file mode 100644
index 00000000000..31459907c08
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro-issue1053-2.rs
@@ -0,0 +1,5 @@
+macro_rules! m {
+    ($e:expr $(forbidden)*) => {{}}; // { dg-error "token .identifier. is not allowed after .expr. fragment" }
+                                     // { dg-error "required first macro rule in macro rules definition could not be parsed" "" { target *-*-* } .-1 }
+                                     // { dg-error "failed to parse item in crate" "" { target *-*-* } .-2 }
+}
diff --git a/gcc/testsuite/rust/compile/macro-issue1053.rs b/gcc/testsuite/rust/compile/macro-issue1053.rs
new file mode 100644
index 00000000000..1e968496e0c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro-issue1053.rs
@@ -0,0 +1,3 @@
+macro_rules! m {
+    ($e:expr $(,)*) => {{}};
+}
diff --git a/gcc/testsuite/rust/compile/macro28.rs b/gcc/testsuite/rust/compile/macro28.rs
index 3d83c08ec42..8002f284ecf 100644
--- a/gcc/testsuite/rust/compile/macro28.rs
+++ b/gcc/testsuite/rust/compile/macro28.rs
@@ -1,6 +1,6 @@
 macro_rules! m {
     ($a:expr $(tok $es:expr)*) => {
-        // { dg-error "fragment is not allowed after .expr. fragment" "" { target *-*-* } .-1 }
+        // { dg-error "token .identifier. is not allowed after .expr. fragment" "" { target *-*-* } .-1 }
         // { dg-error "required first macro rule in macro rules definition could not be parsed" "" { target *-*-* } .-2 }
         // { dg-error "failed to parse item in crate" "" { target *-*-* } .-3 }
         $a


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:23 [gcc/devel/rust/master] macros: Allow repetitions of tokens in follow-set in follow-set Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).