From 35ca685200830626e5abd623f65a850649beace2 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Fri, 18 Mar 2022 16:20:47 +0100 Subject: macros: Add base functions to check for follow-set ambiguities Rust does not allow for all macro fragments to be followed by any kind of tokens: We must check tokens following those fragments that might contain restrictions and make sure that they are allowed, conforming to the Macro Follow-Set Ambiguity specification Co-authored-by: philberty macro-frag-spec: Transform enum into a class This allows us to add methods on the fragment specifier, which are needed to make sure that follow-set ambiguities are respected tests: Add tests for forbidden follow-up tokens This also fix a test that was previously accepted but invalid: rustc also rejected it --- gcc/rust/parse/rust-parse-impl.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'gcc/rust/parse/rust-parse-impl.h') diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 644e789..1d1b624 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -22,6 +22,7 @@ along with GCC; see the file COPYING3. If not see #include "rust-diagnostics.h" #include "util/rust-make-unique.h" +#include namespace Rust { // Left binding powers of operations. @@ -1767,6 +1768,13 @@ Parser::parse_macro_matcher () return AST::MacroMatcher::create_error (t->get_locus ()); } + if (matches.size () > 0) + { + auto &last_match = matches.back (); + if (!is_match_compatible (*last_match, *match)) + return AST::MacroMatcher::create_error (match->get_match_locus ()); + } + matches.push_back (std::move (match)); // DEBUG @@ -1955,8 +1963,9 @@ Parser::parse_macro_match_fragment () if (t == nullptr) return nullptr; - AST::MacroFragSpec frag = AST::get_frag_spec_from_str (t->get_str ()); - if (frag == AST::INVALID) + AST::MacroFragSpec frag + = AST::MacroFragSpec::get_frag_spec_from_str (t->get_str ()); + if (frag.is_error ()) { Error error (t->get_locus (), "invalid fragment specifier %qs in fragment macro match", -- cgit v1.1