From patchwork Tue May 2 23:10:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1776049 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=QzQW0Hvd; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q9wmN4pKVz1ydX for ; Wed, 3 May 2023 09:10:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1BB713858C30 for ; Tue, 2 May 2023 23:10:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BB713858C30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683069041; bh=KwPXpPjGgkQIgRfMFE1gSydEVnuGm81vqBh6i+zbDiA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=QzQW0Hvd3Px2X15zEmNJb/6AxneB94n0X2DPtiWZj3Ph1e2ecdljowEIEAVVNE/bB ews3GSgpL2A/46VGaV88xdUfYjvGz1HGja6bd2Mkzz/Q9NKgFIhGFkSRtJsIt0yYN4 MvQB08T8CKgIC8kBZ5lW1+HMyebgJ+SkIgmgmLHQ= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 5A1EB3858D20 for ; Tue, 2 May 2023 23:10:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A1EB3858D20 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-602-Z92XJMgWOTS8H1SIjw8awg-1; Tue, 02 May 2023 19:10:18 -0400 X-MC-Unique: Z92XJMgWOTS8H1SIjw8awg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 39A303C0ED44 for ; Tue, 2 May 2023 23:10:18 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.17.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1BAA640BC798; Tue, 2 May 2023 23:10:18 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: wrong std::is_convertible with cv-qual fn [PR109680] Date: Tue, 2 May 2023 19:10:15 -0400 Message-Id: <20230502231015.56181-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This PR points out that std::is_convertible has given the wrong answer in static_assert (!std::is_convertible_v , ""); since r13-2822 implemented __is_{,nothrow_}convertible. std::is_convertible uses the imaginary To test() { return std::declval(); } to do its job. Here, From is 'int () const'. std::declval is defined as: template typename std::add_rvalue_reference::type declval() noexcept; std::add_rvalue_reference is defined as "If T is a function type that has no cv- or ref- qualifier or an object type, provides a member typedef type which is T&&, otherwise type is T." In our case, T is cv-qualified, so the result is T, so we end up with int () const declval() noexcept; which is invalid. In other words, this is pretty much like: using T = int () const; T fn1(); // bad, fn returning a fn T& fn2(); // bad, cannot declare reference to qualified function type T* fn3(); // bad, cannot declare pointer to qualified function type using U = int (); U fn4(); // bad, fn returning a fn U& fn5(); // OK U* fn6(); // OK I think is_convertible_helper needs to simulate std::declval better. I wouldn't be surprised if other type traits needed a similar fix. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/13? I've tested the new test with G++12 and clang++ as well (with std::is_convertible). PR c++/109680 gcc/cp/ChangeLog: * method.cc (is_convertible_helper): Correct simulating std::declval. gcc/testsuite/ChangeLog: * g++.dg/ext/is_convertible6.C: New test. --- gcc/cp/method.cc | 20 ++++++++++++++++++++ gcc/testsuite/g++.dg/ext/is_convertible6.C | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible6.C base-commit: 33020780a9699f1146eeed61783cec89fde337a0 diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index 00eae56eb5b..38eb7520312 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -2245,6 +2245,26 @@ is_convertible_helper (tree from, tree to) { if (VOID_TYPE_P (from) && VOID_TYPE_P (to)) return integer_one_node; + /* std::is_{,nothrow_}convertible test whether the imaginary function + definition + + To test() { return std::declval(); } + + is well-formed. A function can't return a function... */ + if (FUNC_OR_METHOD_TYPE_P (to) + /* ...neither can From be a function with cv-/ref-qualifiers: + std::declval is defined as + + template + typename std::add_rvalue_reference::type declval() noexcept; + + and std::add_rvalue_reference yields T when T is a function with + cv- or ref-qualifiers, making the definition ill-formed. + ??? Should we check this in other uses of build_stub_object too? */ + || (FUNC_OR_METHOD_TYPE_P (from) + && (type_memfn_quals (from) != TYPE_UNQUALIFIED + || type_memfn_rqual (from) != REF_QUAL_NONE))) + return error_mark_node; cp_unevaluated u; tree expr = build_stub_object (from); deferring_access_check_sentinel acs (dk_no_deferred); diff --git a/gcc/testsuite/g++.dg/ext/is_convertible6.C b/gcc/testsuite/g++.dg/ext/is_convertible6.C new file mode 100644 index 00000000000..180582663e8 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_convertible6.C @@ -0,0 +1,16 @@ +// PR c++/109680 +// { dg-do compile { target c++11 } } + +#define SA(X) static_assert((X),#X) + +SA(!__is_convertible(int () const, int (*)())); +SA(!__is_convertible(int (*)(), int () const)); + +SA( __is_convertible(int (), int (*)())); +SA(!__is_convertible(int (*)(), int ())); + +SA( __is_convertible(int (int), int (*) (int))); +SA(!__is_convertible(int (*) (int), int (int))); + +SA(!__is_convertible(int (int) const, int (*) (int))); +SA(!__is_convertible(int (*) (int), int (int) const));