]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
gccrs: Add all rust keywords (except priv) to the follow-set of `:vis` when parsing...
authorTage Johansson <frans.tage@gmail.com>
Sat, 4 Mar 2023 18:43:02 +0000 (19:43 +0100)
committerCohenArthur <arthur.cohen@embecosm.com>
Thu, 16 Mar 2023 10:19:16 +0000 (10:19 +0000)
Previously, the following macro rules were rejected by gccrs:
```Rust
macro_rules! {
    ($v:vis <KEY_WORD>) => { ... };
}
```

This PR fixes so the above code is accepted by the compiler for all key words like `async` or `unsafe`.
The only exception is the keyword `priv` which is not allowed.
See [this page](https://doc.rust-lang.org/reference/macro-ambiguity.html) for reference. Especially the following excerpt:
> FOLLOW(vis) = {,l any keyword or identifier except a non-raw priv; any token that can begin a type; ident, ty, and path nonterminals}.

Fixes #1060

gcc/rust/ChangeLog:

* parse/rust-parse.cc: fix follow-sets

gcc/testsuite/ChangeLog:

* rust/compile/macro47.rs: Test that :vis can be followed by some keywords
* rust/compile/macro48.rs: Test that :vis cannot be followed by the keyword priv

Signed-off-by: Tage Johansson <frans.tage@gmail.com>
gcc/rust/parse/rust-parse.cc
gcc/testsuite/rust/compile/macro47.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/macro48.rs [new file with mode: 0644]

index f1e2caa258b0ca6869255aecc385a9e905dd6663..1b565bc8f456a5563bdad170f3f266472295dcee 100644 (file)
@@ -17,6 +17,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "rust-parse.h"
 #include "rust-linemap.h"
 #include "rust-diagnostics.h"
+#include "rust-token.h"
 
 namespace Rust {
 
@@ -166,34 +167,69 @@ peculiar_fragment_match_compatible (const AST::MacroMatchFragment &last_match,
        {MATCH_ARROW, COMMA, EQUAL, PIPE, SEMICOLON, COLON, RIGHT_ANGLE,
         RIGHT_SHIFT, LEFT_SQUARE, LEFT_CURLY, AS, WHERE}},
        {AST::MacroFragSpec::VIS,
-       {
-         COMMA,
-         IDENTIFIER /* FIXME: Other than `priv` */,
-         LEFT_PAREN,
-         LEFT_SQUARE,
-         EXCLAM,
-         ASTERISK,
-         AMP,
-         LOGICAL_AND,
-         QUESTION_MARK,
-         LIFETIME,
-         LEFT_ANGLE,
-         LEFT_SHIFT,
-         SUPER,
-         SELF,
-         SELF_ALIAS,
-         EXTERN_TOK,
-         CRATE,
-         UNDERSCORE,
-         FOR,
-         IMPL,
-         FN_TOK,
-         UNSAFE,
-         TYPEOF,
-         DYN
-         // FIXME: Add Non kw identifiers
-         // FIXME: Add $crate as valid
-       }}};
+       {COMMA,
+        IDENTIFIER,
+        LEFT_PAREN,
+        LEFT_SQUARE,
+        EXCLAM,
+        ASTERISK,
+        AMP,
+        LOGICAL_AND,
+        QUESTION_MARK,
+        LIFETIME,
+        LEFT_ANGLE,
+        LEFT_SHIFT,
+        UNDERSCORE,
+        ABSTRACT,
+        AS,
+        ASYNC,
+        AUTO,
+        BECOME,
+        BOX,
+        BREAK,
+        CONST,
+        CONTINUE,
+        CRATE,
+        DO,
+        DYN,
+        ELSE,
+        ENUM_TOK,
+        EXTERN_TOK,
+        FALSE_LITERAL,
+        FINAL_TOK,
+        FN_TOK,
+        FOR,
+        IF,
+        IMPL,
+        IN,
+        LET,
+        LOOP,
+        MACRO,
+        MATCH_TOK,
+        MOD,
+        MOVE,
+        MUT,
+        OVERRIDE_TOK,
+        PUB,
+        REF,
+        RETURN_TOK,
+        SELF_ALIAS,
+        SELF,
+        STATIC_TOK,
+        STRUCT_TOK,
+        SUPER,
+        TRAIT,
+        TRUE_LITERAL,
+        TRY,
+        TYPE,
+        TYPEOF,
+        UNSAFE,
+        UNSIZED,
+        USE,
+        VIRTUAL,
+        WHERE,
+        WHILE,
+        YIELD}}};
 
   Location error_locus = match.get_match_locus ();
   std::string kind_str = "fragment";
diff --git a/gcc/testsuite/rust/compile/macro47.rs b/gcc/testsuite/rust/compile/macro47.rs
new file mode 100644 (file)
index 0000000..36545af
--- /dev/null
@@ -0,0 +1,10 @@
+// Check the follow-set of :vis in macro rules.
+
+macro_rules! my_mac {
+    ($v:vis async) => {
+        $v struct Foo(i32);
+    };
+    ($v:vis $i:ident) => {
+        $v struct $i(i32);
+    }
+}
diff --git a/gcc/testsuite/rust/compile/macro48.rs b/gcc/testsuite/rust/compile/macro48.rs
new file mode 100644 (file)
index 0000000..6b3b369
--- /dev/null
@@ -0,0 +1,10 @@
+// Check that "priv" is not in the follow set of :vis.
+
+// { dg-error "token .priv. is not allowed after .vis. fragment" "#359" { target *-*-* } .+4 }
+// { dg-error "required first macro rule in macro rules definition could not be parsed" "" { target *-*-* } .+3 }
+// { dg-error "failed to parse item in crate" "" { target *-*-* } .+2 }
+macro_rules! my_mac {
+    ($v:vis priv) => {
+        $v struct Foo(i32);
+    }
+}