From patchwork Wed Apr 5 14:03:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79708 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp359342vqo; Wed, 5 Apr 2023 07:41:52 -0700 (PDT) X-Google-Smtp-Source: AKy350Ycsh6G/ACkMADDosVN3PWqIpeIEz8U687QMocP/Zr/MvwxxbfPoGLlPqaMcylnHxpg7VNb X-Received: by 2002:a17:906:f988:b0:931:6b2b:a671 with SMTP id li8-20020a170906f98800b009316b2ba671mr2996374ejb.65.1680705712177; Wed, 05 Apr 2023 07:41:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680705712; cv=none; d=google.com; s=arc-20160816; b=Q1JwTHw7W11hvjF6Iui5kJ+us1rBqpURAocMmG1AjI1XVUvOUyU1If2uAPLvqJczHt SeNfRTJvnBB2p2evObVLOP7L7Ou4mbRh/rPfQes8FLY/3OuYBISb7+nut6PGKGmNoFNh zm6DqRjXmzSpmhobz0dWrTLkz537Qy4aHo2V901w+xYBEEkfl6OdXbbdfqcwUBkH5oAR fukodkwWBJnUG29QLUZJ+4BbhKxwkpniUh/ZpPFPZPIaMhz1Bs/1gVVtqw8v0vHas1Kt APlDMI/EKeTuXa5H57U/xAgAuo6ndVt78f+VsqsYBYWmCqp5KOb+iolNjauGr5UwQzlp gW1A== 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=JdMuYAAo+JLXDSgsw/5eSKXlrKv4TUSMTCWd93BaoCY=; b=ym4kTCANK2gTtpCdW4YhTWPS1PbGiVAKVaVGKKmtdQ6WSfrYVVIZzPHBtTU0HQqr7h TCMBW06CEn5yvEi2zeTeZ7+FJ7kv9bJQnDKIC6TECZruuy1wCDHirIb+6lFpxMpiUPQ3 KvsxbCQWdNCoNxxBt4LQELmqKEN46did4K33/lWFFXmNnLDoN9qgCCrXsmnvdAbAEf/K Yrs6ouAyzq4FjRLU8A/NBMa1mETrjgFt22EGADjIcijrN7N6iSwpCq12h/RHj1nOWHXQ jTSG5u0s9fSipeZmwaQ3VhFMr8SindNNRritKqNL5CxA+tyqb1N/NTgZT/IpN2UO8elW LKgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=GjbUnohL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id zm9-20020a170906994900b00930943f134esi2146293ejb.8.2023.04.05.07.41.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:41:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=GjbUnohL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 51E413A61B99 for ; Wed, 5 Apr 2023 14:15:10 +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 4AC003834685 for ; Wed, 5 Apr 2023 14:06:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4AC003834685 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 n19-20020a05600c501300b003f064936c3eso1252596wmr.0 for ; Wed, 05 Apr 2023 07:06:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703565; 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=JdMuYAAo+JLXDSgsw/5eSKXlrKv4TUSMTCWd93BaoCY=; b=GjbUnohLK3AiKljmXdBEXrR3StmFATLbNO5qfh4N7aFBuRAFBotrwqoK+FI/iLOlmp AsqBzSMu8mqAABbXI/WkRw7lJKldVDacRAOmhOUoF6kwzxo4bIzx59DTeZ+xO4un/bdp xXi3KjebhOX6UCpwLsW+0IZJQtDHIbp1gRZsq8DVwDYmV22fmsd7Xz4EaWXSylT37gKR GkPPMnnoAL5oKJXc2z06J8mhDp3zgUCVpfT37hwor7pVqyfal61l6L4pdDwgcBvtIl/z q+E15DD8Q/0YD/Oc1AkNa32qLN6bycTdta4YIa89GXjjrxv2CLCaRibh5m5JogV7iGVp hzVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703565; 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=JdMuYAAo+JLXDSgsw/5eSKXlrKv4TUSMTCWd93BaoCY=; b=ZBLFOcyOXZb1xTNbTMvQboIV58ST0qn3Ff5sWrIfu+TrJTyKRYiigWvAnKPhFgE9zN CGA1XYxDumAmWF6kzLSjzLFjb1Ci29MnfsJP03raPp36VFe80IiJHJHq1HQWbhnIBtNk UPcZGXWYb9APnQkN27td/26HKkVof4HEtHdi9WKd+5ff5snTuS0Fsule7WmG8pS6RFiO q10P2WocTPsoCSskSIHZ0U3JHYnURsch3ufCWXUoDKe3QcfEVIr4eUZ50+I8y2Yrc4bG Q47c+X9L+7Q3cL6PfAinQOOHobdOdPtbvdqhBVz+zHlxB2gJdAfCm21aW5WGdVRnpqEW tofg== X-Gm-Message-State: AAQBX9c7FAtDxfwoDzjYwLuWohRhXAbmwbQF6g2Npcnm5dYLCvzsyXhV eMX+QlxK9VlaUwARSUHivt+kFhN7w8uG0ypPvA== X-Received: by 2002:a1c:6a13:0:b0:3df:e468:17dc with SMTP id f19-20020a1c6a13000000b003dfe46817dcmr4632970wmc.40.1680703565577; Wed, 05 Apr 2023 07:06:05 -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.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:06:05 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 53/88] gccrs: Fix higher ranked trait bounds computation of self Date: Wed, 5 Apr 2023 16:03:37 +0200 Message-Id: <20230405140411.3016563-54-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.3 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=unavailable 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?1762347673329426028?= X-GMAIL-MSGID: =?utf-8?q?1762347673329426028?= From: Philip Herron This updates the higher ranked trait bounds computation to handle ambigious cases. When we have a slice for example: let slice = &a[1..3]; This works by reusing the Index operator overload from libcore, so when the index range of 1..3 is computed, the type system needs to compute what the types of index are; this works by integer inference variables Range<> that need to be unified with the impl Index for Range which computes the real type of usize for the index. This is fine but what happens when we have the Copy and Clone traits bounds which have implementations for all the primitive types i8, i16, i32, i64... which is valid for any integer inference variable so the code prior to this patch would have grabbed the first impl it would have found and used it which is incorrect. When we have integer or float inference variables we need to look for their respective defaults or emit an ambigious type bound error. Signed-off-by: Philip Herron gcc/rust/ChangeLog: * typecheck/rust-hir-trait-reference.h: add const infterface * typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::get_generic_param): make const (SubstitutionRef::monomorphize): fix issue * typecheck/rust-tyty-subst.h: constify interface --- gcc/rust/typecheck/rust-hir-trait-reference.h | 1 + gcc/rust/typecheck/rust-tyty-subst.cc | 107 ++++++++++++++++-- gcc/rust/typecheck/rust-tyty-subst.h | 2 +- 3 files changed, 102 insertions(+), 8 deletions(-) diff --git a/gcc/rust/typecheck/rust-hir-trait-reference.h b/gcc/rust/typecheck/rust-hir-trait-reference.h index 9b4461b9f18..adb63b468d1 100644 --- a/gcc/rust/typecheck/rust-hir-trait-reference.h +++ b/gcc/rust/typecheck/rust-hir-trait-reference.h @@ -496,6 +496,7 @@ public: HIR::ImplBlock *get_impl_block () { return impl; } TyTy::BaseType *get_self () { return self; } + const TyTy::BaseType *get_self () const { return self; } TyTy::BaseType * setup_associated_types (const TyTy::BaseType *self, diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc index 996bbf2d885..3dfe95873bc 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.cc +++ b/gcc/rust/typecheck/rust-tyty-subst.cc @@ -65,7 +65,7 @@ SubstitutionParamMapping::get_param_ty () const } const HIR::TypeParam & -SubstitutionParamMapping::get_generic_param () +SubstitutionParamMapping::get_generic_param () const { return generic; } @@ -892,8 +892,7 @@ SubstitutionRef::monomorphize () // setup any associated type mappings for the specified bonds and this // type auto candidates = Resolver::TypeBoundsProbe::Probe (binding); - - Resolver::AssociatedImplTrait *associated_impl_trait = nullptr; + std::vector associated_impl_traits; for (auto &probed_bound : candidates) { const Resolver::TraitReference *bound_trait_ref @@ -914,15 +913,109 @@ SubstitutionRef::monomorphize () = associated->get_self ()->can_eq (binding, false); if (found_trait && found_self) { - associated_impl_trait = associated; - break; + associated_impl_traits.push_back (associated); } } } - if (associated_impl_trait != nullptr) + if (!associated_impl_traits.empty ()) { - associated_impl_trait->setup_associated_types (binding, bound); + // This code is important when you look at slices for example when + // you have a slice such as: + // + // let slice = &array[1..3] + // + // the higher ranked bounds will end up having an Index trait + // implementation for Range so we need this code to resolve + // that we have an integer inference variable that needs to become + // a usize + // + // The other complicated issue is that we might have an intrinsic + // which requires the :Clone or Copy bound but the libcore adds + // implementations for all the integral types so when there are + // multiple candidates we need to resolve to the default + // implementation for that type otherwise its an error for + // ambiguous type bounds + + if (associated_impl_traits.size () == 1) + { + Resolver::AssociatedImplTrait *associate_impl_trait + = associated_impl_traits.at (0); + associate_impl_trait->setup_associated_types (binding, bound); + } + else + { + // if we have a non-general inference variable we need to be + // careful about the selection here + bool is_infer_var + = binding->get_kind () == TyTy::TypeKind::INFER; + bool is_integer_infervar + = is_infer_var + && static_cast (binding) + ->get_infer_kind () + == TyTy::InferType::InferTypeKind::INTEGRAL; + bool is_float_infervar + = is_infer_var + && static_cast (binding) + ->get_infer_kind () + == TyTy::InferType::InferTypeKind::FLOAT; + + Resolver::AssociatedImplTrait *associate_impl_trait = nullptr; + if (is_integer_infervar) + { + TyTy::BaseType *type = nullptr; + bool ok = context->lookup_builtin ("i32", &type); + rust_assert (ok); + + for (auto &impl : associated_impl_traits) + { + bool found = impl->get_self ()->is_equal (*type); + if (found) + { + associate_impl_trait = impl; + break; + } + } + } + else if (is_float_infervar) + { + TyTy::BaseType *type = nullptr; + bool ok = context->lookup_builtin ("f64", &type); + rust_assert (ok); + + for (auto &impl : associated_impl_traits) + { + bool found = impl->get_self ()->is_equal (*type); + if (found) + { + associate_impl_trait = impl; + break; + } + } + } + + if (associate_impl_trait == nullptr) + { + // go for the first one? or error out? + auto &mappings = *Analysis::Mappings::get (); + const auto &type_param = subst.get_generic_param (); + const auto *trait_ref = bound.get (); + + RichLocation r (type_param.get_locus ()); + r.add_range (bound.get_locus ()); + r.add_range ( + mappings.lookup_location (binding->get_ref ())); + + rust_error_at ( + r, "ambiguous type bound for trait %s and type %s", + trait_ref->get_name ().c_str (), + binding->get_name ().c_str ()); + + return false; + } + + associate_impl_trait->setup_associated_types (binding, bound); + } } } } diff --git a/gcc/rust/typecheck/rust-tyty-subst.h b/gcc/rust/typecheck/rust-tyty-subst.h index 039eb36589e..982861e9bc6 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.h +++ b/gcc/rust/typecheck/rust-tyty-subst.h @@ -48,7 +48,7 @@ public: const ParamType *get_param_ty () const; - const HIR::TypeParam &get_generic_param (); + const HIR::TypeParam &get_generic_param () const; // this is used for the backend to override the HirId ref of the param to // what the concrete type is for the rest of the context