public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] lexer: Allow specifiying tokens as reserved in certain editions
@ 2022-07-19 21:57 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-07-19 21:57 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:66af31d5bc5a1ea76be381dc9246fbe4fe034fbb

commit 66af31d5bc5a1ea76be381dc9246fbe4fe034fbb
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Tue Jul 19 13:09:17 2022 +0200

    lexer: Allow specifiying tokens as reserved in certain editions
    
    Some tokens such as `try` only became reserved keywords in certain
    editions. This behavior might happen again in the future and we should
    be able to handle it.

Diff:
---
 gcc/rust/lex/rust-lex.cc                        | 19 +++++++++++++++++--
 gcc/rust/rust-session-manager.h                 |  6 ++++--
 gcc/testsuite/rust/compile/macro-issue1395-2.rs |  7 +++++++
 gcc/testsuite/rust/compile/macro-issue1395.rs   |  5 +++++
 4 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc
index 93d96d33dd6..ecf151dc778 100644
--- a/gcc/rust/lex/rust-lex.cc
+++ b/gcc/rust/lex/rust-lex.cc
@@ -21,6 +21,7 @@
 #include "rust-system.h"      // for rust_assert and rust_unreachable
 #include "rust-diagnostics.h" // for rust_error_at
 #include "rust-linemap.h"
+#include "rust-session-manager.h"
 #include "safe-ctype.h"
 
 namespace Rust {
@@ -228,10 +229,24 @@ Lexer::classify_keyword (const std::string &str)
 
   if (idx == last || str != *idx)
     return IDENTIFIER;
-  else
-    return keyword_keys[idx - keyword_index];
 
   // TODO: possibly replace this x-macro system with something like hash map?
+
+  // We now have the expected token ID of the reserved keyword. However, some
+  // keywords are reserved starting in certain editions. For example, `try` is
+  // only a reserved keyword in editions >=2018. The language might gain new
+  // reserved keywords in the future.
+  //
+  // https://doc.rust-lang.org/reference/keywords.html#reserved-keywords
+  auto id = keyword_keys[idx - keyword_index];
+
+  // `try` is not a reserved keyword before 2018
+  if (Session::get_instance ().options.get_edition ()
+	== CompileOptions::Edition::E2015
+      && id == TRY)
+    return IDENTIFIER;
+
+  return id;
 }
 
 TokenPtr
diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h
index c68e796461f..24a15f55393 100644
--- a/gcc/rust/rust-session-manager.h
+++ b/gcc/rust/rust-session-manager.h
@@ -191,13 +191,13 @@ struct CompileOptions
   bool proc_macro = false;
   std::string metadata_output_path;
 
-  enum Edition
+  enum class Edition
   {
     E2015 = 0,
     E2018,
     E2021,
   } edition
-    = E2015;
+    = Edition::E2015;
 
   bool dump_option_enabled (DumpOption option) const
   {
@@ -239,6 +239,8 @@ struct CompileOptions
     edition = static_cast<Edition> (raw_edition);
   }
 
+  const Edition &get_edition () { return edition; }
+
   void set_metadata_output (const std::string &path)
   {
     metadata_output_path = path;
diff --git a/gcc/testsuite/rust/compile/macro-issue1395-2.rs b/gcc/testsuite/rust/compile/macro-issue1395-2.rs
new file mode 100644
index 00000000000..1df6a3a0038
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro-issue1395-2.rs
@@ -0,0 +1,7 @@
+// { dg-additional-options "-frust-edition=2018" }
+
+macro_rules! try {
+    // { dg-error "expecting .identifier. but .try. found" "" { target *-*-* } .-1 }
+    // { dg-error "failed to parse item in crate" "" { target *-*-* } .-2 }
+    () => {};
+}
diff --git a/gcc/testsuite/rust/compile/macro-issue1395.rs b/gcc/testsuite/rust/compile/macro-issue1395.rs
new file mode 100644
index 00000000000..b0368c1610f
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro-issue1395.rs
@@ -0,0 +1,5 @@
+// Default edition is 2015 - this is valid
+
+macro_rules! try {
+    () => {};
+}


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

only message in thread, other threads:[~2022-07-19 21:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-19 21:57 [gcc/devel/rust/master] lexer: Allow specifiying tokens as reserved in certain editions 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).