diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 27d3134ec2a1..ed01cde95702 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -578,6 +578,11 @@ class MacroRulesDefinition : public VisItem MacroKind get_kind () const { return kind; } + std::unique_ptr clone_macro_rules_def () const + { + return std::unique_ptr (clone_item_impl ()); + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index a15d310ce6d2..1f76ef77574d 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -917,7 +917,8 @@ Mappings::insert_macro_invocation (AST::MacroInvocation &invoc, auto it = macroInvocations.find (invoc.get_macro_node_id ()); rust_assert (it == macroInvocations.end ()); - macroInvocations[invoc.get_macro_node_id ()] = def; + // TODO: remove hack that converts use-after-free into memory leak + macroInvocations[invoc.get_macro_node_id ()] = def->clone_macro_rules_def ().release (); } bool