From c8bd8f99fe2fe98622fe8da9006b162a343f5cb6 Mon Sep 17 00:00:00 2001 From: Lucas Ly Ba Date: Wed, 15 Oct 2025 13:23:05 +0000 Subject: [PATCH] gccrs: fix segfault on exported macro 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 --- 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 ¯o) { 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 ¯o : mappings.get_exported_macros ()) + for (auto ¯o : 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 ¯o); 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 & +std::vector 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 &get_exported_macros (); + std::vector get_exported_macros (); void insert_derive_proc_macros (CrateNum num, std::vector macros); @@ -408,7 +408,7 @@ private: std::map> macroMappings; std::map macroInvocations; - std::vector exportedMacros; + std::vector exportedMacros; // Procedural macros std::map> 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.47.3