https://gcc.gnu.org/g:c8bd8f99fe2fe98622fe8da9006b162a343f5cb6
commit r16-5340-gc8bd8f99fe2fe98622fe8da9006b162a343f5cb6 Author: Lucas Ly Ba <[email protected]> Date: Wed Oct 15 13:23:05 2025 +0000 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 <[email protected]> Diff: --- 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(-) diff --git a/gcc/rust/metadata/rust-export-metadata.cc b/gcc/rust/metadata/rust-export-metadata.cc index 4dfc28036c35..a8d4af129db3 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 ee006cd83d1a..7747d95bc153 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 4629e6a57026..1587c7ee7a22 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 c8fafa4a35f0..8a284cb938bd 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 000000000000..64c2166c112d --- /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!(); +}
