]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: nr: Improve Macro Def/Invoc mappings
authorArthur Cohen <arthur.cohen@embecosm.com>
Fri, 3 Apr 2026 15:16:14 +0000 (17:16 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 25 Jun 2026 17:21:27 +0000 (19:21 +0200)
This now uses find_leaf_definition to better resolve macro definitions
to their actual definitions instead of a possible import, and likewise for
invocations. This also improves the robustness and error checking for
resolving definitions.

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc (Early::insert_once): Rename...
(Early::try_insert_once): ...to this, and improve logic.
(Early::go): Use new API.
(Early::visit): Likewise.
(Early::finalize_simple_import): Likewise.
(Early::finalize_rebind_import): Likewise.
* resolve/rust-early-name-resolver-2.0.h: Declare the new API.

gcc/rust/resolve/rust-early-name-resolver-2.0.cc
gcc/rust/resolve/rust-early-name-resolver-2.0.h

index 2109ab263e79834096e9a1c0e97cf0896d58e4f1..8ba452d66166a0811d3ef7a22bd137586a66a6b6 100644 (file)
@@ -37,10 +37,17 @@ Early::Early (NameResolutionContext &ctx)
 {}
 
 void
-Early::insert_once (AST::MacroInvocation &invocation, NodeId resolved)
+Early::try_insert_once (AST::MacroInvocation &invocation, NodeId resolved)
 {
-  // TODO: Should we use `ctx.mark_resolved()`?
-  auto definition = ctx.mappings.lookup_macro_def (resolved);
+  auto leaf_macro = ctx.find_leaf_definition (resolved);
+
+  // Sometimes the import itself isn't resolved yet this turn of the fixed-point
+  if (!leaf_macro)
+    return;
+
+  // TODO: Should we use `ctx.map_usage()`?
+
+  auto definition = ctx.mappings.lookup_macro_def (leaf_macro->id);
 
   if (!ctx.mappings.lookup_macro_invocation (invocation))
     ctx.mappings.insert_macro_invocation (invocation, definition.value ());
@@ -49,7 +56,6 @@ Early::insert_once (AST::MacroInvocation &invocation, NodeId resolved)
 void
 Early::insert_once (AST::MacroRulesDefinition &def)
 {
-  // TODO: Should we use `ctx.mark_resolved()`?
   if (!ctx.mappings.lookup_macro_def (def.get_node_id ()))
     ctx.mappings.insert_macro_def (&def);
 }
@@ -64,6 +70,7 @@ Early::go (AST::Crate &crate)
   // us
 
   dirty = toplevel.is_dirty ();
+
   // We now proceed with resolving macros, which can be nested in almost any
   // items
   textual_scope.push ();
@@ -345,7 +352,7 @@ Early::visit (AST::MacroInvocation &invoc)
       return;
     }
 
-  insert_once (invoc, definition->get_node_id ());
+  try_insert_once (invoc, definition->get_node_id ());
 
   // now do we need to keep mappings or something? or insert "uses" into our
   // ForeverStack? can we do that? are mappings simpler?
@@ -447,7 +454,7 @@ Early::finalize_simple_import (const Early::ImportPair &mapping)
 
   for (auto &&definition : data.definitions ())
     {
-      dirty = true;
+      // dirty = true;
 
       ctx.map_usage (Usage (import_id),
                     Definition (definition.first.get_node_id ()));
@@ -455,6 +462,8 @@ Early::finalize_simple_import (const Early::ImportPair &mapping)
       toplevel
        .insert_or_error_out (identifier,
                              import.get_locus (), import_id, definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */);
+
+      dirty = dirty || toplevel.is_dirty ();
     }
 }
 
@@ -526,7 +535,7 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
 
   for (auto &&definition : data.definitions ())
     {
-      dirty = true;
+      // dirty = true;
 
       ctx.map_usage (Usage (import_id),
                     Definition (definition.first.get_node_id ()));
@@ -535,6 +544,8 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
        .insert_or_error_out (declared_name,
                              path.get_locus (), import_id, definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */);
 
+      dirty = dirty || toplevel.is_dirty ();
+
       // Map the import to the glob container if it exists - this is important
       // for 2-stepped glob imports which refer to glob containers, e.g.
       //
index 0fdf1436cbd0f4e833ea2d75b5352bc438a20bc0..ff3542ce54682c70d944cf17c37f419dd2acb2e7 100644 (file)
@@ -181,7 +181,7 @@ private:
    * and it will not trigger assertions for already resolved invocations.
    */
   // TODO: Rename
-  void insert_once (AST::MacroInvocation &invocation, NodeId resolved);
+  void try_insert_once (AST::MacroInvocation &invocation, NodeId resolved);
   // TODO: Rename
   void insert_once (AST::MacroRulesDefinition &definition);