From: Lucas Ly Ba <[email protected]>

An imbricated exported macro leads to a segfault.

gcc/rust/ChangeLog:

        * metadata/rust-export-metadata.cc (ExportContext::emit_macro):
        Change method argument NodeId to AST::MacroRulesDefinition.
        * metadata/rust-export-metadata.h:
        Likewise.
        * util/rust-hir-map.cc (Mappings::insert_exported_macro):
        Insert AST::MacroRulesDefinition instead of NodeId.
        * util/rust-hir-map.h:
        Change methods declarations of exported macros.

gcc/testsuite/ChangeLog:

        * rust/compile/issue-3617.rs: New test.

Signed-off-by: Lucas Ly Ba <[email protected]>
---
 gcc/rust/metadata/rust-export-metadata.cc |  9 ++++-----
 gcc/rust/metadata/rust-export-metadata.h  |  2 +-
 gcc/rust/util/rust-hir-map.cc             |  4 ++--
 gcc/rust/util/rust-hir-map.h              |  4 ++--
 gcc/testsuite/rust/compile/issue-3617.rs  | 14 ++++++++++++++
 5 files changed, 23 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-3617.rs

diff --git a/gcc/rust/metadata/rust-export-metadata.cc 
b/gcc/rust/metadata/rust-export-metadata.cc
index 4dfc28036c3..a8d4af129db 100644
--- a/gcc/rust/metadata/rust-export-metadata.cc
+++ b/gcc/rust/metadata/rust-export-metadata.cc
@@ -23,6 +23,7 @@
 #include "rust-ast-dump.h"
 #include "rust-abi.h"
 #include "rust-item.h"
+#include "rust-macro.h"
 #include "rust-object-export.h"
 
 #include "md5.h"
@@ -111,14 +112,12 @@ ExportContext::emit_function (const HIR::Function &fn)
 }
 
 void
-ExportContext::emit_macro (NodeId macro)
+ExportContext::emit_macro (AST::MacroRulesDefinition &macro)
 {
   std::stringstream oss;
   AST::Dump dumper (oss);
 
-  AST::Item *item = mappings.lookup_ast_item (macro).value ();
-
-  dumper.go (*item);
+  dumper.go (macro);
 
   public_interface_buffer += oss.str ();
 }
@@ -195,7 +194,7 @@ PublicInterface::gather_export_data ()
        vis_item.accept_vis (visitor);
     }
 
-  for (const auto &macro : mappings.get_exported_macros ())
+  for (auto &macro : mappings.get_exported_macros ())
     context.emit_macro (macro);
 }
 
diff --git a/gcc/rust/metadata/rust-export-metadata.h 
b/gcc/rust/metadata/rust-export-metadata.h
index ee006cd83d1..7747d95bc15 100644
--- a/gcc/rust/metadata/rust-export-metadata.h
+++ b/gcc/rust/metadata/rust-export-metadata.h
@@ -48,7 +48,7 @@ public:
    * directly refer to them using their NodeId. There's no need to keep an HIR
    * node for them.
    */
-  void emit_macro (NodeId macro);
+  void emit_macro (AST::MacroRulesDefinition &macro);
 
   const std::string &get_interface_buffer () const;
 
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 4629e6a5702..1587c7ee7a2 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -925,10 +925,10 @@ Mappings::lookup_macro_invocation (AST::MacroInvocation 
&invoc)
 void
 Mappings::insert_exported_macro (AST::MacroRulesDefinition &def)
 {
-  exportedMacros.emplace_back (def.get_node_id ());
+  exportedMacros.emplace_back (def);
 }
 
-std::vector<NodeId> &
+std::vector<AST::MacroRulesDefinition>
 Mappings::get_exported_macros ()
 {
   return exportedMacros;
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index c8fafa4a35f..8a284cb938b 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -279,7 +279,7 @@ public:
   lookup_macro_invocation (AST::MacroInvocation &invoc);
 
   void insert_exported_macro (AST::MacroRulesDefinition &def);
-  std::vector<NodeId> &get_exported_macros ();
+  std::vector<AST::MacroRulesDefinition> get_exported_macros ();
 
   void insert_derive_proc_macros (CrateNum num,
                                  std::vector<CustomDeriveProcMacro> macros);
@@ -408,7 +408,7 @@ private:
   std::map<NodeId, std::pair<AST::MacroRulesDefinition *, CrateNum>>
     macroMappings;
   std::map<NodeId, AST::MacroRulesDefinition *> macroInvocations;
-  std::vector<NodeId> exportedMacros;
+  std::vector<AST::MacroRulesDefinition> exportedMacros;
 
   // Procedural macros
   std::map<CrateNum, std::vector<CustomDeriveProcMacro>>
diff --git a/gcc/testsuite/rust/compile/issue-3617.rs 
b/gcc/testsuite/rust/compile/issue-3617.rs
new file mode 100644
index 00000000000..64c2166c112
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3617.rs
@@ -0,0 +1,14 @@
+macro_rules! quote_tokens {
+    () => {
+        #[macro_export]
+        macro_rules! inner {
+                    () => {
+                        $crate::
+                    }
+                }
+    };
+}
+
+pub fn main() {
+    quote_tokens!();
+}
-- 
2.50.1

Reply via email to