aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCohenArthur <arthur.cohen@epita.fr>2022-05-07 18:45:21 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2022-05-09 12:35:39 +0200
commit16da547c6de0957d4dd697c53260a6bb9a087163 (patch)
tree3766599e0ae4c76c50d47e162b23b18ed8fbc749
parent9ea940e4dcabbf99fbb44c125a0af7cf82e48146 (diff)
downloadgcc-16da547c6de0957d4dd697c53260a6bb9a087163.zip
gcc-16da547c6de0957d4dd697c53260a6bb9a087163.tar.gz
gcc-16da547c6de0957d4dd697c53260a6bb9a087163.tar.bz2
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.
-rw-r--r--gcc/rust/expand/rust-macro-substitute-ctx.cc15
-rw-r--r--gcc/testsuite/rust/compile/macro-issue1224.rs9
2 files changed, 24 insertions, 0 deletions
diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.cc b/gcc/rust/expand/rust-macro-substitute-ctx.cc
index a6933bb..b7ab3ab 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 0000000..003bbcd
--- /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);