https://gcc.gnu.org/g:327eb6a10595d655c3de67f3c6c16d7a8824828c
commit r17-1873-g327eb6a10595d655c3de67f3c6c16d7a8824828c Author: Arthur Cohen <[email protected]> Date: Tue Apr 21 05:04:54 2026 +0200 gccrs: nr: Add more namespace lookup APIs 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. Diff: --- .../rust-finalized-name-resolution-context.cc | 7 ++++++ .../rust-finalized-name-resolution-context.h | 2 ++ gcc/rust/resolve/rust-forever-stack.h | 5 ++-- gcc/rust/resolve/rust-name-resolution-context.cc | 8 +++--- gcc/rust/resolve/rust-name-resolution-context.h | 29 ++++++++++++++++++++-- gcc/rust/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 6df72bd5187c..a8902e283282 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 5834a70c5f23..e29552d2b294 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 47e769abe758..3a2877244837 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 a70eccfa8ca9..c84c34258a60 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 e2e2e3ebd290..fa383df37a07 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 94830ddbd593..bb1f1d06f817 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 {
