Skip to content

Commit 1fe6ae5

Browse files
author
Lucas Ly Ba
committed
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]>
1 parent b92a684 commit 1fe6ae5

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

gcc/rust/metadata/rust-export-metadata.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "rust-ast-dump.h"
2424
#include "rust-abi.h"
2525
#include "rust-item.h"
26+
#include "rust-macro.h"
2627
#include "rust-object-export.h"
2728

2829
#include "md5.h"
@@ -111,14 +112,12 @@ ExportContext::emit_function (const HIR::Function &fn)
111112
}
112113

113114
void
114-
ExportContext::emit_macro (NodeId macro)
115+
ExportContext::emit_macro (AST::MacroRulesDefinition macro)
115116
{
116117
std::stringstream oss;
117118
AST::Dump dumper (oss);
118119

119-
AST::Item *item = mappings.lookup_ast_item (macro).value ();
120-
121-
dumper.go (*item);
120+
dumper.go (macro);
122121

123122
public_interface_buffer += oss.str ();
124123
}

gcc/rust/metadata/rust-export-metadata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ExportContext
4848
* directly refer to them using their NodeId. There's no need to keep an HIR
4949
* node for them.
5050
*/
51-
void emit_macro (NodeId macro);
51+
void emit_macro (AST::MacroRulesDefinition macro);
5252

5353
const std::string &get_interface_buffer () const;
5454

gcc/rust/util/rust-hir-map.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,10 +925,10 @@ Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc)
925925
void
926926
Mappings::insert_exported_macro (AST::MacroRulesDefinition &def)
927927
{
928-
exportedMacros.emplace_back (def.get_node_id ());
928+
exportedMacros.emplace_back (def);
929929
}
930930

931-
std::vector<NodeId> &
931+
std::vector<AST::MacroRulesDefinition>
932932
Mappings::get_exported_macros ()
933933
{
934934
return exportedMacros;

gcc/rust/util/rust-hir-map.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class Mappings
279279
lookup_macro_invocation (AST::MacroInvocation &invoc);
280280

281281
void insert_exported_macro (AST::MacroRulesDefinition &def);
282-
std::vector<NodeId> &get_exported_macros ();
282+
std::vector<AST::MacroRulesDefinition> get_exported_macros ();
283283

284284
void insert_derive_proc_macros (CrateNum num,
285285
std::vector<CustomDeriveProcMacro> macros);
@@ -408,7 +408,7 @@ class Mappings
408408
std::map<NodeId, std::pair<AST::MacroRulesDefinition *, CrateNum>>
409409
macroMappings;
410410
std::map<NodeId, AST::MacroRulesDefinition *> macroInvocations;
411-
std::vector<NodeId> exportedMacros;
411+
std::vector<AST::MacroRulesDefinition> exportedMacros;
412412

413413
// Procedural macros
414414
std::map<CrateNum, std::vector<CustomDeriveProcMacro>>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! quote_tokens {
2+
() => {
3+
#[macro_export]
4+
macro_rules! inner {
5+
() => {
6+
$crate::
7+
}
8+
}
9+
};
10+
}
11+
12+
pub fn main() {
13+
quote_tokens!();
14+
}

0 commit comments

Comments
 (0)