]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: typecheck: Refactor coercion_site
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 22 Feb 2023 09:44:16 +0000 (10:44 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:13:35 +0000 (18:13 +0100)
Refactor coercion_site to be a simple function in rust-type-util.h
instead of a static function in TypeCheckBase.
gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc (TraitItemReference::resolve_item):
Remove TypeCheckBase namespace qualifier.
* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::coercion_site):
Remove coercion_site function.
* typecheck/rust-hir-type-check-base.h: Remove coercion_site
prototype.
* typecheck/rust-type-util.cc (coercion_site): Add coercion_site
function.
* typecheck/rust-type-util.h (coercion_site): Add coercion_site
prototype.
* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit):
Remove TypeCheckBase namespace qualifier.
(TypeCheckMethodCallExpr::check): Remove TypeCheckBase namespace
qualifier.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/typecheck/rust-hir-trait-resolve.cc
gcc/rust/typecheck/rust-hir-type-check-base.cc
gcc/rust/typecheck/rust-hir-type-check-base.h
gcc/rust/typecheck/rust-type-util.cc
gcc/rust/typecheck/rust-type-util.h
gcc/rust/typecheck/rust-tyty-call.cc

index 388a5b17400050ea0269f0a44a0ab28baef12eb2..f96c84c2410fbd44124a045271a934d119cb2a1e 100644 (file)
@@ -349,11 +349,9 @@ TraitItemReference::resolve_item (HIR::TraitItemFunc &func)
        ? func.get_decl ().get_return_type ()->get_locus ()
        : func.get_locus ();
 
-  TypeCheckBase::coercion_site (func.get_mappings ().get_hirid (),
-                               TyTy::TyWithLocation (expected_ret_tyty,
-                                                     fn_return_locus),
-                               TyTy::TyWithLocation (block_expr_ty),
-                               func.get_locus ());
+  coercion_site (func.get_mappings ().get_hirid (),
+                TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus),
+                TyTy::TyWithLocation (block_expr_ty), func.get_locus ());
 
   context->pop_return_type ();
 }
index 6f105ce9ef917e7d042a072e6d649d77f3353085..c99210992c40fef3da94479c3278dc6e0a749819 100644 (file)
@@ -18,7 +18,6 @@
 
 #include "rust-hir-type-check-base.h"
 #include "rust-casts.h"
-#include "rust-coercion.h"
 #include "rust-hir-type-check-expr.h"
 #include "rust-hir-type-check-implitem.h"
 #include "rust-hir-type-check-item.h"
@@ -349,40 +348,6 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, Location locus)
   return repr;
 }
 
-TyTy::BaseType *
-TypeCheckBase::coercion_site (HirId id, TyTy::TyWithLocation lhs,
-                             TyTy::TyWithLocation rhs, Location locus)
-{
-  TyTy::BaseType *expected = lhs.get_ty ();
-  TyTy::BaseType *expr = rhs.get_ty ();
-
-  rust_debug ("coercion_site id={%u} expected={%s} expr={%s}", id,
-             expected->debug_str ().c_str (), expr->debug_str ().c_str ());
-
-  auto context = TypeCheckContext::get ();
-  if (expected->get_kind () == TyTy::TypeKind::ERROR
-      || expr->get_kind () == TyTy::TypeKind::ERROR)
-    return expr;
-
-  // can we autoderef it?
-  auto result = TypeCoercionRules::Coerce (expr, expected, locus);
-
-  // the result needs to be unified
-  TyTy::BaseType *receiver = expr;
-  if (!result.is_error ())
-    {
-      receiver = result.tyty;
-    }
-
-  rust_debug ("coerce_default_unify(a={%s}, b={%s})",
-             receiver->debug_str ().c_str (), expected->debug_str ().c_str ());
-  TyTy::BaseType *coerced
-    = unify_site (id, lhs, TyTy::TyWithLocation (receiver, rhs.get_locus ()),
-                 locus);
-  context->insert_autoderef_mappings (id, std::move (result.adjustments));
-  return coerced;
-}
-
 TyTy::BaseType *
 TypeCheckBase::cast_site (HirId id, TyTy::TyWithLocation from,
                          TyTy::TyWithLocation to, Location cast_locus)
index f0b3bbba85a635c03e2c2ca591a387dec05a3b3e..ba54ff3b54a76f7dcae8066059df908e15c1800c 100644 (file)
@@ -34,10 +34,6 @@ class TypeCheckBase
 public:
   virtual ~TypeCheckBase () {}
 
-  static TyTy::BaseType *coercion_site (HirId id, TyTy::TyWithLocation lhs,
-                                       TyTy::TyWithLocation rhs,
-                                       Location coercion_locus);
-
   static TyTy::BaseType *cast_site (HirId id, TyTy::TyWithLocation from,
                                    TyTy::TyWithLocation to,
                                    Location cast_locus);
