public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-8856] gccrs: refactor inference variable computation into a seperate method
@ 2024-02-07 12:44 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-02-07 12:44 UTC (permalink / raw)
  To: gcc-cvs

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

commit r14-8856-gc91248d1de2f83861a41b5f16d78b198d8a6bb44
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Sat Feb 3 15:18:51 2024 +0000

    gccrs: refactor inference variable computation into a seperate method
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check.cc (TypeResolution::Resolve): refactor
            * typecheck/rust-hir-type-check.h: new prototype
            * typecheck/rust-typecheck-context.cc (TypeCheckContext::compute_inference_variables): x

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check.cc    | 34 +----------------------
 gcc/rust/typecheck/rust-hir-type-check.h     |  2 ++
 gcc/rust/typecheck/rust-typecheck-context.cc | 41 ++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc
index 2b7868685d45..0bc72d3f73fc 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check.cc
@@ -76,40 +76,8 @@ TypeResolution::Resolve (HIR::Crate &crate)
   if (saw_errors ())
     return;
 
-  auto mappings = Analysis::Mappings::get ();
   auto context = TypeCheckContext::get ();
-
-  // default inference variables if possible
-  context->iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
-    // nothing to do
-    if (ty->get_kind () != TyTy::TypeKind::INFER)
-      return true;
-
-    TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
-    TyTy::BaseType *default_type;
-    bool ok = infer_var->default_type (&default_type);
-    if (!ok)
-      {
-	rust_error_at (mappings->lookup_location (id), ErrorCode::E0282,
-		       "type annotations needed");
-	return true;
-      }
-    else
-      {
-	auto result
-	  = unify_site (id, TyTy::TyWithLocation (ty),
-			TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
-	rust_assert (result);
-	rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
-	result->set_ref (id);
-	context->insert_type (
-	  Analysis::NodeMapping (mappings->get_current_crate (), 0, id,
-				 UNKNOWN_LOCAL_DEFID),
-	  result);
-      }
-
-    return true;
-  });
+  context->compute_inference_variables (true);
 }
 
 // rust-hir-trait-ref.h
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h
index 3d66e29052f9..52c84fc44354 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -231,6 +231,8 @@ public:
   WARN_UNUSED_RESULT std::vector<TyTy::Region>
   regions_from_generic_args (const HIR::GenericArgs &args) const;
 
+  void compute_inference_variables (bool error);
+
 private:
   TypeCheckContext ();
 
diff --git a/gcc/rust/typecheck/rust-typecheck-context.cc b/gcc/rust/typecheck/rust-typecheck-context.cc
index 1f4a5a356260..9059e0261b32 100644
--- a/gcc/rust/typecheck/rust-typecheck-context.cc
+++ b/gcc/rust/typecheck/rust-typecheck-context.cc
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-hir-type-check.h"
+#include "rust-type-util.h"
 
 namespace Rust {
 namespace Resolver {
@@ -576,6 +577,46 @@ TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const
   return regions;
 }
 
+void
+TypeCheckContext::compute_inference_variables (bool error)
+{
+  auto mappings = Analysis::Mappings::get ();
+
+  // default inference variables if possible
+  iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
+    // nothing to do
+    if (ty->get_kind () != TyTy::TypeKind::INFER)
+      return true;
+
+    TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
+    TyTy::BaseType *default_type;
+
+    rust_debug_loc (mappings->lookup_location (id),
+		    "trying to default infer-var: %s",
+		    infer_var->as_string ().c_str ());
+    bool ok = infer_var->default_type (&default_type);
+    if (!ok)
+      {
+	if (error)
+	  rust_error_at (mappings->lookup_location (id), ErrorCode::E0282,
+			 "type annotations needed");
+	return true;
+      }
+
+    auto result
+      = unify_site (id, TyTy::TyWithLocation (ty),
+		    TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
+    rust_assert (result);
+    rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
+    result->set_ref (id);
+    insert_type (Analysis::NodeMapping (mappings->get_current_crate (), 0, id,
+					UNKNOWN_LOCAL_DEFID),
+		 result);
+
+    return true;
+  });
+}
+
 // TypeCheckContextItem
 
 TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-07 12:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-07 12:44 [gcc r14-8856] gccrs: refactor inference variable computation into a seperate method Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).