]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix segfault on exported macro
authorLucas Ly Ba <lucas.ly-ba@outlook.fr>
Wed, 15 Oct 2025 13:23:05 +0000 (13:23 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Nov 2025 14:58:17 +0000 (15:58 +0100)
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 <lucas.ly-ba@outlook.fr>
gcc/rust/metadata/rust-export-metadata.cc
gcc/rust/metadata/rust-export-metadata.h
gcc/rust/util/rust-hir-map.cc
gcc/rust/util/rust-hir-map.h
gcc/testsuite/rust/compile/issue-3617.rs [new file with mode: 0644]

index 4dfc28036c35a6b0a5a2a341322771cda94282ce..a8d4af129db3670123e4eb668285492b8d77cc62 100644 (file)
@@ -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);
 }
 
index ee006cd83d1abb86172ef8978a55a7b337faaceb..7747d95bc153734b6efbcbe5f8d9be7c4b89c3b9 100644 (file)
@@ -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;
 
index 4629e6a570261fe971bbad29cbdc2bde6b31a52d..1587c7ee7a22530316af2cc2bafe2ba698b5b36b 100644 (file)
@@ -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;
index c8fafa4a35f099837d88885ac3ba91666f969d6a..8a284cb938bd424ae4b9f2b909820ed0cc796b27 100644 (file)
@@ -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 (file)
index 0000000..64c2166
--- /dev/null
@@ -0,0 +1,14 @@
+macro_rules! quote_tokens {
+    () => {
+        #[macro_export]
+        macro_rules! inner {
+                    () => {
+                        $crate::
+                    }
+                }
+    };
+}
+
+pub fn main() {
+    quote_tokens!();
+}