From patchwork Tue Apr 26 10:52:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 53220 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB5A93858C2D for ; Tue, 26 Apr 2022 10:53:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB5A93858C2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1650970388; bh=bw2NGHJ0N+g9w2AsNJv9JqgZ5c2CfOTfdUONkajYST4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IBT+Rnf3ZBrIYjslFNvOyPBTrfWpvhhdB23oInUL+0yMJUcQNO6Qh9dklvT4s6/ON anENVtE2LrPmM4YKn9PB2dyt4vOKsO8gk0UM5o9dGMXUSzpCRyQ5rgXa5RJ7fFjfqa YJuxmAa0GD0BeO9ugm+pwzZXZXL8bVjd8muHVnic= 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 5F5153858D37 for ; Tue, 26 Apr 2022 10:52:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5F5153858D37 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-656-SDoZ8Xo-NWuxHMXrLlPxwQ-1; Tue, 26 Apr 2022 06:52:37 -0400 X-MC-Unique: SDoZ8Xo-NWuxHMXrLlPxwQ-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 986428811BB; Tue, 26 Apr 2022 10:52:36 +0000 (UTC) Received: from localhost (unknown [10.33.36.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 424EF40FF426; Tue, 26 Apr 2022 10:52:33 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Gate constexpr string and vector on constexpr destructor support Date: Tue, 26 Apr 2022 11:52:32 +0100 Message-Id: <20220426105232.2059737-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP 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: 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 Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" I was going to push this patch to "fix" our C++20 constexpr string and vector so they depend on __cpp_constexpr_dynamic_alloc. But then I realised that Clang supports that since 10.0.0 and I don't think we need to bother supporting anything older than that for C++20 mode (Clang 9 users can still use C++17 mode). So I'm just posting this here for the archives, but I don't plan to push it. We could probably remove the __cpp_constexpr_dynamic_alloc checks in std::allocator and std::unique_ptr now too. -- >8 -- Declaring a destructor as constexpr is only supported when the __cpp_constexpr_dynamic_alloc macro is defined. Introduce a new macro, _GLIBCXX_CONSTEXPR_DTOR, which can be used on destructors. Adjust the relevant feature test macros to also depend on __cpp_constexpr_dynamic_alloc. libstdc++-v3/ChangeLog: * include/bits/c++config (_GLIBCXX_CONSTEXPR_DTOR): Define new macro. (_GLIBCXX23_CONSTEXPR_DTOR): Likewise. * include/bits/allocator.h (allocator::~allocator()): Use new macro. * include/bits/basic_string.h (__cpp_lib_constexpr_string): Depend on __cpp_constexpr_dynamic_alloc. (basic_string::~basic_string()): Use new macro. * include/bits/stl_vector.h (__cpp_lib_constexpr_vector): Depend on __cpp_constexpr_dynamic_alloc. (_Vector_base::~_Vector_base(), vector::~vector()): Use new macro. * include/bits/unique_ptr.h (unique_ptr, unique_ptr): Likewise. * include/std/version (__cpp_lib_constexpr_string) (__cpp_lib_constexpr_vector): Depend on __cpp_constexpr_dynamic_alloc. --- libstdc++-v3/include/bits/allocator.h | 4 +--- libstdc++-v3/include/bits/basic_string.h | 11 ++++++++--- libstdc++-v3/include/bits/c++config | 12 ++++++++++++ libstdc++-v3/include/bits/stl_vector.h | 14 ++++++++------ libstdc++-v3/include/bits/unique_ptr.h | 8 ++------ libstdc++-v3/include/std/version | 6 ++++-- 6 files changed, 35 insertions(+), 20 deletions(-) diff --git a/libstdc++-v3/include/bits/allocator.h b/libstdc++-v3/include/bits/allocator.h index f7770165273..d94d93546c9 100644 --- a/libstdc++-v3/include/bits/allocator.h +++ b/libstdc++-v3/include/bits/allocator.h @@ -168,9 +168,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _GLIBCXX20_CONSTEXPR allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { } -#if __cpp_constexpr_dynamic_alloc - constexpr -#endif + _GLIBCXX_CONSTEXPR_DTOR ~allocator() _GLIBCXX_NOTHROW { } #if __cplusplus > 201703L diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index c3fbc53953c..e5ec7ea794e 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -56,9 +56,14 @@ namespace std _GLIBCXX_VISIBILITY(default) _GLIBCXX_BEGIN_NAMESPACE_VERSION _GLIBCXX_BEGIN_NAMESPACE_CXX11 -#ifdef __cpp_lib_is_constant_evaluated +#if __cpp_lib_is_constant_evaluated +# if __cpp_constexpr_dynamic_alloc // Support P0980R1 in C++20. -# define __cpp_lib_constexpr_string 201907L +# define __cpp_lib_constexpr_string 201907L +# else +// Support P1032R1 in C++20. +# define __cpp_lib_constexpr_string 201811L +# endif #elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED // Support P0426R1 changes to char_traits in C++17. # define __cpp_lib_constexpr_string 201611L @@ -790,7 +795,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 /** * @brief Destroy the string instance. */ - _GLIBCXX20_CONSTEXPR + _GLIBCXX_CONSTEXPR_DTOR ~basic_string() { _M_dispose(); } diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index 2798b9786dc..6b575505932 100644 --- a/libstdc++-v3/include/bits/c++config +++ b/libstdc++-v3/include/bits/c++config @@ -190,6 +190,18 @@ # endif #endif +#ifdef __cpp_constexpr_dynamic_alloc +# define _GLIBCXX_CONSTEXPR_DTOR constexpr +#else +# define _GLIBCXX_CONSTEXPR_DTOR +#endif + +# if __cplusplus >= 202100L +# define _GLIBCXX23_CONSTEXPR_DTOR _GLIBCXX_CONSTEXPR_DTOR +#else +# define _GLIBCXX23_CONSTEXPR_DTOR +#endif + #ifndef _GLIBCXX17_INLINE # if __cplusplus >= 201703L # define _GLIBCXX17_INLINE inline diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index b4ff3989a5d..a93018f8700 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -64,7 +64,9 @@ #endif #if __cplusplus >= 202002L # include -#define __cpp_lib_constexpr_vector 201907L +# ifdef __cpp_constexpr_dynamic_alloc +# define __cpp_lib_constexpr_vector 201907L +# endif #endif #include @@ -229,7 +231,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _S_on_dealloc(_M_impl); } - _GLIBCXX20_CONSTEXPR + _GLIBCXX_CONSTEXPR_DTOR ~_Reinit() { // Mark unused capacity as invalid after reallocation. @@ -254,7 +256,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER : _M_impl(__impl), _M_n(__n) { _S_grow(_M_impl, __n); } - _GLIBCXX20_CONSTEXPR + _GLIBCXX_CONSTEXPR_DTOR ~_Grow() { if (_M_n) _S_shrink(_M_impl, _M_n); } _GLIBCXX20_CONSTEXPR @@ -360,7 +362,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { } #endif - _GLIBCXX20_CONSTEXPR + _GLIBCXX_CONSTEXPR_DTOR ~_Vector_base() _GLIBCXX_NOEXCEPT { _M_deallocate(_M_impl._M_start, @@ -724,7 +726,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * not touched in any way. Managing the pointer is the user's * responsibility. */ - _GLIBCXX20_CONSTEXPR + _GLIBCXX_CONSTEXPR_DTOR ~vector() _GLIBCXX_NOEXCEPT { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, @@ -1831,7 +1833,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER std::forward<_Args>(__args)...); } - _GLIBCXX20_CONSTEXPR + _GLIBCXX_CONSTEXPR_DTOR ~_Temporary_value() { _Alloc_traits::destroy(_M_this->_M_impl, _M_ptr()); } diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index ad60fada59b..a3269ed4f8e 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -384,9 +384,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif /// Destructor, invokes the deleter if the stored pointer is not null. -#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc - constexpr -#endif + _GLIBCXX23_CONSTEXPR_DTOR ~unique_ptr() noexcept { static_assert(__is_invocable::value, @@ -659,9 +657,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { } /// Destructor, invokes the deleter if the stored pointer is not null. -#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc - constexpr -#endif + _GLIBCXX23_CONSTEXPR_DTOR ~unique_ptr() { auto& __ptr = _M_t._M_ptr(); diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index 22280e1a349..ecf4b0a6c4e 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -248,7 +248,7 @@ #define __cpp_lib_constexpr_memory 201811L #define __cpp_lib_constexpr_numeric 201911L #ifdef __cpp_lib_is_constant_evaluated -# if _GLIBCXX_USE_CXX11_ABI +# if _GLIBCXX_USE_CXX11_ABI && __cpp_constexpr_dynamic_alloc # define __cpp_lib_constexpr_string 201907L # else # define __cpp_lib_constexpr_string 201811L @@ -257,7 +257,9 @@ #define __cpp_lib_constexpr_string_view 201811L #define __cpp_lib_constexpr_tuple 201811L #define __cpp_lib_constexpr_utility 201811L -#define __cpp_lib_constexpr_vector 201907L +#if __cpp_constexpr_dynamic_alloc +# define __cpp_lib_constexpr_vector 201907L +#endif #define __cpp_lib_erase_if 202002L #define __cpp_lib_generic_unordered_lookup 201811L #define __cpp_lib_interpolate 201902L