https://gcc.gnu.org/g:a8b5f28aec56f355fe2db1e65d1cb380374f9ce2

commit a8b5f28aec56f355fe2db1e65d1cb380374f9ce2
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Thu Aug 24 17:51:21 2023 +0200

    typecheck: Start using nr2.0 properly
    
    Fetch the ImmutableNrCtx in order to access name resolver during
    typechecking.
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): 
Start
            fetching name resolution information in the typechecker.
            * typecheck/rust-hir-type-check-type.cc 
(TypeCheckType::resolve_root_path):
            Likewise.
            * typecheck/rust-hir-type-check-path.cc: Use nr 2.0.

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-item.cc | 91 +++++++++++++++++++++-----
 gcc/rust/typecheck/rust-hir-type-check-path.cc | 16 +++--
 gcc/rust/typecheck/rust-hir-type-check-type.cc | 24 +++++--
 3 files changed, 105 insertions(+), 26 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc 
b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 4bbd28021a02..4ab946e1c2a1 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -17,12 +17,17 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-hir-type-check-item.h"
+#include "rust-canonical-path.h"
+#include "rust-diagnostics.h"
 #include "rust-hir-type-check-enumitem.h"
 #include "rust-hir-type-check-implitem.h"
 #include "rust-hir-type-check-type.h"
 #include "rust-hir-type-check-expr.h"
 #include "rust-hir-type-check-pattern.h"
 #include "rust-hir-trait-resolve.h"
+#include "rust-identifier.h"
+#include "rust-session-manager.h"
+#include "rust-immutable-name-resolution-context.h"
 #include "rust-substitution-mapper.h"
 #include "rust-type-util.h"
 #include "rust-tyty-variance-analysis.h"
@@ -185,11 +190,30 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl)
     }
 
   // get the path
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok = mappings->lookup_canonical_path (
-    struct_decl.get_mappings ().get_nodeid (), &canonical_path);
-  rust_assert (ok);
-  RustIdent ident{*canonical_path, struct_decl.get_locus ()};
+
+  auto path = CanonicalPath::create_empty ();
+
+  // FIXME: HACK: ARTHUR: Disgusting
+  if (flag_name_resolution_2_0)
+    {
+      auto nr_ctx
+       = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+      auto canonical_path = nr_ctx.values.to_canonical_path (
+       struct_decl.get_mappings ().get_nodeid ());
+
+      path = canonical_path.value ();
+    }
+  else
+    {
+      const CanonicalPath *canonical_path = nullptr;
+      bool ok = mappings->lookup_canonical_path (
+       struct_decl.get_mappings ().get_nodeid (), &canonical_path);
+      rust_assert (ok);
+
+      path = *canonical_path;
+    }
+
+  RustIdent ident{path, struct_decl.get_locus ()};
 
   // its a single variant ADT
   std::vector<TyTy::VariantDef *> variants;
@@ -248,12 +272,29 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
       context->insert_type (field.get_mappings (), ty_field->get_field_type 
());
     }
 
-  // get the path
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok = mappings->lookup_canonical_path (
-    struct_decl.get_mappings ().get_nodeid (), &canonical_path);
-  rust_assert (ok);
-  RustIdent ident{*canonical_path, struct_decl.get_locus ()};
+  auto path = CanonicalPath::create_empty ();
+
+  // FIXME: HACK: ARTHUR: Disgusting
+  if (flag_name_resolution_2_0)
+    {
+      auto nr_ctx
+       = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+      auto canonical_path = nr_ctx.values.to_canonical_path (
+       struct_decl.get_mappings ().get_nodeid ());
+
+      path = canonical_path.value ();
+    }
+  else
+    {
+      const CanonicalPath *canonical_path = nullptr;
+      bool ok = mappings->lookup_canonical_path (
+       struct_decl.get_mappings ().get_nodeid (), &canonical_path);
+      rust_assert (ok);
+
+      path = *canonical_path;
+    }
+
+  RustIdent ident{path, struct_decl.get_locus ()};
 
   // its a single variant ADT
   std::vector<TyTy::VariantDef *> variants;
