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 DA6623888C5A for ; Wed, 23 Mar 2022 12:19:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DA6623888C5A Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-194-C3dCqcfVO7OE5yEuKLQe3A-1; Wed, 23 Mar 2022 08:19:27 -0400 X-MC-Unique: C3dCqcfVO7OE5yEuKLQe3A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 14281811E7A; Wed, 23 Mar 2022 12:19:27 +0000 (UTC) Received: from localhost (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id D140753D8; Wed, 23 Mar 2022 12:19:26 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add missing constraints to std::bit_cast [PR105027] Date: Wed, 23 Mar 2022 12:19:26 +0000 Message-Id: <20220323121926.933528-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 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.3 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Mar 2022 12:19:30 -0000 Tested powerpc64le-linux, pushed to trunk. Backport to gcc-11 to follow. -- >8 -- Our std::bit_cast was relying on the compiler to check for errors inside __builtin_bit_cast, instead of checking them as constraints. That means std::bit_cast was not SFINAE-friendly. This fix uses a requires-clause, so for old versions of Clang without concepts support the function will still be unconstrained. At some point in future we can remove the #ifdef __cpp_concepts check and rely on all compilers having full concepts support in C++20 mode. libstdc++-v3/ChangeLog: PR libstdc++/105027 * include/std/bit (bit_cast): Add constraints. * testsuite/26_numerics/bit/bit.cast/105027.cc: New test. --- libstdc++-v3/include/std/bit | 4 ++++ .../26_numerics/bit/bit.cast/105027.cc | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 libstdc++-v3/testsuite/26_numerics/bit/bit.cast/105027.cc diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit index cfd98e24eb5..a40f1ce99df 100644 --- a/libstdc++-v3/include/std/bit +++ b/libstdc++-v3/include/std/bit @@ -73,6 +73,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION [[nodiscard]] constexpr _To bit_cast(const _From& __from) noexcept +#ifdef __cpp_concepts + requires (sizeof(_To) == sizeof(_From)) + && __is_trivially_copyable(_To) && __is_trivially_copyable(_From) +#endif { return __builtin_bit_cast(_To, __from); } diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/105027.cc b/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/105027.cc new file mode 100644 index 00000000000..301d94ec575 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/105027.cc @@ -0,0 +1,18 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +// PR libstdc++/105027 - Missing constraints on std::bit_cast + +#include + +template +concept BitCastable = requires(const U& u) { std::bit_cast(u); }; + +static_assert(BitCastable); // OK + +static_assert(!BitCastable); // #1: different size + +struct A { A(A const&); int i; }; +static_assert(!BitCastable); // #2: not trivially copyable + +static_assert(!BitCastable); // #3: sizeof(int()) is ill-formed -- 2.34.1