From patchwork Tue Sep 27 10:35:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1489 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp255300wrt; Tue, 27 Sep 2022 03:36:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5ApyUqxnHue/VZMzADoesF7PNc2vwh1h3ZbPdFRp1zNecQJOFJWReCwhQtmnz6EDDJpDQu X-Received: by 2002:a17:907:3f1e:b0:782:fd7a:5c27 with SMTP id hq30-20020a1709073f1e00b00782fd7a5c27mr12757000ejc.349.1664275002282; Tue, 27 Sep 2022 03:36:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664275002; cv=none; d=google.com; s=arc-20160816; b=S3lyoUyooWBzhA8YBxsSZf6BRctzabItExgR4/0KsjFJYzm7B4SDg0nH5LKUUnzlzY rEEEbanbaFBWVkELOvgAdZKyqAaezDBBGpnQmZi351kIl6vllvpxMVltfp2ctdkbM4EA b+cpvN9MhrgmxfCu3H0MOvfKcyZf+jHPI7LZLwgoHLqDCOuQZOqLUBvrTB1RMZ8upr4N ebqxTyfVrCC35aFBK2O0ehCn589Z3ICyZ3dAipYdHxvceqvwziekN55hMmcs7NHElLYj TdzBQxovtLyaEJMO+jXM1va85qU1tO8rArQdw2ZD7HvBiC2OnDxalJjtqGIk1JPm1NSC Pe5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=fa10asfYCu6LBr6bh9vWZDlJKRcWOrwM+rioRFBwSRg=; b=TW1O3dMKWnsasRittMryqsQHDibZBb1ZBrxZ6RV0XA3d1BJpQ4MLH0f0Yla8iXWqXz /qzaPxfSSM2qY3WKhpVQQ83VaFd5G3zL884FxWnj/8xHLk6HGPb3yziLwzINcp8hb6wl 1XohO1oC2G1vHvfAuf9Y8fCiZRFpKex+u97ZdZ60Ind+28LxBS+QBnqf8slBY/P4BMD4 z7POvZGmCenfbRV2gsXZ2lpFzUAQQ/kJxNlbNRgIv+56yv1mRnrPns6d+aVG9N46kj6I Ehi0UYk44+54Jnm+HFeehXBuPpk4jUF+o1IZMeDLGOm+Np416Yym7i/ykz/MfbhFjb1M hvoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=MMASUb6Z; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hw19-20020a170907a0d300b00781bf46536bsi657462ejc.4.2022.09.27.03.36.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 03:36:42 -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=@gcc.gnu.org header.s=default header.b=MMASUb6Z; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6E96E38582B4 for ; Tue, 27 Sep 2022 10:36:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E96E38582B4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664274991; bh=fa10asfYCu6LBr6bh9vWZDlJKRcWOrwM+rioRFBwSRg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=MMASUb6ZranAazqcIDdI4B6QRdV+4r+kuFCBwMstFPo2bRDf15q+sXO3GeW1XwU4t BQxCkRbHwkw/IYWz7tsm0P3klYMRCpHfsy9aDVaVG1fnptqhmmtc9L2mCL8vKNe9bG tTv+mFsQ/NfyEL6lHCqG1I51EAS9OVc3pkpNzvOU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0ACF638582B7 for ; Tue, 27 Sep 2022 10:35:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0ACF638582B7 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-39-D2fSTe8pOnqKfXbPIcBowQ-1; Tue, 27 Sep 2022 06:35:11 -0400 X-MC-Unique: D2fSTe8pOnqKfXbPIcBowQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3D9CB1C05159; Tue, 27 Sep 2022 10:35:11 +0000 (UTC) Received: from localhost (unknown [10.33.36.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id F19DC483EFB; Tue, 27 Sep 2022 10:35:10 +0000 (UTC) To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] c++: Make __is_{, nothrow_}convertible SFINAE on access [PR107049] Date: Tue, 27 Sep 2022 11:35:10 +0100 Message-Id: <20220927103510.2321453-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Cc: Marek Polacek 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?1745118824591619009?= X-GMAIL-MSGID: =?utf-8?q?1745118824591619009?= Tested powerpc64le-linux. OK for trunk? -- >8 -- The is_convertible built-ins should return false if the conversion fails an access check, not report an error. PR c++/107049 gcc/cp/ChangeLog: * method.cc (is_convertible_helper): Use access check sentinel. gcc/testsuite/ChangeLog: * g++.dg/ext/is_convertible4.C: New test. * g++.dg/ext/is_nothrow_convertible4.C: New test. libstdc++-v3/ChangeLog: * testsuite/20_util/is_convertible/requirements/access.cc: New test. --- gcc/cp/method.cc | 1 + gcc/testsuite/g++.dg/ext/is_convertible4.C | 33 +++++++++++++++++++ .../g++.dg/ext/is_nothrow_convertible4.C | 33 +++++++++++++++++++ .../is_convertible/requirements/access.cc | 18 ++++++++++ 4 files changed, 85 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible4.C create mode 100644 gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C create mode 100644 libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index 9f917f13134..55af5c43c18 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -2246,6 +2246,7 @@ is_convertible_helper (tree from, tree to) return integer_one_node; cp_unevaluated u; tree expr = build_stub_object (from); + deferring_access_check_sentinel acs (dk_no_deferred); return perform_implicit_conversion (to, expr, tf_none); } diff --git a/gcc/testsuite/g++.dg/ext/is_convertible4.C b/gcc/testsuite/g++.dg/ext/is_convertible4.C new file mode 100644 index 00000000000..8a7724c5852 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_convertible4.C @@ -0,0 +1,33 @@ +// PR c++/107049 +// { dg-do compile { target c++11 } } +// Failed access check should be a substitution failure, not an error. + +template +struct bool_constant { static constexpr bool value = B; }; + +template +struct is_convertible +: public bool_constant<__is_convertible(From, To)> +{ }; + +#if __cpp_variable_templates +template +constexpr bool is_convertible_v = __is_convertible(From, To); +#endif + +class Private +{ + operator int() const + { + static_assert( not is_convertible::value, "" ); +#if __cpp_variable_templates + static_assert( not is_convertible_v, "" ); +#endif + return 0; + } +}; + +static_assert( not is_convertible::value, "" ); +#if __cpp_variable_templates +static_assert( not is_convertible_v, "" ); +#endif diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C new file mode 100644 index 00000000000..f81b5944ca2 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible4.C @@ -0,0 +1,33 @@ +// PR c++/107049 +// { dg-do compile { target c++11 } } +// Failed access check should be a substitution failure, not an error. + +template +struct bool_constant { static constexpr bool value = B; }; + +template +struct is_nt_convertible +: public bool_constant<__is_nothrow_convertible(From, To)> +{ }; + +#if __cpp_variable_templates +template +constexpr bool is_nt_convertible_v = __is_nothrow_convertible(From, To); +#endif + +class Private +{ + operator int() const + { + static_assert( not is_nt_convertible::value, "" ); +#if __cpp_variable_templates + static_assert( not is_nt_convertible_v, "" ); +#endif + return 0; + } +}; + +static_assert( not is_nt_convertible::value, "" ); +#if __cpp_variable_templates +static_assert( not is_nt_convertible_v, "" ); +#endif diff --git a/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc new file mode 100644 index 00000000000..04a8c525961 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/is_convertible/requirements/access.cc @@ -0,0 +1,18 @@ +// { dg-do compile { target c++11 } } +// PR c++/107049 + +#include + +class Private +{ + operator int() const + { + static_assert( not std::is_convertible::value, "" ); +#if __cpp_lib_type_trait_variable_templates + static_assert( not std::is_convertible_v, "" ); +#endif + return 0; + } +}; + +static_assert( not std::is_convertible::value, "" );