public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macros: Error out when repeating metavars which refer to repetitions
@ 2022-06-08 12:44 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:44 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:16da547c6de0957d4dd697c53260a6bb9a087163

commit 16da547c6de0957d4dd697c53260a6bb9a087163
Author: CohenArthur <arthur.cohen@epita.fr>
Date:   Sat May 7 18:45:21 2022 +0200

    macros: Error out when repeating metavars which refer to repetitions
    
    In the case were a repeting metavar was used as a regular metavar ($var
    instead of $($var)* for example), the compiler would crash on an
    assertion that $var was only repeating once. We should instead error out
    and point to the user that this is probably not what they intended to
    do.

Diff:
---
 gcc/rust/expand/rust-macro-substitute-ctx.cc  | 15 +++++++++++++++
 gcc/testsuite/rust/compile/macro-issue1224.rs |  9 +++++++++
 2 files changed, 24 insertions(+)

diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.cc b/gcc/rust/expand/rust-macro-substitute-ctx.cc
index a6933bb6aab..b7ab3abc0d9 100644
--- a/gcc/rust/expand/rust-macro-substitute-ctx.cc
+++ b/gcc/rust/expand/rust-macro-substitute-ctx.cc
@@ -16,6 +16,21 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar)
     }
   else
     {
+      // If we are expanding a metavar which has a lof of matches, we are
+      // currently expanding a repetition metavar - not a simple metavar. We
+      // need to error out and inform the user.
+      // Associated test case for an example: compile/macro-issue1224.rs
+      if (it->second.get_match_amount () != 1)
+	{
+	  rust_error_at (metavar->get_locus (),
+			 "metavariable is still repeating at this depth");
+	  rust_inform (
+	    metavar->get_locus (),
+	    "you probably forgot the repetition operator: %<%s%s%s%>", "$(",
+	    metavar->as_string ().c_str (), ")*");
+	  return expanded;
+	}
+
       // We only care about the vector when expanding repetitions.
       // Just access the first element of the vector.
       auto &frag = it->second.get_single_fragment ();
diff --git a/gcc/testsuite/rust/compile/macro-issue1224.rs b/gcc/testsuite/rust/compile/macro-issue1224.rs
new file mode 100644
index 00000000000..003bbcd5067
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro-issue1224.rs
@@ -0,0 +1,9 @@
+macro_rules! impl_uint {
+    ($($ty:ident),*) => {
+        impl $ty {} // { dg-error "metavariable is still repeating at this depth" }
+                    // { dg-error "unrecognised token" "" { target *-*-* } .-1 } // Spurious
+                    // { dg-error "could not parse type" "" { target *-*-* } .-2 } // Spurious
+    };
+}
+
+impl_uint!(u8, u16, u32, u64, u128);


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

only message in thread, other threads:[~2022-06-08 12:44 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:44 [gcc/devel/rust/master] macros: Error out when repeating metavars which refer to repetitions 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).