public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] typecheck: Refactor coercion_site
@ 2023-02-23 16:35 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-02-23 16:35 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:918851ad2c965cef63dd0cb0be01f80a80d6c9c7
commit 918851ad2c965cef63dd0cb0be01f80a80d6c9c7
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date: Wed Feb 22 10:44:16 2023 +0100
typecheck: Refactor coercion_site
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>
Diff:
---
gcc/rust/typecheck/rust-hir-trait-resolve.cc | 8 +++---
gcc/rust/typecheck/rust-hir-type-check-base.cc | 35 --------------------------
gcc/rust/typecheck/rust-hir-type-check-base.h | 4 ---
gcc/rust/typecheck/rust-type-util.cc | 35 ++++++++++++++++++++++++++
gcc/rust/typecheck/rust-type-util.h | 4 +++
gcc/rust/typecheck/rust-tyty-call.cc | 21 +++++++++-------
6 files changed, 54 insertions(+), 53 deletions(-)
diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
index cb8d4d62fce..5a4d9660219 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
@@ -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 ();
}
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index e401e831441..0c54a05ced1 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -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)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.h b/gcc/rust/typecheck/rust-hir-type-check-base.h
index 133c91eca0f..e6f0e3c4305 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.h
@@ -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);
diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc
index 19935ebd922..16b0a240589 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -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
diff --git a/gcc/rust/typecheck/rust-type-util.h b/gcc/rust/typecheck/rust-type-util.h
index e504082cec3..764ec4d7b81 100644
--- a/gcc/rust/typecheck/rust-type-util.h
+++ b/gcc/rust/typecheck/rust-type-util.h
@@ -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
diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc
index 3d5fc6593da..b50c809a561 100644
--- a/gcc/rust/typecheck/rust-tyty-call.cc
+++ b/gcc/rust/typecheck/rust-tyty-call.cc
@@ -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)
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-02-23 16:35 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-23 16:35 [gcc/devel/rust/master] typecheck: Refactor coercion_site Thomas Schwinge
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).