From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683069020; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KwPXpPjGgkQIgRfMFE1gSydEVnuGm81vqBh6i+zbDiA=; b=FhR2DVo7sulXuU9sOWU7Z7tLSTpbl021DLOp12lY+B9EyEPLC5qS3ObuIgu5pL0fDRuOT2 SrV73RIepBPa2ux/ewy1JCFw4kiVo9FVJyXUkOAV9ZcGQe0Yeyq6XTgqHD8oDA88cu/tvl AKQEey4qP1YZNGw4XWix2j2yXO/+xMs= 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) From: Marek Polacek 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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)); base-commit: 33020780a9699f1146eeed61783cec89fde337a0 -- 2.40.1