From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 587EE381ECB3; Wed, 8 Jun 2022 12:41:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 587EE381ECB3 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Support recursive coercion sites X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 44c058c30dd1d36f87c36f2c487355a5736d0afc X-Git-Newrev: 7e576d70b6a1065bf31c37380a8e39354d961cd6 Message-Id: <20220608124131.587EE381ECB3@sourceware.org> Date: Wed, 8 Jun 2022 12:41:31 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:41:31 -0000 https://gcc.gnu.org/g:7e576d70b6a1065bf31c37380a8e39354d961cd6 commit 7e576d70b6a1065bf31c37380a8e39354d961cd6 Author: Philip Herron Date: Fri Apr 29 14:37:44 2022 +0100 Support recursive coercion sites Our code for coercion sites are handled in two places once where we do the type-checking portion and one where we do the code-generation. This makes the code-generation piece recursive so that we apply the behaviours consistantly when types need checked or converted. Fixes #1146 Diff: --- gcc/rust/backend/rust-compile-expr.cc | 16 +--- gcc/rust/backend/rust-compile.cc | 163 +++++++++++++++++++++------------- 2 files changed, 101 insertions(+), 78 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 5f9334cd1d1..422526321b4 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -677,17 +677,6 @@ CompileExpr::compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn, = ctx->get_backend ()->convert_expression (expected_fntype, fn_vtable_access, expr_locus); - fncontext fnctx = ctx->peek_fn (); - tree enclosing_scope = ctx->peek_enclosing_scope (); - bool is_address_taken = false; - tree ret_var_stmt = NULL_TREE; - Bvariable *fn_convert_expr_tmp - = ctx->get_backend ()->temporary_variable (fnctx.fndecl, enclosing_scope, - expected_fntype, fn_convert_expr, - is_address_taken, expr_locus, - &ret_var_stmt); - ctx->add_statement (ret_var_stmt); - std::vector args; args.push_back (self_argument); for (auto &argument : arguments) @@ -696,10 +685,7 @@ CompileExpr::compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn, args.push_back (compiled_expr); } - tree fn_expr - = ctx->get_backend ()->var_expression (fn_convert_expr_tmp, expr_locus); - - return ctx->get_backend ()->call_expression (fn_expr, args, nullptr, + return ctx->get_backend ()->call_expression (fn_convert_expr, args, nullptr, expr_locus); } diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index ad36a3790d1..5c9bbf90e66 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -202,27 +202,104 @@ HIRCompileBase::coercion_site (tree rvalue, TyTy::BaseType *actual, TyTy::BaseType *expected, Location lvalue_locus, Location rvalue_locus) { - auto root_actual_kind = actual->get_root ()->get_kind (); - auto root_expected_kind = expected->get_root ()->get_kind (); + if (rvalue == error_mark_node) + return error_mark_node; - if (root_expected_kind == TyTy::TypeKind::ARRAY - && root_actual_kind == TyTy::TypeKind::ARRAY) + if (expected->get_kind () == TyTy::TypeKind::REF) { - tree tree_rval_type - = TyTyResolveCompile::compile (ctx, actual->get_root ()); - tree tree_lval_type - = TyTyResolveCompile::compile (ctx, expected->get_root ()); + // bad coercion... of something to a reference + if (actual->get_kind () != TyTy::TypeKind::REF) + return error_mark_node; + + TyTy::ReferenceType *exp = static_cast (expected); + TyTy::ReferenceType *act = static_cast (actual); + + tree expected_type = TyTyResolveCompile::compile (ctx, act->get_base ()); + tree deref_rvalue + = ctx->get_backend ()->indirect_expression (expected_type, rvalue, + false /*known_valid*/, + rvalue_locus); + tree coerced + = coercion_site (deref_rvalue, act->get_base (), exp->get_base (), + lvalue_locus, rvalue_locus); + + return address_expression (coerced, + build_reference_type (TREE_TYPE (coerced)), + rvalue_locus); + } + else if (expected->get_kind () == TyTy::TypeKind::POINTER) + { + // bad coercion... of something to a reference + bool valid_coercion = actual->get_kind () == TyTy::TypeKind::REF + || actual->get_kind () == TyTy::TypeKind::POINTER; + if (!valid_coercion) + return error_mark_node; + + TyTy::ReferenceType *exp = static_cast (expected); + + TyTy::BaseType *actual_base = nullptr; + tree expected_type = error_mark_node; + if (actual->get_kind () == TyTy::TypeKind::REF) + { + TyTy::ReferenceType *act + = static_cast (actual); + actual_base = act->get_base (); + expected_type = TyTyResolveCompile::compile (ctx, act->get_base ()); + } + else if (actual->get_kind () == TyTy::TypeKind::POINTER) + { + TyTy::PointerType *act = static_cast (actual); + actual_base = act->get_base (); + expected_type = TyTyResolveCompile::compile (ctx, act->get_base ()); + } + rust_assert (actual_base != nullptr); + + tree deref_rvalue + = ctx->get_backend ()->indirect_expression (expected_type, rvalue, + false /*known_valid*/, + rvalue_locus); + tree coerced = coercion_site (deref_rvalue, actual_base, exp->get_base (), + lvalue_locus, rvalue_locus); + + return address_expression (coerced, + build_pointer_type (TREE_TYPE (coerced)), + rvalue_locus); + } + else if (expected->get_kind () == TyTy::TypeKind::ARRAY) + { + if (actual->get_kind () != TyTy::TypeKind::ARRAY) + return error_mark_node; + + tree tree_rval_type = TyTyResolveCompile::compile (ctx, actual); + tree tree_lval_type = TyTyResolveCompile::compile (ctx, expected); if (!verify_array_capacities (tree_lval_type, tree_rval_type, lvalue_locus, rvalue_locus)) return error_mark_node; } - else if (root_expected_kind == TyTy::TypeKind::DYNAMIC - && root_actual_kind != TyTy::TypeKind::DYNAMIC) + else if (expected->get_kind () == TyTy::TypeKind::DYNAMIC + && actual->get_kind () != TyTy::TypeKind::DYNAMIC) { const TyTy::DynamicObjectType *dyn - = static_cast (expected->get_root ()); + = static_cast (expected); return coerce_to_dyn_object (rvalue, actual, expected, dyn, rvalue_locus); } + else if (expected->get_kind () == TyTy::TypeKind::SLICE) + { + // bad coercion + bool valid_coercion = actual->get_kind () == TyTy::TypeKind::SLICE + || actual->get_kind () == TyTy::TypeKind::ARRAY; + if (!valid_coercion) + return error_mark_node; + + // nothing to do here + if (actual->get_kind () == TyTy::TypeKind::SLICE) + return rvalue; + + // return an unsized coercion + Resolver::Adjustment unsize_adj ( + Resolver::Adjustment::AdjustmentType::UNSIZE, expected); + return resolve_unsized_adjustment (unsize_adj, rvalue, rvalue_locus); + } return rvalue; } @@ -240,12 +317,17 @@ HIRCompileBase::coerce_to_dyn_object (tree compiled_ref, // __trait_object_ptr // [list of function ptrs] - auto root = actual->get_root (); std::vector> - probed_bounds_for_receiver = Resolver::TypeBoundsProbe::Probe (root); - + probed_bounds_for_receiver = Resolver::TypeBoundsProbe::Probe (actual); + + tree address_of_compiled_ref = null_pointer_node; + if (!actual->is_unit ()) + address_of_compiled_ref + = address_expression (compiled_ref, + build_pointer_type (TREE_TYPE (compiled_ref)), + locus); std::vector vals; - vals.push_back (compiled_ref); + vals.push_back (address_of_compiled_ref); for (auto &bound : ty->get_object_items ()) { const Resolver::TraitItemReference *item = bound.first; @@ -253,57 +335,12 @@ HIRCompileBase::coerce_to_dyn_object (tree compiled_ref, auto address = compute_address_for_trait_item (item, predicate, probed_bounds_for_receiver, - actual, root, locus); + actual, actual, locus); vals.push_back (address); } - tree constructed_trait_object - = ctx->get_backend ()->constructor_expression (dynamic_object, false, vals, - -1, locus); - - fncontext fnctx = ctx->peek_fn (); - tree enclosing_scope = ctx->peek_enclosing_scope (); - bool is_address_taken = false; - tree ret_var_stmt = NULL_TREE; - - Bvariable *dyn_tmp = ctx->get_backend ()->temporary_variable ( - fnctx.fndecl, enclosing_scope, dynamic_object, constructed_trait_object, - is_address_taken, locus, &ret_var_stmt); - ctx->add_statement (ret_var_stmt); - - // FIXME this needs to be more generic to apply any covariance - - auto e = expected; - std::vector adjustments; - while (e->get_kind () == TyTy::TypeKind::REF) - { - auto r = static_cast (e); - e = r->get_base (); - - if (r->is_mutable ()) - adjustments.push_back ( - Resolver::Adjustment (Resolver::Adjustment::AdjustmentType::MUT_REF, - e)); - else - adjustments.push_back ( - Resolver::Adjustment (Resolver::Adjustment::AdjustmentType::IMM_REF, - e)); - } - - auto resulting_dyn_object_ref - = ctx->get_backend ()->var_expression (dyn_tmp, locus); - for (auto it = adjustments.rbegin (); it != adjustments.rend (); it++) - { - bool ok - = it->get_type () == Resolver::Adjustment::AdjustmentType::IMM_REF - || it->get_type () == Resolver::Adjustment::AdjustmentType::MUT_REF; - rust_assert (ok); - - resulting_dyn_object_ref = address_expression ( - resulting_dyn_object_ref, - build_reference_type (TREE_TYPE (resulting_dyn_object_ref)), locus); - } - return resulting_dyn_object_ref; + return ctx->get_backend ()->constructor_expression (dynamic_object, false, + vals, -1, locus); } tree