index 5f10adefc066ef8eaae35fe102997d686fa7d7b9..c186411bbdce6dea099ee8b9b70efcc749c986e3 100644 (file)
@@ -25,6 +25,7 @@
 #include "rust-hir-visitor.h"
 #include "rust-name-resolver.h"
 #include "rust-unify.h"
+#include "rust-coercion.h"
 
 namespace Rust {
 namespace Resolver {
@@ -119,5 +120,39 @@ unify_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
                              true /*emit_error*/);
 }
 
+TyTy::BaseType *
+coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
+              Location locus)
+{
+  TyTy::BaseType *expected = lhs.get_ty ();
+  TyTy::BaseType *expr = rhs.get_ty ();
+
+  rust_debug ("coercion_site id={%u} expected={%s} expr={%s}", id,
+             expected->debug_str ().c_str (), expr->debug_str ().c_str ());
+
+  auto context = TypeCheckContext::get ();
+  if (expected->get_kind () == TyTy::TypeKind::ERROR
+      || expr->get_kind () == TyTy::TypeKind::ERROR)
+    return expr;
+
+  // can we autoderef it?
+  auto result = TypeCoercionRules::Coerce (expr, expected, locus);
+
+  // the result needs to be unified
+  TyTy::BaseType *receiver = expr;
+  if (!result.is_error ())
+    {
+      receiver = result.tyty;
+    }
+
+  rust_debug ("coerce_default_unify(a={%s}, b={%s})",
+             receiver->debug_str ().c_str (), expected->debug_str ().c_str ());
+  TyTy::BaseType *coerced
+    = unify_site (id, lhs, TyTy::TyWithLocation (receiver, rhs.get_locus ()),
+                 locus);
+  context->insert_autoderef_mappings (id, std::move (result.adjustments));
+  return coerced;
+}
+
 } // namespace Resolver
 } // namespace Rust
index 2adf9d80a2fa3c54db5f9a3a1c91be8b91613dbd..607994aa8109467f9275216a9dcbd15b74fcc56d 100644 (file)
@@ -37,6 +37,10 @@ TyTy::BaseType *
 unify_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
            Location unify_locus);
 
+TyTy::BaseType *
+coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
+              Location coercion_locus);
+
 } // namespace Resolver
 } // namespace Rust
 
index b51414706d9a2ceeb69f454a37475d0f354ce13b..156ed98205499c99b590f4335ab1fa188b21cd76 100644 (file)
@@ -60,9 +60,10 @@ TypeCheckCallExpr::visit (ADTType &type)
        }
 
       HirId coercion_side_id = argument->get_mappings ().get_hirid ();
-      auto res = Resolver::TypeCheckBase::coercion_site (
-       coercion_side_id, TyWithLocation (field_tyty),
-       TyWithLocation (arg, arg_locus), argument->get_locus ());
+      auto res = Resolver::coercion_site (coercion_side_id,
+                                         TyWithLocation (field_tyty),
+                                         TyWithLocation (arg, arg_locus),
+                                         argument->get_locus ());
       if (res->get_kind () == TyTy::TypeKind::ERROR)
        {
          return;
@@ -134,10 +135,12 @@ TypeCheckCallExpr::visit (FnType &type)
                : fn_param_pattern->get_locus ();
 
          HirId coercion_side_id = argument->get_mappings ().get_hirid ();
-         auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site (
-           coercion_side_id, TyWithLocation (param_ty, param_locus),
-           TyWithLocation (argument_expr_tyty, arg_locus),
-           argument->get_locus ());
+         auto resolved_argument_type
+           = Resolver::coercion_site (coercion_side_id,
+                                      TyWithLocation (param_ty, param_locus),
+                                      TyWithLocation (argument_expr_tyty,
+                                                      arg_locus),
+                                      argument->get_locus ());
          if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
            {
              return;
@@ -240,7 +243,7 @@ TypeCheckCallExpr::visit (FnPtr &type)
          return;
        }
 
-      auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site (
+      auto resolved_argument_type = Resolver::coercion_site (
        argument->get_mappings ().get_hirid (), TyWithLocation (fnparam),
        TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ());
       if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
@@ -347,7 +350,7 @@ TypeCheckMethodCallExpr::check (FnType &type)
 
       auto argument_expr_tyty = argument.get_argument_type ();
       HirId coercion_side_id = argument.get_mappings ().get_hirid ();
-      auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site (
+      auto resolved_argument_type = Resolver::coercion_site (
        coercion_side_id, TyWithLocation (param_ty, param_locus),
        TyWithLocation (argument_expr_tyty, arg_locus), arg_locus);
       if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)