]> gcc.gnu.org Git - gcc.git/commitdiff
gccrs: refactor inference variable computation into a seperate method
authorPhilip Herron <herron.philip@googlemail.com>
Sat, 3 Feb 2024 15:18:51 +0000 (15:18 +0000)
committerPhilip Herron <philip.herron@embecosm.com>
Sun, 4 Feb 2024 00:21:21 +0000 (00:21 +0000)
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

gcc/rust/typecheck/rust-hir-type-check.cc
gcc/rust/typecheck/rust-hir-type-check.h
gcc/rust/typecheck/rust-typecheck-context.cc

index cc8b05a2efc1ae5e26e0686ed3858bd8fe6a0db3..f22c68805034900952c8c33704b13b96ce0b36e9 100644 (file)
@@ -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
index a07ffb3cecbf8b96907bfeb6b7ea76ff840ed79d..88ac95eacdf5fedca751aeb1138599b941b9b3ff 100644 (file)
@@ -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 ();
 
index c949a7567706ebd536e6fa6bbc68e6abde7a950d..b059f63483b6692228765e95486c70733359c685 100644 (file)
@@ -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) {}
This page took 0.069229 seconds and 5 git commands to generate.