From patchwork Wed Apr 5 14:03:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79698 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp357572vqo; Wed, 5 Apr 2023 07:39:10 -0700 (PDT) X-Google-Smtp-Source: AKy350Y6rzu6B++yojuMjk/kepFXT0E+LF3ESBCy75VII/dmYl6ILdZ3CD4ycljfObYaO7pU8m2L X-Received: by 2002:a05:6402:160f:b0:502:9c52:4482 with SMTP id f15-20020a056402160f00b005029c524482mr2276419edv.6.1680705550662; Wed, 05 Apr 2023 07:39:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680705550; cv=none; d=google.com; s=arc-20160816; b=GB5vNBXXit27EE9vzxa/WN5Msx6XLNZONhRoXArS08x8yR1kvwYVkmb9nMuAoZcGV8 9AkgkeJewUOtXXYZHx4P3ZI3gy8SIS/P8SU6Jg4Zffi7A1f2iJQ64OCeoZK5YimC9lRF hItLe2WLF8IIAmu2uTShXx+6whwDJGRD6rzWVvbSZcaFfp54/lsoPq8iVid4xy3sOgKb /5amS998586oMlj754ysLyN1zz3/Bcfgr1scXs619hLW04q7awg2V1i82XNXc5Zis78U WNtEUBen+1n3vT4Lx5lJSeKYl5mNrhhl36EIY2tTQgU0kURiNRrabZ2sQTt9Pe/L4YeZ I90g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=V7WX+KkTwVqPGkhibX5iqKa4Qqgwekjer3Rw/oVeLRc=; b=SmFGKIELpH3APxPDp3gcZLXFIHCraqma32LjGpDU5Wm0X4ack9y7uInMU0PZp7Nb1U rh+1uqzf/HKEg/2kqMKeLW8FN/Nf06u9EURgkGrjO3VrWCS4QVYt7B0roiVpj3UrZpaB Y+sJL9xjs/DBMMO2YwTM1kqV4L4L67cRApiCxssh1h7Yi35fD1gQYrowEXxPiplc6isE 6yqXV1JpcE/BcUkF1o/GCKwx7IJdsl0J8rHNavh815Du744CANWVFn8gTAGbkLO1uSHB ktZGNI1Dj7lf6/WKqGxKVrcbD8qXa/xy5C8P25LuLLwEqW2DP0wyXheiE5dNvC/NAXBW 9XnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=V8jxkL+c; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id v7-20020aa7d647000000b004c0abff5545si8713636edr.614.2023.04.05.07.39.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:39:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=V8jxkL+c; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 690F839AFFE5 for ; Wed, 5 Apr 2023 14:14:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id B22313870D53 for ; Wed, 5 Apr 2023 14:06:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B22313870D53 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x334.google.com with SMTP id s13so20940006wmr.4 for ; Wed, 05 Apr 2023 07:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703563; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=V7WX+KkTwVqPGkhibX5iqKa4Qqgwekjer3Rw/oVeLRc=; b=V8jxkL+camAKVaCEwCJpeVfvKnYHa1c4NTdATa+ZBn4d78ru8cXPA8IQMn6LPN6dxO FX9caVTIB9YEccWQUHxdSHgfk2QxUkipUHcNCvCSFVEpBPq1wI7R1wjtzTSs4XpiY7WF jOy36B6q7bc2tYrG24chmERPApx2mD+N1Uy3w3/TDJjQqV9AzULzHp2YeeDx7bxjsbRf EeqDGCNFIlwevkp+t9f+9m1Tmwmv14G733aj/Cs2c39InQDp8HCotqGMom2qnG9sDWDP JmMcnQe+SjwFNbmuoIp7j1aUzmIqCcFAYZ3ucQt3WnUHF+UYdeh8Zwk++SmrEC7BrFJ9 LOSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703563; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=V7WX+KkTwVqPGkhibX5iqKa4Qqgwekjer3Rw/oVeLRc=; b=iM1t7LVCxvXjQZbtT0kpE/W3H2NCAyzl9+g5XLIAkFeFQmLG/VTsYGI5PSYRkxGRo/ NXOdZQ1MFYi+OSa6aOdEca47fcflBVgCYB02VKtKT9j3xd4/8b6bd4tfSoZo9dEKygBa JVvUv28lvW//p0p2+3WrV/ScLauGjSZBWwJ/te10ed4PnfKuJwd6C2GsDjrIRKa6Hysb LkyCV7U6oXjafO1TQzwxfVf0GHa5YQ79XIGkG1wmic+J+C/G2xi28FUGMR9/A+kYv08m mhfck4BHDgfAIJDN9l+mTnP2LveSWVdfRbJLB/eZ21d4gRpxeEclu8+X50kgyKd8JPj3 M17g== X-Gm-Message-State: AAQBX9fFpZn7Z8tw56BHYY9H/pbT+pc2kSz99SA6Y/X3ifgOiASp0Cn2 sfkTIcoW9dK8m/Oss6cpDU7Ad38lRhWoFvPbNw== X-Received: by 2002:a7b:c384:0:b0:3e9:f15b:935b with SMTP id s4-20020a7bc384000000b003e9f15b935bmr4677664wmj.32.1680703563004; Wed, 05 Apr 2023 07:06:03 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.06.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:06:02 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 49/88] gccrs: Remove monomorphization hack to setup possible associated types Date: Wed, 5 Apr 2023 16:03:33 +0200 Message-Id: <20230405140411.3016563-50-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762347503546891369?= X-GMAIL-MSGID: =?utf-8?q?1762347503546891369?= From: Philip Herron During CallExpr argument type checking we may be calling a default implementation of a trait function this will require any possible associated types to be resolved and setup. This monomoprhization call does this but it will premtivly do extra unification of types which will throw off type checking later on. This fix is required for my work into type bounds checking. Fixes #1773 Signed-off-by: Philip Herron gcc/rust/ChangeLog: * typecheck/rust-hir-trait-reference.h: change interface to return self * typecheck/rust-hir-trait-resolve.cc: likewise * typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): likewise * typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): remove monomorphization hack gcc/testsuite/ChangeLog: * rust/compile/issue-1773.rs: New test. --- gcc/rust/typecheck/rust-hir-trait-reference.h | 5 +-- gcc/rust/typecheck/rust-hir-trait-resolve.cc | 10 +++--- .../typecheck/rust-hir-type-check-path.cc | 36 ++++++++++++++----- gcc/rust/typecheck/rust-tyty-call.cc | 1 - gcc/testsuite/rust/compile/issue-1773.rs | 20 +++++++++++ 5 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/rust/compile/issue-1773.rs diff --git a/gcc/rust/typecheck/rust-hir-trait-reference.h b/gcc/rust/typecheck/rust-hir-trait-reference.h index 0df35265959..9b4461b9f18 100644 --- a/gcc/rust/typecheck/rust-hir-trait-reference.h +++ b/gcc/rust/typecheck/rust-hir-trait-reference.h @@ -497,8 +497,9 @@ public: TyTy::BaseType *get_self () { return self; } - void setup_associated_types (const TyTy::BaseType *self, - const TyTy::TypeBoundPredicate &bound); + TyTy::BaseType * + setup_associated_types (const TyTy::BaseType *self, + const TyTy::TypeBoundPredicate &bound); void reset_associated_types (); diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index e2d0cf2d7a2..6e23093eceb 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -377,13 +377,10 @@ TraitItemReference::associated_type_reset () const placeholder->clear_associated_type (); } -void +TyTy::BaseType * AssociatedImplTrait::setup_associated_types ( const TyTy::BaseType *self, const TyTy::TypeBoundPredicate &bound) { - if (!bound.contains_associated_types ()) - return; - // compute the constrained impl block generic arguments based on self and the // higher ranked trait bound TyTy::BaseType *receiver = self->clone (); @@ -486,6 +483,7 @@ AssociatedImplTrait::setup_associated_types ( TyTy::TyWithLocation (receiver), TyTy::TyWithLocation (impl_self_infer), impl_predicate.get_locus ()); rust_assert (result->get_kind () != TyTy::TypeKind::ERROR); + TyTy::BaseType *self_result = result; // unify the bounds arguments std::vector hrtb_bound_arguments; @@ -500,7 +498,7 @@ AssociatedImplTrait::setup_associated_types ( } if (impl_trait_predicate_args.size () != hrtb_bound_arguments.size ()) - return; + return self_result; for (size_t i = 0; i < impl_trait_predicate_args.size (); i++) { @@ -554,6 +552,8 @@ AssociatedImplTrait::setup_associated_types ( resolved_trait_item->associated_type_set (substituted); }); iter.go (); + + return self_result; } void diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc index 6f1fd416c19..9d9b2949944 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-path.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc @@ -379,16 +379,36 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id, if (associated_impl_block != nullptr) { - // get the type of the parent Self - HirId impl_ty_id - = associated_impl_block->get_type ()->get_mappings ().get_hirid (); + // associated types + HirId impl_block_id + = associated_impl_block->get_mappings ().get_hirid (); + + AssociatedImplTrait *associated = nullptr; + bool found_impl_trait + = context->lookup_associated_trait_impl (impl_block_id, + &associated); TyTy::BaseType *impl_block_ty = nullptr; - bool ok = query_type (impl_ty_id, &impl_block_ty); - rust_assert (ok); + if (found_impl_trait) + { + TyTy::TypeBoundPredicate predicate (*associated->get_trait (), + seg.get_locus ()); + impl_block_ty + = associated->setup_associated_types (prev_segment, predicate); + } + else + { + // get the type of the parent Self + HirId impl_ty_id = associated_impl_block->get_type () + ->get_mappings () + .get_hirid (); - if (impl_block_ty->needs_generic_substitutions ()) - impl_block_ty - = SubstMapper::InferSubst (impl_block_ty, seg.get_locus ()); + bool ok = query_type (impl_ty_id, &impl_block_ty); + rust_assert (ok); + + if (impl_block_ty->needs_generic_substitutions ()) + impl_block_ty + = SubstMapper::InferSubst (impl_block_ty, seg.get_locus ()); + } prev_segment = unify_site (seg.get_mappings ().get_hirid (), TyTy::TyWithLocation (prev_segment), diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index d520b595e43..4c6442e2d82 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -85,7 +85,6 @@ TypeCheckCallExpr::visit (ADTType &type) void TypeCheckCallExpr::visit (FnType &type) { - type.monomorphize (); if (call.num_params () != type.num_params ()) { if (type.is_varadic ()) diff --git a/gcc/testsuite/rust/compile/issue-1773.rs b/gcc/testsuite/rust/compile/issue-1773.rs new file mode 100644 index 00000000000..c627ac09cec --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-1773.rs @@ -0,0 +1,20 @@ +trait Foo { + type A; + + fn test(a: Self::A) -> Self::A { + a + } +} + +struct Bar(T); +impl Foo for Bar { + type A = T; +} + +fn main() { + let a; + a = Bar(123); + + let b; + b = Bar::test(a.0); +}