From: Arthur Cohen <[email protected]>

Add more functions for looking up Usage(s) in multiple namespaces.

gcc/rust/ChangeLog:

        * resolve/rust-finalized-name-resolution-context.cc
        (FinalizedNameResolutionContext::lookup): New function.
        * resolve/rust-finalized-name-resolution-context.h: Declare it.
        * resolve/rust-forever-stack.h: Remove assertion in map_usage.
        * resolve/rust-name-resolution-context.h: Add new NSLookup return type.
        * resolve/rust-name-resolution-context.cc 
(NameResolutionContext::lookup): Use it.
        * typecheck/rust-hir-type-check-path.cc 
(TypeCheckExpr::resolve_root_path): Likewise.
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/85788d3cdcdd0faab9ae80db2ef1335edba38b2d

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4551

 .../rust-finalized-name-resolution-context.cc |  7 +++++
 .../rust-finalized-name-resolution-context.h  |  2 ++
 gcc/rust/resolve/rust-forever-stack.h         |  5 ++--
 .../resolve/rust-name-resolution-context.cc   |  8 +++--
 .../resolve/rust-name-resolution-context.h    | 29 +++++++++++++++++--
 .../typecheck/rust-hir-type-check-path.cc     |  5 ++--
 6 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/gcc/rust/resolve/rust-finalized-name-resolution-context.cc 
b/gcc/rust/resolve/rust-finalized-name-resolution-context.cc
index 6df72bd51..a8902e283 100644
--- a/gcc/rust/resolve/rust-finalized-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-finalized-name-resolution-context.cc
@@ -57,6 +57,13 @@ FinalizedNameResolutionContext::lookup (NodeId usage, 
Namespace ns) const
   return ctx.lookup (usage, ns);
 }
 
+tl::optional<NameResolutionContext::NSLookup>
+FinalizedNameResolutionContext::lookup (NodeId usage, Namespace ns1,
+                                       Namespace ns2) const
+{
+  return ctx.lookup (usage, ns1, ns2);
+}
+
 Resolver::CanonicalPath
 FinalizedNameResolutionContext::to_canonical_path (NodeId id,
                                                   Namespace ns) const
diff --git a/gcc/rust/resolve/rust-finalized-name-resolution-context.h 
b/gcc/rust/resolve/rust-finalized-name-resolution-context.h
index 5834a70c5..e29552d2b 100644
--- a/gcc/rust/resolve/rust-finalized-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-finalized-name-resolution-context.h
@@ -52,6 +52,8 @@ public:
    * Same as NameResolutionContext::lookup
    */
   tl::optional<NodeId> lookup (NodeId usage, Namespace ns) const;
+  tl::optional<NameResolutionContext::NSLookup>
+  lookup (NodeId usage, Namespace ns1, Namespace ns2) const;
 
   /**
    * Same as NameResolutionContext::to_canonical_path
diff --git a/gcc/rust/resolve/rust-forever-stack.h 
b/gcc/rust/resolve/rust-forever-stack.h
index 47e769abe..3a2877244 100644
--- a/gcc/rust/resolve/rust-forever-stack.h
+++ b/gcc/rust/resolve/rust-forever-stack.h
@@ -909,10 +909,11 @@ public:
 
   void map_usage (Usage usage, Definition definition)
   {
-    auto inserted = resolved_nodes.emplace (usage, definition).second;
+    auto inserted = resolved_nodes.emplace (usage, definition);
 
     // is that valid?
-    rust_assert (inserted);
+    // FIXME: Yikes
+    // rust_assert (inserted.first->first.id == definition.id);
   }
 
   tl::optional<NodeId> lookup (NodeId usage) const
diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc 
b/gcc/rust/resolve/rust-name-resolution-context.cc
index a70eccfa8..c84c34258 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-name-resolution-context.cc
@@ -304,13 +304,15 @@ NameResolutionContext::lookup (NodeId usage, Namespace 
ns) const
     }
 }
 
-tl::optional<NodeId>
+tl::optional<NameResolutionContext::NSLookup>
 NameResolutionContext::lookup (NodeId usage, Namespace ns1, Namespace ns2) 
const
 {
   if (auto result = lookup (usage, ns1))
-    return result;
+    return NSLookup (*result, ns1);
 
-  return lookup (usage, ns2);
+  return lookup (usage, ns2).map ([&ns2] (NodeId id) {
+    return NSLookup (id, ns2);
+  });
 }
 
 void
diff --git a/gcc/rust/resolve/rust-name-resolution-context.h 
b/gcc/rust/resolve/rust-name-resolution-context.h
index e2e2e3ebd..fa383df37 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-name-resolution-context.h
@@ -524,10 +524,35 @@ public:
 
   CanonicalPathCtx canonical_ctx;
 
+  /**
+   * The result type for a multi-namespace call to
+   * NameResolutionContext::lookup()
+   */
+  struct NSLookup
+  {
+    NodeId id;
+    Namespace ns;
+
+    NSLookup (NodeId id, Namespace ns) : id (id), ns (ns) {}
+  };
+
+  /**
+   * These functions are mostly useful for the FinalizedNameResolutionContext
+   * and used in later passes of the pipeline. They don't need to know as much
+   * about a definition, hence why they don't use the NamespacedDefinition 
which
+   * returns a Rib::Definition.
+   */
   void map_usage (Usage usage, Definition definition, Namespace ns);
   tl::optional<NodeId> lookup (NodeId usage, Namespace ns) const;
-  tl::optional<NodeId> lookup (NodeId usage, Namespace ns1,
-                              Namespace ns2) const;
+
+  /**
+   * The order of namespaces is important - if the usage resolves in the first
+   * namespace, then it will be returned. Collisions are not guarded against 
and
+   * should NOT happen. This is for looking up usages once name resolution is
+   * done and we are in later stages of the pipeline.
+   */
+  tl::optional<NSLookup> lookup (NodeId usage, Namespace ns1,
+                                Namespace ns2) const;
 
   Resolver::CanonicalPath to_canonical_path (NodeId id, Namespace ns) const
   {
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc 
b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index 94830ddbd..bb1f1d06f 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -260,9 +260,10 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression 
&expr, size_t *offset,
 
       // lookup the reference_node_id
       NodeId ref_node_id;
-      if (auto res = nr_ctx.lookup (ast_node_id, 
Resolver2_0::Namespace::Types))
+      if (auto res = nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types,
+                                   Resolver2_0::Namespace::Values))
        {
-         ref_node_id = *res;
+         ref_node_id = res->id;
        }
       else
        {
-- 
2.54.0

Reply via email to