@@ -510,13 +551,29 @@ TypeCheckItem::visit (HIR::Function &function)
       TypeCheckPattern::Resolve (param.get_param_name ().get (), param_tyty);
     }
 
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok
-    = mappings->lookup_canonical_path (function.get_mappings ().get_nodeid (),
-                                      &canonical_path);
-  rust_assert (ok);
+  auto path = CanonicalPath::create_empty ();
+
+  // FIXME: HACK: ARTHUR: Disgusting
+  if (flag_name_resolution_2_0)
+    {
+      auto nr_ctx
+       = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+      auto canonical_path = nr_ctx.values.to_canonical_path (
+       function.get_mappings ().get_nodeid ());
+
+      path = canonical_path.value ();
+    }
+  else
+    {
+      const CanonicalPath *canonical_path = nullptr;
+      bool ok = mappings->lookup_canonical_path (
+       function.get_mappings ().get_nodeid (), &canonical_path);
+      rust_assert (ok);
+
+      path = *canonical_path;
+    }
 
-  RustIdent ident{*canonical_path, function.get_locus ()};
+  RustIdent ident{path, function.get_locus ()};
 
   auto fn_type = new TyTy::FnType (
     function.get_mappings ().get_hirid (),
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc 
b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index ad31fb74a80e..cdb506dacbe5 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -24,6 +24,8 @@
 #include "rust-hir-path-probe.h"
 #include "rust-type-util.h"
 #include "rust-hir-type-bounds.h"
+#include "rust-session-manager.h"
+#include "rust-immutable-name-resolution-context.h"
 
 namespace Rust {
 namespace Resolver {
@@ -197,12 +199,18 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression 
&expr, size_t *offset,
       bool is_root = *offset == 0;
       NodeId ast_node_id = seg.get_mappings ().get_nodeid ();
 
+      auto nr_ctx
+       = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+
       // then lookup the reference_node_id
       NodeId ref_node_id = UNKNOWN_NODEID;
-      if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
-       {
-         resolver->lookup_resolved_type (ast_node_id, &ref_node_id);
-       }
+
+      if (flag_name_resolution_2_0)
+       // assign the ref_node_id if we've found something
+       nr_ctx.lookup (expr.get_mappings ().get_nodeid ())
+         .map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
+      else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
+       resolver->lookup_resolved_type (ast_node_id, &ref_node_id);
 
       // ref_node_id is the NodeId that the segments refers to.
       if (ref_node_id == UNKNOWN_NODEID)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc 
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index a132b386a965..588e5bce88e1 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -17,10 +17,13 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-hir-type-check-type.h"
+#include "options.h"
 #include "rust-hir-trait-resolve.h"
 #include "rust-hir-type-check-expr.h"
 #include "rust-hir-path-probe.h"
 #include "rust-hir-type-bounds.h"
+#include "rust-immutable-name-resolution-context.h"
+#include "rust-mapping-common.h"
 #include "rust-substitution-mapper.h"
 #include "rust-type-util.h"
 
@@ -129,6 +132,8 @@ TypeCheckType::visit (HIR::TupleType &tuple)
 void
 TypeCheckType::visit (HIR::TypePath &path)
 {
+  rust_debug ("{ARTHUR}: Path visited: %s", path.as_string ().c_str ());
+
   // this can happen so we need to look up the root then resolve the
   // remaining segments if possible
   size_t offset = 0;
@@ -336,12 +341,21 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, 
size_t *offset,
       bool is_root = *offset == 0;
       NodeId ast_node_id = seg->get_mappings ().get_nodeid ();
 
+      auto nr_ctx
+       = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+
       // then lookup the reference_node_id
       NodeId ref_node_id = UNKNOWN_NODEID;
-      if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
-       {
-         resolver->lookup_resolved_type (ast_node_id, &ref_node_id);
-       }
+
+      // FIXME: HACK: ARTHUR: Remove this
+      if (flag_name_resolution_2_0)
+       // assign the ref_node_id if we've found something
+       nr_ctx.lookup (path.get_mappings ().get_nodeid ())
+         .map ([&ref_node_id, &path] (NodeId resolved) {
+           ref_node_id = resolved;
+         });
+      else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
+       resolver->lookup_resolved_type (ast_node_id, &ref_node_id);
 
       // ref_node_id is the NodeId that the segments refers to.
       if (ref_node_id == UNKNOWN_NODEID)
@@ -349,7 +363,7 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, 
size_t *offset,
          if (is_root)
            {
              rust_error_at (seg->get_locus (),
-                            "unknown reference for resolved name: %<%s%>",
+                            "unknown reference for resolved name: %qs",
                             seg->get_ident_segment ().as_string ().c_str ());
              return new TyTy::ErrorType (path.get_mappings ().get_hirid ());
            }

Reply via email to