https://gcc.gnu.org/g:572cc994aabf47eed617a3a8fa7c3d6cdb2cbe37
commit r17-1872-g572cc994aabf47eed617a3a8fa7c3d6cdb2cbe37 Author: Arthur Cohen <[email protected]> Date: Thu May 21 15:25:22 2026 +0200 gccrs: nr: Create new APIs to specify namespaces in which usages should be looked-up. This fundamentally changes the way that NR works for later pipeline - instead of having one singular Usage -> Definition map like it was before, the consumers of finalized NRCtx need to specify in which namespace(s) they expect usages to be looked-up. We now have one usage map for each namespace, and add new APIs for looking up usages in one or multiple namespaces. gcc/rust/ChangeLog: * backend/rust-compile-implitem.cc (CompileTraitItem::visit): Adapt for new NR APIs. * backend/rust-compile-item.cc (CompileItem::visit): Likewise. * backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): Likewise. * checks/errors/borrowck/rust-bir-builder-internal.h: Likewise. * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_for_privacy_violation): Likewise. (PrivacyReporter::check_base_type_privacy): Likewise. (PrivacyReporter::visit): Likewise. * checks/errors/privacy/rust-privacy-reporter.h: Likewise. * checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::resolve_module_path): Likewise. * checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise. * checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): Likewise. * checks/errors/rust-readonly-check.cc (ReadonlyChecker::visit): Likewise. * checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise. * checks/lints/rust-lint-marklive.cc (MarkLive::visit): Likewise. (MarkLive::visit_path_segment): Likewise. (MarkLive::find_ref_node_id): Likewise. (MarkLive::find_value_definition): Likewise. * checks/lints/rust-lint-marklive.h: Likewise. * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): Likewise. * checks/lints/unused/rust-unused-collector.cc (UnusedCollector::visit): Likewise. * checks/lints/unused/rust-unused-collector.h: Likewise. * resolve/rust-early-name-resolver-2.0.cc (Early::resolve_glob_import): Likewise. (Early::resolve_rebind_import): Likewise. (Early::visit): Likewise. (Early::visit_derive_attribute): Likewise. (Early::visit_non_builtin_attribute): Likewise. (Early::finalize_simple_import): Likewise. (Early::finalize_rebind_import): Likewise. * resolve/rust-early-name-resolver-2.0.h: Likewise. * resolve/rust-finalized-name-resolution-context.cc (FinalizedNameResolutionContext::map_usage): Likewise. (FinalizedNameResolutionContext::lookup): Likewise. (FinalizedNameResolutionContext::to_canonical_path): Likewise. * resolve/rust-finalized-name-resolution-context.h: Likewise. * resolve/rust-forever-stack.h: Likewise. * resolve/rust-late-name-resolver-2.0.cc (visit_identifier_as_pattern): Likewise. (Late::visit): Likewise. (Late::resolve_label): Likewise. (resolve_type_path_like): Likewise. * resolve/rust-name-resolution-context.cc (CanonicalPathRecordCrateRoot::as_path): Likewise. (CanonicalPathRecordNormal::as_path): Likewise. (CanonicalPathRecordLookup::as_path): Likewise. (CanonicalPathRecordImpl::as_path): Likewise. (CanonicalPathRecordTraitImpl::as_path): Likewise. (NameResolutionContext::map_usage): Likewise. (NameResolutionContext::lookup): Likewise. * resolve/rust-name-resolution-context.h: Likewise. * typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_path_to_trait): Likewise. * typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::visit): Likewise. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise. (TypeCheckExpr::resolve_fn_trait_call): Likewise. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit): Likewise. * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise. * typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Likewise. (TypeCheckExpr::resolve_root_path): Likewise. (TypeCheckExpr::resolve_segments): Likewise. * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Likewise. (ResolveWhereClauseItem::visit): Likewise. * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise. * typecheck/rust-type-util.cc (query_type): Likewise. * backend/rust-compile-expr.h (CompileExpr::resolve_nodeid): Likewise. * backend/rust-compile-expr.cc (CompileExpr::visit): Likewise. (CompileExpr::lookup_label): Likewise. (CompileExpr::lookup_temp_var): Likewise. (CompileExpr::resolve_NodeId): Likewise, and rename to... (CompileExpr::resolve_nodeid): ...this. Diff: --- gcc/rust/backend/rust-compile-expr.cc | 18 ++- gcc/rust/backend/rust-compile-expr.h | 3 +- gcc/rust/backend/rust-compile-implitem.cc | 7 +- gcc/rust/backend/rust-compile-item.cc | 10 +- gcc/rust/backend/rust-compile-resolve-path.cc | 5 +- .../errors/borrowck/rust-bir-builder-internal.h | 10 +- .../checks/errors/privacy/rust-privacy-reporter.cc | 23 +-- .../checks/errors/privacy/rust-privacy-reporter.h | 4 +- .../errors/privacy/rust-visibility-resolver.cc | 3 +- gcc/rust/checks/errors/rust-const-checker.cc | 3 +- .../checks/errors/rust-hir-pattern-analysis.cc | 3 +- gcc/rust/checks/errors/rust-readonly-check.cc | 3 +- gcc/rust/checks/errors/rust-unsafe-checker.cc | 7 +- gcc/rust/checks/lints/rust-lint-marklive.cc | 15 +- gcc/rust/checks/lints/rust-lint-marklive.h | 2 +- .../checks/lints/unused/rust-unused-checker.cc | 4 +- .../checks/lints/unused/rust-unused-collector.cc | 3 +- .../checks/lints/unused/rust-unused-collector.h | 10 +- gcc/rust/resolve/rust-early-name-resolver-2.0.cc | 69 +++++---- gcc/rust/resolve/rust-early-name-resolver-2.0.h | 37 ++--- .../rust-finalized-name-resolution-context.cc | 14 +- .../rust-finalized-name-resolution-context.h | 8 +- gcc/rust/resolve/rust-forever-stack.h | 21 +++ gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 56 +++++--- gcc/rust/resolve/rust-name-resolution-context.cc | 86 ++++++++--- gcc/rust/resolve/rust-name-resolution-context.h | 159 +++++++++++++-------- gcc/rust/typecheck/rust-hir-trait-resolve.cc | 4 +- gcc/rust/typecheck/rust-hir-type-check-enumitem.cc | 13 +- gcc/rust/typecheck/rust-hir-type-check-expr.cc | 16 ++- gcc/rust/typecheck/rust-hir-type-check-implitem.cc | 8 +- gcc/rust/typecheck/rust-hir-type-check-item.cc | 16 ++- gcc/rust/typecheck/rust-hir-type-check-path.cc | 9 +- gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 7 +- gcc/rust/typecheck/rust-hir-type-check-type.cc | 8 +- gcc/rust/typecheck/rust-hir-type-check.cc | 4 +- gcc/rust/typecheck/rust-type-util.cc | 6 +- 36 files changed, 440 insertions(+), 234 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 57d401fd6053..5dac2439cd73 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -27,6 +27,7 @@ #include "rust-compile-implitem.h" #include "rust-constexpr.h" #include "rust-compile-type.h" +#include "rust-finalized-name-resolution-context.h" #include "rust-gcc.h" #include "rust-compile-asm.h" #include "fold-const.h" @@ -35,6 +36,7 @@ #include "print-tree.h" #include "rust-hir-bound.h" #include "rust-hir-expr.h" +#include "rust-rib.h" #include "rust-system.h" #include "rust-tree.h" #include "rust-tyty.h" @@ -901,7 +903,8 @@ CompileExpr::visit (HIR::BreakExpr &expr) NodeId resolved_node_id; if (auto id - = nr_ctx.lookup (expr.get_label ().get_mappings ().get_nodeid ())) + = nr_ctx.lookup (expr.get_label ().get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Labels)) { resolved_node_id = *id; } @@ -956,7 +959,8 @@ CompileExpr::visit (HIR::ContinueExpr &expr) NodeId resolved_node_id; if (auto id - = nr_ctx.lookup (expr.get_label ().get_mappings ().get_nodeid ())) + = nr_ctx.lookup (expr.get_label ().get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Labels)) { resolved_node_id = *id; } @@ -2799,7 +2803,7 @@ CompileExpr::construct_block_label (HIR::BlockExpr &expr) tree CompileExpr::lookup_label (NodeId to_be_resolved) { - HirId ref = resolve_NodeId (to_be_resolved); + HirId ref = resolve_nodeid (to_be_resolved, Resolver2_0::Namespace::Labels); tree label = NULL_TREE; rust_assert (ctx->lookup_label_decl (ref, &label) && "failed to lookup a label"); @@ -2809,7 +2813,9 @@ CompileExpr::lookup_label (NodeId to_be_resolved) Bvariable * CompileExpr::lookup_temp_var (NodeId to_be_resolved) { - HirId ref = resolve_NodeId (to_be_resolved); + // TODO: Not sure that this temp var should have been inserted in the Labels + // namespace? Why not values? + HirId ref = resolve_nodeid (to_be_resolved, Resolver2_0::Namespace::Labels); Bvariable *ltemp = nullptr; rust_assert (ctx->lookup_var_decl (ref, <emp) && "failed to lookup a temp var"); @@ -2817,12 +2823,12 @@ CompileExpr::lookup_temp_var (NodeId to_be_resolved) } HirId -CompileExpr::resolve_NodeId (NodeId to_be_resolved) +CompileExpr::resolve_nodeid (NodeId to_be_resolved, Resolver2_0::Namespace ns) { auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); NodeId resolved_node_id; - resolved_node_id = nr_ctx.lookup (to_be_resolved).value (); + resolved_node_id = nr_ctx.lookup (to_be_resolved, ns).value (); HirId ref = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id).value (); diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 7764448d8633..9d1238e12fcd 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -23,6 +23,7 @@ #include "rust-gcc.h" #include "rust-hir-expr.h" #include "rust-hir-visitor.h" +#include "rust-rib.h" namespace Rust { namespace Compile { @@ -156,7 +157,7 @@ protected: tree construct_block_label (HIR::BlockExpr &expr); tree lookup_label (NodeId to_be_resolved); Bvariable *lookup_temp_var (NodeId to_be_resolved); - HirId resolve_NodeId (NodeId to_be_resolved); + HirId resolve_nodeid (NodeId to_be_resolved, Resolver2_0::Namespace ns); private: CompileExpr (Context *ctx); diff --git a/gcc/rust/backend/rust-compile-implitem.cc b/gcc/rust/backend/rust-compile-implitem.cc index df572666116a..ba17184e66e0 100644 --- a/gcc/rust/backend/rust-compile-implitem.cc +++ b/gcc/rust/backend/rust-compile-implitem.cc @@ -17,6 +17,7 @@ // <http://www.gnu.org/licenses/>. #include "rust-compile-implitem.h" +#include "rust-rib.h" namespace Rust { namespace Compile { @@ -30,7 +31,8 @@ CompileTraitItem::visit (HIR::TraitItemConst &constant) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); Resolver::CanonicalPath canonical_path - = nr_ctx.to_canonical_path (constant.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (constant.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); HIR::Expr &const_value_expr = constant.get_expr (); TyTy::BaseType *expr_type = nullptr; @@ -91,7 +93,8 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); Resolver::CanonicalPath canonical_path - = nr_ctx.to_canonical_path (func.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (func.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); // FIXME: How do we get the proper visibility here? auto vis = HIR::Visibility (HIR::Visibility::VisType::Public); diff --git a/gcc/rust/backend/rust-compile-item.cc b/gcc/rust/backend/rust-compile-item.cc index 788373fb9148..37b0dd75da48 100644 --- a/gcc/rust/backend/rust-compile-item.cc +++ b/gcc/rust/backend/rust-compile-item.cc @@ -19,6 +19,7 @@ #include "rust-compile-item.h" #include "rust-compile-implitem.h" #include "rust-compile-extern.h" +#include "rust-rib.h" #include "rust-substitution-mapper.h" #include "rust-type-util.h" #include "rust-finalized-name-resolution-context.h" @@ -53,7 +54,8 @@ CompileItem::visit (HIR::StaticItem &var) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); Resolver::CanonicalPath canonical_path - = nr_ctx.to_canonical_path (var.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (var.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); ctx->push_const_context (); tree value @@ -106,7 +108,8 @@ CompileItem::visit (HIR::ConstantItem &constant) // canonical path Resolver::CanonicalPath canonical_path - = nr_ctx.to_canonical_path (mappings.get_nodeid ()); + = nr_ctx.to_canonical_path (mappings.get_nodeid (), + Resolver2_0::Namespace::Values); if (constant_type->is<const TyTy::FnType> ()) { if (concrete == nullptr) @@ -201,7 +204,8 @@ CompileItem::visit (HIR::Function &function) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); Resolver::CanonicalPath canonical_path - = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); const std::string asm_name = ctx->mangle_item (fntype, canonical_path); diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index 747ab98fa8ec..2cfdca5de7ca 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -27,6 +27,7 @@ #include "rust-hir-path-probe.h" #include "rust-compile-extern.h" #include "rust-constexpr.h" +#include "rust-rib.h" #include "rust-tyty.h" namespace Rust { @@ -233,7 +234,9 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment, // in that case the caller should attempt ResolvePathType::Compile auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); - auto resolved = nr_ctx.lookup (mappings.get_nodeid ()); + // TODO: Is Values the correct NS here? + auto resolved + = nr_ctx.lookup (mappings.get_nodeid (), Resolver2_0::Namespace::Values); if (!resolved) return attempt_constructor_expression_lookup (lookup, ctx, mappings, diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h index 0f68c1b7300b..85f6c6300cdf 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h @@ -28,6 +28,7 @@ #include "rust-bir-free-region.h" #include "rust-finalized-name-resolution-context.h" #include "options.h" +#include "rust-rib.h" namespace Rust { @@ -402,14 +403,16 @@ protected: // HIR resolution helpers template <typename T> NodeId resolve_label (T &expr) { - auto res = ctx.resolver.lookup (expr.get_mappings ().get_nodeid ()); + auto res = ctx.resolver.lookup (expr.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Labels); rust_assert (res.has_value ()); return res.value (); } template <typename T> PlaceId resolve_variable (T &variable) { - auto res = ctx.resolver.lookup (variable.get_mappings ().get_nodeid ()); + auto res = ctx.resolver.lookup (variable.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); rust_assert (res.has_value ()); return ctx.place_db.lookup_variable (res.value ()); } @@ -424,7 +427,8 @@ protected: // HIR resolution helpers if (ty->is<TyTy::FnType> ()) return ctx.place_db.get_constant (ty); - auto res = ctx.resolver.lookup (variable.get_mappings ().get_nodeid ()); + auto res = ctx.resolver.lookup (variable.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); rust_assert (res.has_value ()); return ctx.place_db.lookup_or_add_variable (res.value (), ty); } diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index c102bf6e9983..5d3c50ae36c7 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -17,6 +17,7 @@ // <http://www.gnu.org/licenses/>. #include "rust-privacy-reporter.h" +#include "rust-rib.h" #include "rust-session-manager.h" #include "rust-hir-expr.h" #include "rust-hir-stmt.h" @@ -94,13 +95,14 @@ PrivacyReporter::go (HIR::Crate &crate) // FIXME: This function needs a lot of refactoring void PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, - const location_t locus) + const location_t locus, + Resolver2_0::Namespace ns) { NodeId ref_node_id; // FIXME: Assert here. For now, we return since this causes issues when // checking inferred types (#1260) - if (auto id = resolver.lookup (use_id)) + if (auto id = resolver.lookup (use_id, ns)) ref_node_id = *id; else return; @@ -182,7 +184,8 @@ PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings, { auto ref_id = ty->get_ref (); if (auto lookup_id = mappings.lookup_hir_to_node (ref_id)) - return check_for_privacy_violation (*lookup_id, locus); + return check_for_privacy_violation (*lookup_id, locus, + Resolver2_0::Namespace::Types); } break; @@ -260,7 +263,8 @@ void PrivacyReporter::visit (HIR::PathInExpression &path) { check_for_privacy_violation (path.get_mappings ().get_nodeid (), - path.get_locus ()); + path.get_locus (), + Resolver2_0::Namespace::Values); } void @@ -287,21 +291,24 @@ void PrivacyReporter::visit (HIR::TypePath &path) { check_for_privacy_violation (path.get_mappings ().get_nodeid (), - path.get_locus ()); + path.get_locus (), + Resolver2_0::Namespace::Types); } void PrivacyReporter::visit (HIR::QualifiedPathInExpression &path) { - check_for_privacy_violation (path.get_mappings ().get_nodeid (), - path.get_locus ()); + check_for_privacy_violation ( + path.get_mappings ().get_nodeid (), path.get_locus (), + Resolver2_0::Namespace::Types /* FIXME: Is that correct? */); } void PrivacyReporter::visit (HIR::QualifiedPathInType &path) { check_for_privacy_violation (path.get_mappings ().get_nodeid (), - path.get_locus ()); + path.get_locus (), + Resolver2_0::Namespace::Types); } void diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h index 3d827dfc3465..c735aa44a158 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h @@ -25,6 +25,7 @@ #include "rust-hir-type-check.h" #include "rust-mapping-common.h" #include "rust-finalized-name-resolution-context.h" +#include "rust-rib.h" namespace Rust { namespace Privacy { @@ -58,7 +59,8 @@ private: * @param locus Location of said expression/statement */ void check_for_privacy_violation (const NodeId &use_id, - const location_t locus); + const location_t locus, + Resolver2_0::Namespace ns); /** * Internal function used by `check_type_privacy` when dealing with complex diff --git a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc index 9240a684761e..c24f0657e58e 100644 --- a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc +++ b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc @@ -21,6 +21,7 @@ #include "rust-hir.h" #include "rust-hir-item.h" #include "rust-finalized-name-resolution-context.h" +#include "rust-rib.h" namespace Rust { namespace Privacy { @@ -63,7 +64,7 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction, "cannot use non-module path as privacy restrictor"); NodeId ref_node_id; - if (auto id = resolver.lookup (ast_node_id)) + if (auto id = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Types)) { ref_node_id = *id; } diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index 7796b71f9c57..39dd7095695d 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -21,6 +21,7 @@ #include "rust-hir-expr.h" #include "rust-hir-stmt.h" #include "rust-hir-item.h" +#include "rust-rib.h" #include "rust-system.h" #include "rust-finalized-name-resolution-context.h" @@ -355,7 +356,7 @@ ConstChecker::visit (CallExpr &expr) NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; - if (auto id = resolver.lookup (ast_node_id)) + if (auto id = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Values)) ref_node_id = *id; else return; diff --git a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc index a9a5b6422fa5..460ded1b258d 100644 --- a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc +++ b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // <http://www.gnu.org/licenses/>. +#include "rust-rib.h" #include "rust-system.h" #include "rust-hir-pattern-analysis.h" #include "rust-diagnostics.h" @@ -235,7 +236,7 @@ PatternChecker::visit (CallExpr &expr) NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; - if (auto id = resolver.lookup (ast_node_id)) + if (auto id = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Values)) ref_node_id = *id; else return; diff --git a/gcc/rust/checks/errors/rust-readonly-check.cc b/gcc/rust/checks/errors/rust-readonly-check.cc index 734c766b5179..b7e75a7d5012 100644 --- a/gcc/rust/checks/errors/rust-readonly-check.cc +++ b/gcc/rust/checks/errors/rust-readonly-check.cc @@ -23,6 +23,7 @@ #include "rust-hir-map.h" #include "rust-hir-pattern.h" #include "rust-mapping-common.h" +#include "rust-rib.h" #include "rust-system.h" #include "rust-finalized-name-resolution-context.h" #include "rust-tyty.h" @@ -64,7 +65,7 @@ ReadonlyChecker::visit (PathInExpression &expr) NodeId def_id; auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); - if (auto id = nr_ctx.lookup (ast_node_id)) + if (auto id = nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Values)) def_id = *id; else return; diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 848505632b07..da4530ab2587 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -22,6 +22,7 @@ #include "rust-hir-stmt.h" #include "rust-hir-item.h" #include "rust-attribute-values.h" +#include "rust-rib.h" #include "rust-system.h" #include "rust-finalized-name-resolution-context.h" #include "rust-intrinsic-values.h" @@ -220,7 +221,8 @@ UnsafeChecker::visit (PathInExpression &path) NodeId ast_node_id = path.get_mappings ().get_nodeid (); NodeId ref_node_id; - if (auto resolved = resolver.lookup (ast_node_id)) + if (auto resolved + = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Values)) ref_node_id = resolved.value (); else return; @@ -421,7 +423,8 @@ UnsafeChecker::visit (CallExpr &expr) NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; - if (auto resolved = resolver.lookup (ast_node_id)) + if (auto resolved + = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Values)) ref_node_id = resolved.value (); else return; diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc b/gcc/rust/checks/lints/rust-lint-marklive.cc index 0d76e5f8773f..1ef35f328cbd 100644 --- a/gcc/rust/checks/lints/rust-lint-marklive.cc +++ b/gcc/rust/checks/lints/rust-lint-marklive.cc @@ -26,6 +26,7 @@ #include "rust-hir-path.h" #include "rust-name-resolver.h" #include "rust-finalized-name-resolution-context.h" +#include "rust-rib.h" #include "rust-system.h" namespace Rust { @@ -115,7 +116,7 @@ MarkLive::visit (HIR::PathInExpression &expr) ref_node_id = Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ()); else - find_ref_node_id (ast_node_id, ref_node_id); + find_value_definition (ast_node_id, ref_node_id); // node back to HIR tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id); @@ -140,7 +141,7 @@ MarkLive::visit (HIR::MethodCallExpr &expr) // Trying to find the method definition and mark it alive. NodeId ast_node_id = expr.get_mappings ().get_nodeid (); NodeId ref_node_id = UNKNOWN_NODEID; - find_ref_node_id (ast_node_id, ref_node_id); + find_value_definition (ast_node_id, ref_node_id); // node back to HIR if (auto hid = mappings.lookup_node_to_hir (ref_node_id)) @@ -163,7 +164,8 @@ MarkLive::visit_path_segment (HIR::PathExprSegment seg) // // We should mark them alive all and ignoring other kind of segments. // If the segment we dont care then just return false is fine - if (auto id = resolver.lookup (ast_node_id)) + // TODO: Should we look that up in all namespaces? + if (auto id = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Values)) ref_node_id = *id; else return false; @@ -242,7 +244,8 @@ MarkLive::visit (HIR::TypeAlias &alias) NodeId ast_node_id; if (auto id = resolver.lookup ( - alias.get_type_aliased ().get_mappings ().get_nodeid ())) + alias.get_type_aliased ().get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types)) ast_node_id = *id; else rust_unreachable (); @@ -264,9 +267,9 @@ MarkLive::mark_hir_id (HirId id) } void -MarkLive::find_ref_node_id (NodeId ast_node_id, NodeId &ref_node_id) +MarkLive::find_value_definition (NodeId ast_node_id, NodeId &ref_node_id) { - auto resolved = resolver.lookup (ast_node_id); + auto resolved = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Values); rust_assert (resolved.has_value ()); ref_node_id = resolved.value (); } diff --git a/gcc/rust/checks/lints/rust-lint-marklive.h b/gcc/rust/checks/lints/rust-lint-marklive.h index c651d2876858..562104f671e3 100644 --- a/gcc/rust/checks/lints/rust-lint-marklive.h +++ b/gcc/rust/checks/lints/rust-lint-marklive.h @@ -287,7 +287,7 @@ private: void mark_hir_id (HirId); bool visit_path_segment (HIR::PathExprSegment); - void find_ref_node_id (NodeId ast_node_id, NodeId &ref_node_id); + void find_value_definition (NodeId ast_node_id, NodeId &ref_node_id); }; } // namespace Analysis diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc index 542908c63f16..06bbfd446a24 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc @@ -23,6 +23,7 @@ #include "options.h" #include "rust-keyword-values.h" +#include "rust-rib.h" namespace Rust { namespace Analysis { @@ -112,7 +113,8 @@ UnusedChecker::visit (HIR::AssignmentExpr &expr) const auto &lhs = expr.get_lhs (); auto var_name = lhs.to_string (); NodeId ast_node_id = lhs.get_mappings ().get_nodeid (); - NodeId def_id = nr_context.lookup (ast_node_id).value (); + NodeId def_id + = nr_context.lookup (ast_node_id, Resolver2_0::Namespace::Values).value (); HirId id = mappings.lookup_node_to_hir (def_id).value (); if (unused_context.is_variable_assigned (id, lhs.get_mappings ().get_hirid ()) && var_name[0] != '_') diff --git a/gcc/rust/checks/lints/unused/rust-unused-collector.cc b/gcc/rust/checks/lints/unused/rust-unused-collector.cc index 542bab0a6f00..5596dd97a90b 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-collector.cc +++ b/gcc/rust/checks/lints/unused/rust-unused-collector.cc @@ -23,6 +23,7 @@ #include "rust-hir-path.h" #include "rust-hir-pattern.h" #include "rust-finalized-name-resolution-context.h" +#include "rust-rib.h" namespace Rust { namespace Analysis { @@ -61,7 +62,7 @@ UnusedCollector::visit (HIR::StructExprFieldIdentifier &ident) void UnusedCollector::visit (HIR::AssignmentExpr &expr) { - auto def_id = get_def_id (expr.get_lhs ()); + auto def_id = get_def_id (expr.get_lhs (), Resolver2_0::Namespace::Values); HirId id = expr.get_lhs ().get_mappings ().get_hirid (); unused_context.remove_mut (def_id); unused_context.add_assign (def_id, id); diff --git a/gcc/rust/checks/lints/unused/rust-unused-collector.h b/gcc/rust/checks/lints/unused/rust-unused-collector.h index 581a9fcbd46d..5059f76892d2 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-collector.h +++ b/gcc/rust/checks/lints/unused/rust-unused-collector.h @@ -22,6 +22,7 @@ #include "rust-hir-visitor.h" #include "rust-mapping-common.h" #include "rust-finalized-name-resolution-context.h" +#include "rust-rib.h" #include "rust-unused-context.h" namespace Rust { @@ -55,24 +56,25 @@ private: virtual void visit (HIR::BreakExpr &expr) override; virtual void visit (HIR::ContinueExpr &expr) override; - template <typename T> HirId get_def_id (T &path_expr) + template <typename T> + HirId get_def_id (T &path_expr, Resolver2_0::Namespace ns) { NodeId ast_node_id = path_expr.get_mappings ().get_nodeid (); - NodeId id = nr_context.lookup (ast_node_id).value (); + NodeId id = nr_context.lookup (ast_node_id, ns).value (); HirId def_id = mappings.lookup_node_to_hir (id).value (); return def_id; } template <typename T> void mark_path_used (T &path_expr) { - auto def_id = get_def_id (path_expr); + auto def_id = get_def_id (path_expr, Resolver2_0::Namespace::Values); unused_context.add_variable (def_id); unused_context.remove_assign (def_id); } template <typename T> void mark_label_used (T &path_expr) { - auto def_id = get_def_id (path_expr); + auto def_id = get_def_id (path_expr, Resolver2_0::Namespace::Labels); unused_context.add_label (def_id); } }; diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc index 8ba452d66166..de68bf52e4db 100644 --- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc @@ -23,6 +23,8 @@ #include "rust-diagnostics.h" #include "rust-hir-map.h" #include "rust-item.h" +#include "rust-name-resolution-context.h" +#include "rust-rib.h" #include "rust-toplevel-name-resolver-2.0.h" #include "rust-attributes.h" #include "rust-finalize-imports-2.0.h" @@ -91,7 +93,7 @@ Early::resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&glob) return false; auto result = Analysis::Mappings::get ().lookup_glob_container ( - resolved->get_node_id ()); + resolved->definition.get_node_id ()); if (!result) return false; @@ -99,10 +101,11 @@ Early::resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&glob) // here, we insert the module's NodeId into the import_mappings and will look // up the module proper in `FinalizeImports` // The namespace does not matter here since we are dealing with a glob + // FIXME: Does the namespace not matter? Is that valid? // TODO: Ugly import_mappings.insert (use_dec_id, ImportPair (std::move (glob), - ImportData::Glob (*resolved))); + ImportData::Glob (resolved->definition))); return true; } @@ -160,7 +163,7 @@ Early::resolve_rebind_import (NodeId use_dec_id, break; } - if (ctx.lookup (import_id)) + if (ctx.lookup (import_id, Namespace::Types)) return true; auto definitions = resolve_path_in_all_ns (rebind_import.to_resolve); @@ -170,7 +173,7 @@ Early::resolve_rebind_import (NodeId use_dec_id, return false; for (const auto &def : definitions) { - if (def.first.is_ambiguous ()) + if (def.definition.is_ambiguous ()) { rich_location rich_locus (line_table, rebind_import.to_resolve.get_locus ()); @@ -330,20 +333,23 @@ Early::visit (AST::MacroInvocation &invoc) // https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope - tl::optional<Rib::Definition> definition = tl::nullopt; + tl::optional<NameResolutionContext::NamespacedDefinition> ns_def + = tl::nullopt; if (path.get_segments ().size () == 1) - definition - = textual_scope.get (path.get_final_segment ().as_string ()) - .map ([] (NodeId id) { return Rib::Definition::NonShadowable (id); }); + ns_def = textual_scope.get (path.get_final_segment ().as_string ()) + .map ([] (NodeId id) { + return NameResolutionContext::NamespacedDefinition ( + Rib::Definition::NonShadowable (id), Namespace::Macros); + }); // we won't have changed `definition` from `nullopt` if there are more // than one segments in our path - if (!definition.has_value ()) - definition = ctx.resolve_path (path, Namespace::Macros); + if (!ns_def.has_value ()) + ns_def = ctx.resolve_path (path, Namespace::Macros); // if the definition still does not have a value, then it's an error - unless // we should automatically resolve offset_of!() calls - if (!definition.has_value ()) + if (!ns_def.has_value ()) { if (!resolve_offset_of) collect_error (Error (invoc.get_locus (), ErrorCode::E0433, @@ -352,12 +358,13 @@ Early::visit (AST::MacroInvocation &invoc) return; } - try_insert_once (invoc, definition->get_node_id ()); + try_insert_once (invoc, ns_def->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? auto &mappings = Analysis::Mappings::get (); - auto rules_def = mappings.lookup_macro_def (definition->get_node_id ()); + auto rules_def + = mappings.lookup_macro_def (ns_def->definition.get_node_id ()); // Macro definition not found, maybe it is not expanded yet. if (!rules_def) @@ -376,8 +383,8 @@ Early::visit_derive_attribute (AST::Attribute &attr, auto traits = attr.get_traits_to_derive (); for (auto &trait : traits) { - auto definition = ctx.resolve_path (trait.get (), Namespace::Macros); - if (!definition.has_value ()) + auto ns_def = ctx.resolve_path (trait.get (), Namespace::Macros); + if (!ns_def.has_value ()) { // FIXME: Change to proper error message collect_error (Error (trait.get ().get_locus (), @@ -386,8 +393,8 @@ Early::visit_derive_attribute (AST::Attribute &attr, continue; } - auto pm_def - = mappings.lookup_derive_proc_macro_def (definition->get_node_id ()); + auto pm_def = mappings.lookup_derive_proc_macro_def ( + ns_def->definition.get_node_id ()); if (pm_def.has_value ()) mappings.insert_derive_proc_macro_invocation (trait, pm_def.value ()); @@ -399,8 +406,8 @@ Early::visit_non_builtin_attribute (AST::Attribute &attr, Analysis::Mappings &mappings, std::string &name) { - auto definition = ctx.resolve_path (attr.get_path (), Namespace::Macros); - if (!definition.has_value ()) + auto ns_def = ctx.resolve_path (attr.get_path (), Namespace::Macros); + if (!ns_def.has_value ()) { // FIXME: Change to proper error message collect_error (Error (attr.get_locus (), @@ -408,8 +415,8 @@ Early::visit_non_builtin_attribute (AST::Attribute &attr, name.c_str ())); return; } - auto pm_def - = mappings.lookup_attribute_proc_macro_def (definition->get_node_id ()); + auto pm_def = mappings.lookup_attribute_proc_macro_def ( + ns_def->definition.get_node_id ()); if (!pm_def.has_value ()) return; @@ -457,11 +464,11 @@ Early::finalize_simple_import (const Early::ImportPair &mapping) // dirty = true; ctx.map_usage (Usage (import_id), - Definition (definition.first.get_node_id ())); + Definition (definition.definition.get_node_id ()), + definition.ns); - 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 */); + toplevel.insert_or_error_out (identifier, import.get_locus (), import_id, + definition.ns); dirty = dirty || toplevel.is_dirty (); } @@ -538,11 +545,11 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping) // dirty = true; ctx.map_usage (Usage (import_id), - Definition (definition.first.get_node_id ())); + Definition (definition.definition.get_node_id ()), + definition.ns); - toplevel - .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 */); + toplevel.insert_or_error_out (declared_name, path.get_locus (), import_id, + definition.ns); dirty = dirty || toplevel.is_dirty (); @@ -553,8 +560,8 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping) // pub use Foo; // use self::Foo::*; auto &mappings = Analysis::Mappings::get (); - if (auto container - = mappings.lookup_glob_container (definition.first.get_node_id ())) + if (auto container = mappings.lookup_glob_container ( + definition.definition.get_node_id ())) mappings.insert_glob_container (import_id, container.value ()); } } diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.h b/gcc/rust/resolve/rust-early-name-resolver-2.0.h index ff3542ce5468..7b1e377220d7 100644 --- a/gcc/rust/resolve/rust-early-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.h @@ -79,14 +79,14 @@ public: Rebind } kind; - static ImportData - Simple (std::vector<std::pair<Rib::Definition, Namespace>> &&definitions) + static ImportData Simple ( + std::vector<NameResolutionContext::NamespacedDefinition> &&definitions) { return ImportData (Kind::Simple, std::move (definitions)); } - static ImportData - Rebind (std::vector<std::pair<Rib::Definition, Namespace>> &&definitions) + static ImportData Rebind ( + std::vector<NameResolutionContext::NamespacedDefinition> &&definitions) { return ImportData (Kind::Rebind, std::move (definitions)); } @@ -102,7 +102,8 @@ public: return glob_container; } - std::vector<std::pair<Rib::Definition, Namespace>> definitions () const + std::vector<NameResolutionContext::NamespacedDefinition> + definitions () const { rust_assert (kind != Kind::Glob); return std::move (resolved_definitions); @@ -111,7 +112,7 @@ public: private: ImportData ( Kind kind, - std::vector<std::pair<Rib::Definition, Namespace>> &&definitions) + std::vector<NameResolutionContext::NamespacedDefinition> &&definitions) : kind (kind), resolved_definitions (std::move (definitions)) {} @@ -122,7 +123,8 @@ public: // TODO: Should this be a union? // For Simple and Rebind - std::vector<std::pair<Rib::Definition, Namespace>> resolved_definitions; + std::vector<NameResolutionContext::NamespacedDefinition> + resolved_definitions; // For Glob Rib::Definition glob_container; @@ -221,29 +223,18 @@ private: bool resolve_rebind_import (NodeId use_dec_id, TopLevel::ImportKind &&import); template <typename P> - std::vector<std::pair<Rib::Definition, Namespace>> + std::vector<NameResolutionContext::NamespacedDefinition> resolve_path_in_all_ns (const P &path) { - std::vector<std::pair<Rib::Definition, Namespace>> resolved; - - // Pair a definition with the namespace it was found in - auto pair_with_ns = [&] (Namespace ns) { - return [&, ns] (Rib::Definition def) { - auto pair = std::make_pair (def, ns); - return resolved.emplace_back (std::move (pair)); - }; - }; + std::vector<NameResolutionContext::NamespacedDefinition> resolved; std::vector<Error> value_errors; std::vector<Error> type_errors; std::vector<Error> macro_errors; - ctx.resolve_path (path, value_errors, Namespace::Values) - .map (pair_with_ns (Namespace::Values)); - ctx.resolve_path (path, type_errors, Namespace::Types) - .map (pair_with_ns (Namespace::Types)); - ctx.resolve_path (path, macro_errors, Namespace::Macros) - .map (pair_with_ns (Namespace::Macros)); + ctx.resolve_path (path, value_errors, Namespace::Values); + ctx.resolve_path (path, type_errors, Namespace::Types); + ctx.resolve_path (path, macro_errors, Namespace::Macros); if (!value_errors.empty () && !type_errors.empty () && !macro_errors.empty ()) diff --git a/gcc/rust/resolve/rust-finalized-name-resolution-context.cc b/gcc/rust/resolve/rust-finalized-name-resolution-context.cc index ea60f95fb991..6df72bd5187c 100644 --- a/gcc/rust/resolve/rust-finalized-name-resolution-context.cc +++ b/gcc/rust/resolve/rust-finalized-name-resolution-context.cc @@ -42,24 +42,26 @@ FinalizedNameResolutionContext::get () } void -FinalizedNameResolutionContext::map_usage (Usage usage, Definition definition) +FinalizedNameResolutionContext::map_usage (Usage usage, Definition definition, + Namespace ns) { auto leaf_definition = ctx.find_leaf_definition (definition.id).value_or (definition); - ctx.map_usage (usage, leaf_definition); + ctx.map_usage (usage, leaf_definition, ns); } tl::optional<NodeId> -FinalizedNameResolutionContext::lookup (NodeId usage) const +FinalizedNameResolutionContext::lookup (NodeId usage, Namespace ns) const { - return ctx.lookup (usage); + return ctx.lookup (usage, ns); } Resolver::CanonicalPath -FinalizedNameResolutionContext::to_canonical_path (NodeId id) const +FinalizedNameResolutionContext::to_canonical_path (NodeId id, + Namespace ns) const { - return ctx.canonical_ctx.get_path (id); + return ctx.canonical_ctx.get_path (id, ns); } FinalizedNameResolutionContext::FinalizedNameResolutionContext ( diff --git a/gcc/rust/resolve/rust-finalized-name-resolution-context.h b/gcc/rust/resolve/rust-finalized-name-resolution-context.h index a896423bf523..5834a70c5f23 100644 --- a/gcc/rust/resolve/rust-finalized-name-resolution-context.h +++ b/gcc/rust/resolve/rust-finalized-name-resolution-context.h @@ -20,6 +20,7 @@ #define RUST_FINALIZED_NRCTX_H #include "rust-name-resolution-context.h" +#include "rust-rib.h" namespace Rust { namespace Resolver2_0 { @@ -45,17 +46,17 @@ public: * resolution stage, every import chain has been resolved, and can be followed * to an actual definition instead of an import definition */ - void map_usage (Usage usage, Definition definition); + void map_usage (Usage usage, Definition definition, Namespace ns); /** * Same as NameResolutionContext::lookup */ - tl::optional<NodeId> lookup (NodeId usage) const; + tl::optional<NodeId> lookup (NodeId usage, Namespace ns) const; /** * Same as NameResolutionContext::to_canonical_path */ - Resolver::CanonicalPath to_canonical_path (NodeId id) const; + Resolver::CanonicalPath to_canonical_path (NodeId id, Namespace ns) const; /** * Avoid using these and prefer adding wrapper methods to this class instead. @@ -75,6 +76,7 @@ private: }; } // namespace Resolver2_0 + } // namespace Rust #endif //! RUST_FINALIZED_NRCTX_H diff --git a/gcc/rust/resolve/rust-forever-stack.h b/gcc/rust/resolve/rust-forever-stack.h index c28828fda35e..47e769abe758 100644 --- a/gcc/rust/resolve/rust-forever-stack.h +++ b/gcc/rust/resolve/rust-forever-stack.h @@ -906,6 +906,27 @@ public: return (definition_rib && definition_rib.value ().kind == Rib::Kind::ForwardTypeParamBan); } + + void map_usage (Usage usage, Definition definition) + { + auto inserted = resolved_nodes.emplace (usage, definition).second; + + // is that valid? + rust_assert (inserted); + } + + tl::optional<NodeId> lookup (NodeId usage) const + { + auto it = resolved_nodes.find (Usage (usage)); + + if (it == resolved_nodes.end ()) + return tl::nullopt; + + return it->second.id; + } + + /* Map of "usage" nodes which have been resolved to a "definition" node */ + std::map<Usage, Definition> resolved_nodes; }; } // namespace Resolver2_0 diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc index f0db8277598d..20d8bc8c28fd 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc @@ -26,6 +26,7 @@ #include "rust-name-resolution-context.h" #include "rust-resolve-builtins.h" #include "rust-path.h" +#include "rust-rib.h" #include "rust-system.h" #include "rust-tyty.h" #include "rust-hir-type-check.h" @@ -189,7 +190,8 @@ visit_identifier_as_pattern (NameResolutionContext &ctx, { auto res = ctx.values.get (ident); rust_assert (res.has_value () && !res->is_ambiguous ()); - ctx.map_usage (Usage (node_id), Definition (res->get_node_id ())); + ctx.map_usage (Usage (node_id), Definition (res->get_node_id ()), + Namespace::Values); } else { @@ -244,7 +246,8 @@ Late::visit (AST::StructPatternFieldIdent &field) if (auto resolved = ctx.resolve_path (path, Namespace::Types)) { ctx.map_usage (Usage (field.get_node_id ()), - Definition (resolved->get_node_id ())); + Definition (resolved->definition.get_node_id ()), + Namespace::Types); return; } @@ -299,7 +302,8 @@ Late::visit (AST::BreakExpr &expr) void Late::visit (AST::LoopLabel &label) { - auto resolved = ctx.lookup (label.get_lifetime ().get_node_id ()); + auto resolved + = ctx.lookup (label.get_lifetime ().get_node_id (), Namespace::Labels); if (resolved.has_value ()) return; auto &lifetime = label.get_lifetime (); @@ -314,7 +318,8 @@ Late::resolve_label (AST::Lifetime &lifetime) { if (resolved->get_node_id () != lifetime.get_node_id ()) ctx.map_usage (Usage (lifetime.get_node_id ()), - Definition (resolved->get_node_id ())); + Definition (resolved->get_node_id ()), + Namespace::Labels); } else rust_error_at (lifetime.get_locus (), ErrorCode::E0426, @@ -337,14 +342,17 @@ Late::visit (AST::IdentifierExpr &expr) // TODO: same thing as visit(PathInExpression) here? tl::optional<Rib::Definition> resolved = tl::nullopt; + tl::optional<Namespace> ns = tl::nullopt; if (auto value = ctx.values.get (expr.get_ident ())) { resolved = value; + ns = Namespace::Values; } else if (auto type = ctx.types.get (expr.get_ident ())) { resolved = type; + ns = Namespace::Types; } else if (funny_error) { @@ -359,15 +367,18 @@ Late::visit (AST::IdentifierExpr &expr) if (auto type = ctx.types.get_lang_prelude (expr.get_ident ())) { resolved = type; + ns = Namespace::Types; } else if (!resolved && ctx.prelude) { resolved = ctx.values.get_from_prelude (*ctx.prelude, expr.get_ident ()); + ns = Namespace::Values; if (!resolved) resolved = ctx.types.get_from_prelude (*ctx.prelude, expr.get_ident ()); + ns = Namespace::Types; } if (!resolved) @@ -387,7 +398,7 @@ Late::visit (AST::IdentifierExpr &expr) } ctx.map_usage (Usage (expr.get_node_id ()), - Definition (resolved->get_node_id ())); + Definition (resolved->get_node_id ()), ns.value ()); // For empty types, do we perform a lookup in ctx.types or should the // toplevel instead insert a name in ctx.values? (like it currently does) @@ -418,7 +429,7 @@ Late::visit (AST::StructExprFieldIdentifier &expr) } ctx.map_usage (Usage (expr.get_node_id ()), - Definition (resolved->get_node_id ())); + Definition (resolved->get_node_id ()), Namespace::Values); } void @@ -430,26 +441,30 @@ Late::visit (AST::PathInExpression &expr) DefaultResolver::visit (expr); + // TODO: do we need a namespace associated with each lang item? if (expr.is_lang_item ()) { ctx.map_usage (Usage (expr.get_node_id ()), Definition (Analysis::Mappings::get ().get_lang_item_node ( - expr.get_lang_item ()))); + expr.get_lang_item ())), + Namespace::Values); return; } + // TODO: we need to know which namespace that was in actually auto resolved = ctx.resolve_path (expr, Namespace::Values, Namespace::Types); if (!resolved) { - if (!ctx.lookup (expr.get_segments ().front ().get_node_id ())) + if (!ctx.lookup (expr.get_segments ().front ().get_node_id (), + Namespace::Values, Namespace::Types)) rust_error_at (expr.get_locus (), ErrorCode::E0433, "Cannot find path %qs in this scope", expr.as_simple_path ().as_string ().c_str ()); return; } - if (resolved->is_ambiguous ()) + if (resolved->definition.is_ambiguous ()) { rust_error_at (expr.get_locus (), ErrorCode::E0659, "%qs is ambiguous", expr.as_string ().c_str ()); @@ -457,7 +472,8 @@ Late::visit (AST::PathInExpression &expr) } ctx.map_usage (Usage (expr.get_node_id ()), - Definition (resolved->get_node_id ())); + Definition (resolved->definition.get_node_id ()), + resolved->ns); } void @@ -498,21 +514,22 @@ resolve_type_path_like (NameResolutionContext &ctx, bool block_big_self, if (!resolved.has_value ()) { - if (!ctx.lookup (unwrap_segment_node_id (type.get_segments ().front ()))) + if (!ctx.lookup (unwrap_segment_node_id (type.get_segments ().front ()), + Namespace::Types)) rust_error_at (type.get_locus (), ErrorCode::E0412, "could not resolve type path %qs", unwrap_segment_error_string (type).c_str ()); return; } - if (resolved->is_ambiguous ()) + if (resolved->definition.is_ambiguous ()) { rust_error_at (type.get_locus (), ErrorCode::E0659, "%qs is ambiguous", unwrap_segment_error_string (type).c_str ()); return; } - if (ctx.types.forward_declared (resolved->get_node_id (), + if (ctx.types.forward_declared (resolved->definition.get_node_id (), type.get_node_id ())) { rust_error_at (type.get_locus (), ErrorCode::E0128, @@ -521,7 +538,8 @@ resolve_type_path_like (NameResolutionContext &ctx, bool block_big_self, } ctx.map_usage (Usage (type.get_node_id ()), - Definition (resolved->get_node_id ())); + Definition (resolved->definition.get_node_id ()), + Namespace::Types); } void @@ -578,14 +596,15 @@ Late::visit (AST::Visibility &vis) } // TODO: is this possible? - if (res->is_ambiguous ()) + if (res->definition.is_ambiguous ()) { rust_error_at (path.get_locus (), ErrorCode::E0659, "%qs is ambiguous", path.as_string ().c_str ()); return; } - ctx.map_usage (Usage (path.get_node_id ()), Definition (res->get_node_id ())); + ctx.map_usage (Usage (path.get_node_id ()), + Definition (res->definition.get_node_id ()), res->ns); } void @@ -596,7 +615,7 @@ Late::visit (AST::Trait &trait) // which is then resolved to the node id of trait // we set up the latter mapping here ctx.map_usage (Usage (trait.get_implicit_self ().get_node_id ()), - Definition (trait.get_node_id ())); + Definition (trait.get_node_id ()), Namespace::Types); DefaultResolver::visit (trait); } @@ -646,7 +665,8 @@ Late::visit (AST::StructExprStructFields &s) } ctx.map_usage (Usage (path.get_node_id ()), - Definition (resolved->get_node_id ())); + Definition (resolved->definition.get_node_id ()), + Namespace::Types); } // needed because Late::visit (AST::GenericArg &) is non-virtual diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc b/gcc/rust/resolve/rust-name-resolution-context.cc index 70c74323eaee..a70eccfa8ca9 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.cc +++ b/gcc/rust/resolve/rust-name-resolution-context.cc @@ -19,6 +19,8 @@ #include "rust-name-resolution-context.h" #include "optional.h" #include "rust-mapping-common.h" +#include "rust-rib.h" +#include "rust-system.h" namespace Rust { namespace Resolver2_0 { @@ -130,7 +132,8 @@ BindingLayer::get_source () const } Resolver::CanonicalPath -CanonicalPathRecordCrateRoot::as_path (const NameResolutionContext &) +CanonicalPathRecordCrateRoot::as_path (const NameResolutionContext &, + Namespace ns) { auto ret = Resolver::CanonicalPath::new_seg (node_id, seg); ret.set_crate_num (crate_num); @@ -138,19 +141,23 @@ CanonicalPathRecordCrateRoot::as_path (const NameResolutionContext &) } Resolver::CanonicalPath -CanonicalPathRecordNormal::as_path (const NameResolutionContext &ctx) +CanonicalPathRecordNormal::as_path (const NameResolutionContext &ctx, + Namespace ns) { - auto parent_path = get_parent ().as_path (ctx); + auto parent_path = get_parent ().as_path (ctx, ns); return parent_path.append (Resolver::CanonicalPath::new_seg (node_id, seg)); } Resolver::CanonicalPath -CanonicalPathRecordLookup::as_path (const NameResolutionContext &ctx) +CanonicalPathRecordLookup::as_path (const NameResolutionContext &ctx, + Namespace ns) { if (!cache) { - auto res = ctx.lookup (lookup_id).and_then ( - [&ctx] (NodeId id) { return ctx.canonical_ctx.get_record_opt (id); }); + // TODO: what namespace do we use here? can the caller give one? + auto res = ctx.lookup (lookup_id, ns).and_then ([&ctx] (NodeId id) { + return ctx.canonical_ctx.get_record_opt (id); + }); if (!res) { @@ -163,25 +170,29 @@ CanonicalPathRecordLookup::as_path (const NameResolutionContext &ctx) cache = res.value (); } - return cache->as_path (ctx); + return cache->as_path (ctx, ns); } Resolver::CanonicalPath -CanonicalPathRecordImpl::as_path (const NameResolutionContext &ctx) +CanonicalPathRecordImpl::as_path (const NameResolutionContext &ctx, + Namespace ns) { - auto parent_path = get_parent ().as_path (ctx); + auto parent_path = get_parent ().as_path (ctx, ns); return parent_path.append ( Resolver::CanonicalPath::inherent_impl_seg (impl_id, - type_record.as_path (ctx))); + type_record.as_path (ctx, ns))); } Resolver::CanonicalPath -CanonicalPathRecordTraitImpl::as_path (const NameResolutionContext &ctx) +CanonicalPathRecordTraitImpl::as_path (const NameResolutionContext &ctx, + Namespace ns) { - auto parent_path = get_parent ().as_path (ctx); + // Maybe this doesn't need the namespace and will always be in the types NS? + auto parent_path = get_parent ().as_path (ctx, ns); return parent_path.append ( Resolver::CanonicalPath::trait_impl_projection_seg ( - impl_id, trait_path_record.as_path (ctx), type_record.as_path (ctx))); + impl_id, trait_path_record.as_path (ctx, ns), + type_record.as_path (ctx, ns))); } NameResolutionContext::NameResolutionContext () @@ -253,24 +264,53 @@ NameResolutionContext::insert_globbed (Identifier name, NodeId id, Namespace ns) } } +// TODO: Maybe this should take a NamespacedDefinition as argument? void -NameResolutionContext::map_usage (Usage usage, Definition definition) +NameResolutionContext::map_usage (Usage usage, Definition definition, + Namespace ns) { - auto inserted = resolved_nodes.emplace (usage, definition).second; - - // is that valid? - // rust_assert (inserted); + switch (ns) + { + case Namespace::Values: + values.map_usage (usage, definition); + break; + case Namespace::Types: + types.map_usage (usage, definition); + break; + case Namespace::Labels: + labels.map_usage (usage, definition); + break; + case Namespace::Macros: + macros.map_usage (usage, definition); + break; + } } tl::optional<NodeId> -NameResolutionContext::lookup (NodeId usage) const +NameResolutionContext::lookup (NodeId usage, Namespace ns) const { - auto it = resolved_nodes.find (Usage (usage)); + switch (ns) + { + case Namespace::Values: + return values.lookup (usage); + case Namespace::Types: + return types.lookup (usage); + case Namespace::Labels: + return labels.lookup (usage); + case Namespace::Macros: + return macros.lookup (usage); + default: + rust_unreachable (); + } +} - if (it == resolved_nodes.end ()) - return tl::nullopt; +tl::optional<NodeId> +NameResolutionContext::lookup (NodeId usage, Namespace ns1, Namespace ns2) const +{ + if (auto result = lookup (usage, ns1)) + return result; - return it->second.id; + return lookup (usage, ns2); } void diff --git a/gcc/rust/resolve/rust-name-resolution-context.h b/gcc/rust/resolve/rust-name-resolution-context.h index f574bec51989..e2e2e3ebd290 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.h +++ b/gcc/rust/resolve/rust-name-resolution-context.h @@ -223,7 +223,9 @@ class NameResolutionContext; class CanonicalPathRecord { public: - virtual Resolver::CanonicalPath as_path (const NameResolutionContext &) = 0; + virtual Resolver::CanonicalPath as_path (const NameResolutionContext &, + Namespace ns) + = 0; virtual bool is_root () const = 0; @@ -254,7 +256,8 @@ public: crate_num = Analysis::Mappings::get ().lookup_crate_num (node_id).value (); } - Resolver::CanonicalPath as_path (const NameResolutionContext &) override; + Resolver::CanonicalPath as_path (const NameResolutionContext &, + Namespace ns) override; bool is_root () const override final { return true; } @@ -275,7 +278,8 @@ public: rust_assert (!Analysis::Mappings::get ().node_is_crate (node_id)); } - Resolver::CanonicalPath as_path (const NameResolutionContext &) override; + Resolver::CanonicalPath as_path (const NameResolutionContext &, + Namespace ns) override; private: NodeId node_id; @@ -289,7 +293,8 @@ public: : lookup_id (lookup_id), cache (nullptr) {} - Resolver::CanonicalPath as_path (const NameResolutionContext &) override; + Resolver::CanonicalPath as_path (const NameResolutionContext &, + Namespace ns) override; bool is_root () const override final { return true; } @@ -307,7 +312,8 @@ public: type_record (type_id) {} - Resolver::CanonicalPath as_path (const NameResolutionContext &) override; + Resolver::CanonicalPath as_path (const NameResolutionContext &, + Namespace ns) override; private: NodeId impl_id; @@ -323,7 +329,8 @@ public: type_record (type_id), trait_path_record (trait_path_id) {} - Resolver::CanonicalPath as_path (const NameResolutionContext &) override; + Resolver::CanonicalPath as_path (const NameResolutionContext &, + Namespace ns) override; private: NodeId impl_id; @@ -338,9 +345,9 @@ public: : current_record (nullptr), nr_ctx (&ctx) {} - Resolver::CanonicalPath get_path (NodeId id) const + Resolver::CanonicalPath get_path (NodeId id, Namespace ns) const { - return get_record (id).as_path (*nr_ctx); + return get_record (id).as_path (*nr_ctx, ns); } CanonicalPathRecord &get_record (NodeId id) const @@ -517,46 +524,75 @@ public: CanonicalPathCtx canonical_ctx; - // TODO: Rename - // TODO: Use newtype pattern for Usage and Definition - void map_usage (Usage usage, Definition 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; - tl::optional<NodeId> lookup (NodeId usage) const; - - Resolver::CanonicalPath to_canonical_path (NodeId id) const + Resolver::CanonicalPath to_canonical_path (NodeId id, Namespace ns) const { - return canonical_ctx.get_path (id); + return canonical_ctx.get_path (id, ns); } - tl::optional<Rib::Definition> + /** + * The return value when the namespace in which a definition was resolved + * matters + */ + struct NamespacedDefinition + { + explicit NamespacedDefinition (Rib::Definition definition, Namespace ns) + : definition (definition), ns (ns) + {} + + static tl::optional<NamespacedDefinition> + Maybe (tl::optional<Rib::Definition> definition, Namespace ns) + { + return definition.map ([ns] (Rib::Definition definition) { + return NamespacedDefinition (definition, ns); + }); + } + + Rib::Definition definition; + Namespace ns; + }; + + tl::optional<NamespacedDefinition> resolve_path (const ResolutionPath &path, ResolutionMode mode, std::vector<Error> &collect_errors, Namespace ns) { std::function<void (Usage, Definition)> insert_segment_resolution - = [this] (Usage seg_id, Definition id) { + = [this, ns] (Usage seg_id, Definition id) { if (resolved_nodes.find (seg_id) == resolved_nodes.end ()) - map_usage (seg_id, id); + map_usage (seg_id, id, ns); }; - tl::optional<Rib::Definition> resolved = tl::nullopt; + tl::optional<NamespacedDefinition> resolved = tl::nullopt; switch (ns) { case Namespace::Values: - resolved = resolve_path (values, path, mode, insert_segment_resolution, - collect_errors); + resolved = NamespacedDefinition::Maybe ( + resolve_path (values, path, mode, insert_segment_resolution, + collect_errors), + ns); break; case Namespace::Types: - resolved = resolve_path (types, path, mode, insert_segment_resolution, - collect_errors); + resolved = NamespacedDefinition::Maybe ( + resolve_path (types, path, mode, insert_segment_resolution, + collect_errors), + ns); break; case Namespace::Macros: - resolved = resolve_path (macros, path, mode, insert_segment_resolution, - collect_errors); + resolved = NamespacedDefinition::Maybe ( + resolve_path (macros, path, mode, insert_segment_resolution, + collect_errors), + ns); break; case Namespace::Labels: - resolved = resolve_path (labels, path, mode, insert_segment_resolution, - collect_errors); + resolved = NamespacedDefinition::Maybe ( + resolve_path (labels, path, mode, insert_segment_resolution, + collect_errors), + ns); break; default: rust_unreachable (); @@ -569,17 +605,25 @@ public: switch (ns) { case Namespace::Values: - return resolve_path (values, path, mode, insert_segment_resolution, - collect_errors, *prelude); + return NamespacedDefinition::Maybe ( + resolve_path (values, path, mode, insert_segment_resolution, + collect_errors, *prelude), + ns); case Namespace::Types: - return resolve_path (types, path, mode, insert_segment_resolution, - collect_errors, *prelude); + return NamespacedDefinition::Maybe ( + resolve_path (types, path, mode, insert_segment_resolution, + collect_errors, *prelude), + ns); case Namespace::Macros: - return resolve_path (macros, path, mode, insert_segment_resolution, - collect_errors, *prelude); + return NamespacedDefinition::Maybe ( + resolve_path (macros, path, mode, insert_segment_resolution, + collect_errors, *prelude), + ns); case Namespace::Labels: - return resolve_path (labels, path, mode, insert_segment_resolution, - collect_errors, *prelude); + return NamespacedDefinition::Maybe ( + resolve_path (labels, path, mode, insert_segment_resolution, + collect_errors, *prelude), + ns); default: rust_unreachable (); } @@ -651,7 +695,7 @@ public: this->collect_errors = collect_errors; } - tl::optional<Rib::Definition> resolve () + tl::optional<NamespacedDefinition> resolve () { rust_assert (has_path_set); @@ -693,7 +737,7 @@ public: }; template <typename S, typename... Args> - tl::optional<Rib::Definition> + tl::optional<NamespacedDefinition> resolve_path (const std::vector<S> &path_segments, ResolutionMode mode, tl::optional<std::vector<Error> &> collect_errors, Namespace ns_first, Args... ns_args) @@ -707,7 +751,7 @@ public: } template <typename S, typename... Args> - tl::optional<Rib::Definition> + tl::optional<NamespacedDefinition> resolve_path (const std::vector<S> &path_segments, bool has_opening_scope_resolution, tl::optional<std::vector<Error> &> collect_errors, @@ -723,7 +767,7 @@ public: } template <typename S, typename... Args> - tl::optional<Rib::Definition> + tl::optional<NamespacedDefinition> resolve_path (const std::vector<S> &path_segments, bool has_opening_scope_resolution, Namespace ns_first, Args... ns_args) @@ -737,7 +781,7 @@ public: } template <typename S, typename... Args> - tl::optional<Rib::Definition> + tl::optional<NamespacedDefinition> resolve_path (const std::vector<S> &path_segments, ResolutionMode mode, Namespace ns_first, Args... ns_args) { @@ -749,8 +793,8 @@ public: } template <typename... Args> - tl::optional<Rib::Definition> resolve_path (const AST::SimplePath &path, - Args &&...args) + tl::optional<NamespacedDefinition> resolve_path (const AST::SimplePath &path, + Args &&...args) { return resolve_path (path.get_segments (), path.has_opening_scope_resolution (), @@ -758,16 +802,16 @@ public: } template <typename... Args> - tl::optional<Rib::Definition> resolve_path (const AST::PathInExpression &path, - Args &&...args) + tl::optional<NamespacedDefinition> + resolve_path (const AST::PathInExpression &path, Args &&...args) { return resolve_path (path.get_segments (), path.opening_scope_resolution (), std::forward<Args> (args)...); } template <typename... Args> - tl::optional<Rib::Definition> resolve_path (const AST::TypePath &path, - Args &&...args) + tl::optional<NamespacedDefinition> resolve_path (const AST::TypePath &path, + Args &&...args) { return resolve_path (path.get_segments (), path.has_opening_scope_resolution_op (), @@ -780,8 +824,8 @@ public: // This is an internal compiler error NoDefinition, // There was a loop in the map, such as an import resolving to another - // import which eventually resolved to the original import. Report the error - // and stop the pipeline + // import which eventually resolved to the original import. Report the + // error and stop the pipeline Loop, }; @@ -792,22 +836,23 @@ public: * We've now collected every definition and import, and errored out when * necessary if multiple definitions are colliding. Do a final flattening of * the name resolution context to make it easier to digest for the late name - * resolution and type-checker. This basically turns the `resolved_nodes` map - * from a linked-list-like map to a regular, flat hashmap. + * resolution and type-checker. This basically turns the `resolved_nodes` + * map from a linked-list-like map to a regular, flat hashmap. * - * FIXME: The documentation is wrong, this needs to also run after all usages - * have been *resolved* so after Late as well!!! + * FIXME: The documentation is wrong, this needs to also run after all + * usages have been *resolved* so after Late as well!!! * - * TODO: Should this return something like the FinalizedNameResolutionCtx? Or - * set it up at least? And instead of mutating the `resolved_nodes` map, + * TODO: Should this return something like the FinalizedNameResolutionCtx? + * Or set it up at least? And instead of mutating the `resolved_nodes` map, * create a new one for the FinalizedNameResolutionCtx? * Actually, since Late uses the NRCtx directly we should mutate this. Most - * later passes don't look at this map. So let's go for side-effects in a void - * function, yipee. + * later passes don't look at this map. So let's go for side-effects in a + * void function, yipee. */ void flatten (); - /* If declared with #[prelude_import], the current standard library module */ + /* If declared with #[prelude_import], the current standard library module + */ tl::optional<NodeId> prelude; private: diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index 0d99eb7e9cbf..357fb599755d 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -18,6 +18,7 @@ #include "rust-hir-trait-resolve.h" #include "rust-hir-type-check-expr.h" +#include "rust-rib.h" #include "rust-substitution-mapper.h" #include "rust-type-util.h" #include "rust-finalized-name-resolution-context.h" @@ -123,7 +124,8 @@ TraitResolver::resolve_path_to_trait (const HIR::TypePath &path, auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); NodeId ref; - if (auto ref_opt = nr_ctx.lookup (path.get_mappings ().get_nodeid ())) + if (auto ref_opt = nr_ctx.lookup (path.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types)) { ref = *ref_opt; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc index 8566bcd7d1f4..f1e4c51b46cb 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc @@ -20,6 +20,7 @@ #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-enumitem.h" +#include "rust-rib.h" #include "rust-type-util.h" #include "rust-finalized-name-resolution-context.h" @@ -79,7 +80,8 @@ TypeCheckEnumItem::visit (HIR::EnumItem &item) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath canonical_path - = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, item.get_locus ()}; variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), @@ -110,7 +112,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath canonical_path - = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, item.get_locus ()}; variant @@ -159,7 +162,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemTuple &item) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath canonical_path - = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, item.get_locus ()}; variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), @@ -206,7 +210,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemStruct &item) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath canonical_path - = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, item.get_locus ()}; variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 897057040e1d..07ac5c4eaebe 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -20,6 +20,7 @@ #include "rust-common.h" #include "rust-hir-expr.h" #include "rust-hir-map.h" +#include "rust-rib.h" #include "rust-system.h" #include "rust-tyty-call.h" #include "rust-hir-type-check-struct-field.h" @@ -1539,7 +1540,8 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()), - Resolver2_0::Definition (resolved_node_id)); + Resolver2_0::Definition (resolved_node_id), + Resolver2_0::Namespace::Values); // return the result of the function back infered = function_ret_tyty; @@ -2182,7 +2184,8 @@ TypeCheckExpr::resolve_operator_overload ( auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()), - Resolver2_0::Definition (resolved_node_id)); + Resolver2_0::Definition (resolved_node_id), + Resolver2_0::Namespace::Types); // return the result of the function back infered = function_ret_tyty; @@ -2376,12 +2379,17 @@ TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr, // set up the resolved name on the path auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); - auto existing = nr_ctx.lookup (expr.get_mappings ().get_nodeid ()); + // TODO: What namespace to use for inserting and looking up here? It's a trait + // call, so NS::Types is right? + + auto existing = nr_ctx.lookup (expr.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); if (existing) rust_assert (*existing == resolved_node_id); else nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()), - Resolver2_0::Definition (resolved_node_id)); + Resolver2_0::Definition (resolved_node_id), + Resolver2_0::Namespace::Types); // return the result of the function back *result = function_ret_tyty; diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index 85ccd60a4414..8e07cc5f8fce 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -24,6 +24,7 @@ #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-pattern.h" +#include "rust-rib.h" #include "rust-type-util.h" #include "rust-tyty.h" #include "rust-finalized-name-resolution-context.h" @@ -342,7 +343,8 @@ TypeCheckImplItem::visit (HIR::Function &function) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath canonical_path - = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, function.get_locus ()}; auto fnType = new TyTy::FnType ( @@ -400,8 +402,10 @@ TypeCheckImplItem::visit (HIR::ConstantItem &constant) // special case when this is a generic constant auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); + // TODO: Is Values the correct NS? CanonicalPath canonical_path - = nr_ctx.to_canonical_path (constant.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (constant.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); RustIdent ident{canonical_path, constant.get_locus ()}; auto fnType = new TyTy::FnType ( constant.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index db6682fd35b4..3e4e2365821c 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -28,6 +28,7 @@ #include "rust-hir-type-check-pattern.h" #include "rust-hir-trait-resolve.h" #include "rust-identifier.h" +#include "rust-rib.h" #include "rust-session-manager.h" #include "rust-finalized-name-resolution-context.h" #include "rust-substitution-mapper.h" @@ -208,7 +209,8 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath path - = nr_ctx.to_canonical_path (struct_decl.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (struct_decl.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{path, struct_decl.get_locus ()}; @@ -276,7 +278,8 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath path - = nr_ctx.to_canonical_path (struct_decl.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (struct_decl.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{path, struct_decl.get_locus ()}; @@ -354,7 +357,8 @@ TypeCheckItem::visit (HIR::Enum &enum_decl) // get the path CanonicalPath canonical_path - = nr_ctx.to_canonical_path (enum_decl.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (enum_decl.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, enum_decl.get_locus ()}; @@ -406,7 +410,8 @@ TypeCheckItem::visit (HIR::Union &union_decl) // get the path CanonicalPath canonical_path - = nr_ctx.to_canonical_path (union_decl.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (union_decl.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, union_decl.get_locus ()}; @@ -569,7 +574,8 @@ TypeCheckItem::visit (HIR::Function &function) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath path - = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values); RustIdent ident{path, function.get_locus ()}; diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc index f4405f37bba6..94830ddbd593 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-path.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc @@ -23,6 +23,7 @@ #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-item.h" #include "rust-hir-trait-resolve.h" +#include "rust-rib.h" #include "rust-substitution-mapper.h" #include "rust-hir-path-probe.h" #include "rust-type-util.h" @@ -160,7 +161,8 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()), - Resolver2_0::Definition (root_resolved_node_id)); + Resolver2_0::Definition (root_resolved_node_id), + Resolver2_0::Namespace::Values); return; } @@ -258,7 +260,7 @@ 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)) + if (auto res = nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types)) { ref_node_id = *res; } @@ -545,7 +547,8 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id, auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); nr_ctx.map_usage (Resolver2_0::Usage (expr_mappings.get_nodeid ()), - Resolver2_0::Definition (resolved_node_id)); + Resolver2_0::Definition (resolved_node_id), + Resolver2_0::Namespace::Values); infered = tyseg; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc index 6cc14e01935f..8c99790f4d92 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc @@ -19,6 +19,7 @@ #include "rust-hir-type-check-pattern.h" #include "rust-hir-pattern.h" #include "rust-hir-type-check-expr.h" +#include "rust-rib.h" #include "rust-token.h" #include "rust-type-util.h" #include "rust-finalized-name-resolution-context.h" @@ -48,7 +49,7 @@ TypeCheckPattern::Resolve (HIR::Pattern &pattern, TyTy::BaseType *parent) void TypeCheckPattern::visit (HIR::PathInExpression &pattern) { - // Pattern must be enum variants, sturcts, constants, or associated constansts + // Pattern must be enum variants, structs, constants, or associated constansts TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (pattern); NodeId ref_node_id = UNKNOWN_NODEID; @@ -56,7 +57,9 @@ TypeCheckPattern::visit (HIR::PathInExpression &pattern) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); - if (auto id = nr_ctx.lookup (pattern.get_mappings ().get_nodeid ())) + // FIXME: Not sure if this is enough to look up enum variants + if (auto id = nr_ctx.lookup (pattern.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Values)) { ref_node_id = *id; maybe_item = true; diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 4bc73ada05ed..451261363fb0 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -26,6 +26,7 @@ #include "rust-hir-type-bounds.h" #include "rust-finalized-name-resolution-context.h" #include "rust-mapping-common.h" +#include "rust-rib.h" #include "rust-substitution-mapper.h" #include "rust-type-util.h" #include "rust-system.h" @@ -346,9 +347,8 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset, auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); // assign the ref_node_id if we've found something - nr_ctx.lookup (ast_node_id).map ([&ref_node_id] (NodeId resolved) { - ref_node_id = resolved; - }); + nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types) + .map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; }); } // ref_node_id is the NodeId that the segments refers to. @@ -1109,7 +1109,7 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); - if (auto id = nr_ctx.lookup (ast_node_id)) + if (auto id = nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types)) { ref_node_id = *id; } diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index 49f3aeb68cb6..e0f819ea1760 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -25,6 +25,7 @@ #include "rust-hir-type-check-pattern.h" #include "rust-hir-type-check-struct-field.h" #include "rust-finalized-name-resolution-context.h" +#include "rust-rib.h" extern bool saw_errors (void); @@ -278,7 +279,8 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); CanonicalPath canonical_path - = nr_ctx.to_canonical_path (fn.get_mappings ().get_nodeid ()); + = nr_ctx.to_canonical_path (fn.get_mappings ().get_nodeid (), + Resolver2_0::Namespace::Types); RustIdent ident{canonical_path, fn.get_locus ()}; auto resolved = new TyTy::FnType ( diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc index d59e5f0c02a0..76451c771b01 100644 --- a/gcc/rust/typecheck/rust-type-util.cc +++ b/gcc/rust/typecheck/rust-type-util.cc @@ -25,6 +25,7 @@ #include "rust-hir-type-check-type.h" #include "rust-casts.h" #include "rust-mapping-common.h" +#include "rust-rib.h" #include "rust-unify.h" #include "rust-coercion.h" #include "rust-hir-type-bounds.h" @@ -106,9 +107,8 @@ query_type (HirId reference, TyTy::BaseType **result) auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get (); // assign the ref_node_id if we've found something - nr_ctx.lookup (ast_node_id).map ([&ref_node_id] (NodeId resolved) { - ref_node_id = resolved; - }); + nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types) + .map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; }); if (ref_node_id != UNKNOWN_NODEID) {
