[gcc r15-8048] libstdc++: fix compile error when converting std::weak_ptr
https://gcc.gnu.org/g:df0e6509bf74421ea68a2e025300bcd6ca63722f commit r15-8048-gdf0e6509bf74421ea68a2e025300bcd6ca63722f Author: Giuseppe D'Angelo Date: Tue Dec 10 00:56:13 2024 +0100 libstdc++: fix compile error when converting std::weak_ptr A std::weak_ptr can be converted to a compatible std::weak_ptr. This is implemented by having suitable converting constructors to std::weak_ptr which dispatch to the __weak_ptr base class (implementation detail). In __weak_ptr, lock() is supposed to return a __shared_ptr, not a __shared_ptr (that is, __shared_ptr). Unfortunately the return type of lock() and the type of the returned __shared_ptr were mismatching and that was causing a compile error: when converting a __weak_ptr to a __weak_ptr through __weak_ptr's converting constructor, the code calls lock(), and that simply fails to build. Fix it by removing the usage of element_type inside lock(), and using _Tp instead. Note that std::weak_ptr::lock() itself was already correct; the one in __weak_ptr was faulty (and that is the one called by __weak_ptr's converting constructors). libstdc++-v3/ChangeLog: * include/bits/shared_ptr_base.h (lock): Fixed a compile error when calling lock() on a weak_ptr, by removing an erroneous usage of element_type from within lock(). * testsuite/20_util/shared_ptr/requirements/explicit_instantiation/1.cc: Add more tests for array types. * testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc: Likewise. * testsuite/20_util/shared_ptr/requirements/1.cc: New test. * testsuite/20_util/weak_ptr/requirements/1.cc: New test. Diff: --- libstdc++-v3/include/bits/shared_ptr_base.h| 2 +- .../testsuite/20_util/shared_ptr/requirements/1.cc | 33 ++ .../requirements/explicit_instantiation/1.cc | 12 .../testsuite/20_util/weak_ptr/requirements/1.cc | 33 ++ .../requirements/explicit_instantiation/1.cc | 12 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index 053857b4c29f..3622e0291178 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -2075,7 +2075,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __shared_ptr<_Tp, _Lp> lock() const noexcept - { return __shared_ptr(*this, std::nothrow); } + { return __shared_ptr<_Tp, _Lp>(*this, std::nothrow); } long use_count() const noexcept diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/requirements/1.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/requirements/1.cc new file mode 100644 index ..8ddb5d220ac7 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/requirements/1.cc @@ -0,0 +1,33 @@ +// { dg-do compile { target c++11 } } +// { dg-require-effective-target hosted } + +#include +#include + +using namespace __gnu_test; + +void +test01() +{ + std::shared_ptr ptr; + std::shared_ptr ptr2 = ptr; + +#if __cpp_lib_shared_ptr_arrays >= 201611L + std::shared_ptr ptr_array; + std::shared_ptr ptr_array2 = ptr_array; + std::shared_ptr ptr_array3 = ptr_array; +#endif +} + +void +test02() +{ + std::shared_ptr ptr; + std::shared_ptr ptr2 = ptr; + +#if __cpp_lib_shared_ptr_arrays >= 201611L + std::shared_ptr ptr_array; + std::shared_ptr ptr_array2 = ptr_array; + std::shared_ptr ptr_array3 = ptr_array; +#endif +} diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/requirements/explicit_instantiation/1.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/requirements/explicit_instantiation/1.cc index 6418e0c4bc7c..3bd05c36f8fc 100644 --- a/libstdc++-v3/testsuite/20_util/shared_ptr/requirements/explicit_instantiation/1.cc +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/requirements/explicit_instantiation/1.cc @@ -28,3 +28,15 @@ template class std::shared_ptr; template class std::shared_ptr; template class std::shared_ptr; template class std::shared_ptr; + +#if __cpp_lib_shared_ptr_arrays >= 201611L +template class std::shared_ptr; +template class std::shared_ptr; +template class std::shared_ptr; +template class std::shared_ptr; + +template class std::shared_ptr; +template class std::shared_ptr; +template class std::shared_ptr; +template class std::shared_ptr; +#endif diff --git a/libstdc++-v3/testsuite/20_util/weak_ptr/requirements/1.cc b/libstdc++-v3/testsuite/20_util/weak_ptr/requirements/1.cc new file mode 100644 index ..04ea837d85a7 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/weak_ptr/requirements/1.cc @@ -0,0 +1,33 @@ +// { dg-do compile { target c++11 } } +// { dg-require-effective-target hosted } + +#include +#include + +using namespace __gnu_test; + +void +test01() +{ + std::weak_ptr ptr; +
[gcc r15-7746] MAINTAINERS: add myself to write after approval and DCO
https://gcc.gnu.org/g:a3f77f2528b9383c70f0361e0f3863cee58e9648 commit r15-7746-ga3f77f2528b9383c70f0361e0f3863cee58e9648 Author: Giuseppe D'Angelo Date: Fri Feb 28 08:37:25 2025 +0100 MAINTAINERS: add myself to write after approval and DCO ChangeLog: * MAINTAINERS: Added myself as write after approval and DCO. Diff: --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index c423dd6e7874..193cd802a078 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -428,6 +428,7 @@ Ludovic Courtès ludo Cary Coutant- Lawrence Crowl crowl Lili Cui- +Giuseppe D'Angelo peppe Palmer Dabbelt palmer Ian Dalliandall David Daney daney @@ -929,6 +930,7 @@ information. Soumya AR Juergen Christ +Giuseppe D'Angelo Robin Dapp Robin Dapp Aldy Hernandez
[gcc r15-7908] libstdc++: constrain std::atomic's default constructor
https://gcc.gnu.org/g:613f8ddbe3d7da63d827e588bf0333813c184b8a commit r15-7908-g613f8ddbe3d7da63d827e588bf0333813c184b8a Author: Giuseppe D'Angelo Date: Sat Sep 21 10:36:20 2024 +0200 libstdc++: constrain std::atomic's default constructor This commit implements the proposed resolution to LWG4169, which is to constrain std::atomic's default constructor based on whether T itself is default constructible. At the moment, std::atomic's primary template in libstdc++ has a defaulted default constructor. Value-initialization of the T member (since C++20 / P0883R2) is done via a NSDMI (= T()). GCC already considers the defaulted constructor constrained/deleted, however this behavior is non-standard (see the discussion in PR116769): the presence of a NSDMI should not make the constructor unavailable to overload resolution/deleted ([class.default.ctor]/2.5 does not apply). When using libstdc++ on Clang, this causes build issues as the constructor is *not* deleted there -- the interpretation of [class.default.ctor]/4 seems to match Clang's behavior. Therefore, although there would be "nothing to do" with GCC+libstdc++, this commit changes the code as to stop relying on the GCC language extension. In C++ >= 20 modes, std::atomic's defaulted default constructor is changed to be a non-defaulted one, with a constraint added as per LWG4169; value-initialization of the data member is moved from the NSDMI to the member init list. The new signature matches the one in the Standard as per [atomics.types.operations]/1. In pre-C++20 modes, the constructor is left defaulted. This ensures compatibility with C++11/14/17 behavior. In other words: we are not backporting P0883R2 to earlier language modes here. Amend an existing test to check that a std::atomic wrapping a non-default constructible type is always non-default constructible: from C++20, because of the constraint; before C++20, because we are removing the NSDMI, and therefore [class.default.ctor]/2.5 applies. Add another test that checks that std::atomic is trivially default constructible in pre-C++20 modes, and it isn't afterwards. libstdc++-v3/ChangeLog: * include/bits/version.def (atomic_value_initialization): Guard the FTM with the language concepts FTM. * include/bits/version.h: Regenerate. * include/std/atomic (atomic): When atomic value init is defined, change the defaulted default constructor to a non-defaulted one, constraining it as per LWG4169. Otherwise, keep the existing constructor. Remove the NSDMI for the _M_i member. (_GLIBCXX20_INIT): Drop the macro, as it is not needed any more. * testsuite/29_atomics/atomic/69301.cc: Test that an atomic wrapping a non-default-constructible type is always itself non-default-constructible (in all language modes). * testsuite/29_atomics/atomic/cons/trivial.cc: New test. Diff: --- libstdc++-v3/include/bits/version.def | 1 + libstdc++-v3/include/bits/version.h| 2 +- libstdc++-v3/include/std/atomic| 22 ++-- libstdc++-v3/testsuite/29_atomics/atomic/69301.cc | 2 ++ .../testsuite/29_atomics/atomic/cons/trivial.cc| 41 ++ 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index 2af5a54bff28..56638759539b 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -770,6 +770,7 @@ ftms = { values = { v = 201911; cxxmin = 20; +extra_cond = "__cpp_concepts >= 201907L"; }; }; diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 9833023cfdc8..29e1535298cb 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -856,7 +856,7 @@ #undef __glibcxx_want_atomic_ref #if !defined(__cpp_lib_atomic_value_initialization) -# if (__cplusplus >= 202002L) +# if (__cplusplus >= 202002L) && (__cpp_concepts >= 201907L) # define __glibcxx_atomic_value_initialization 201911L # if defined(__glibcxx_want_all) || defined(__glibcxx_want_atomic_value_initialization) # define __cpp_lib_atomic_value_initialization 201911L diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic index cd08df34ba77..9b1aca0fc09a 100644 --- a/libstdc++-v3/include/std/atomic +++ b/libstdc++-v3/include/std/atomic @@ -51,6 +51,7 @@ #include #include +#include namespace std _GLIBCXX_VISIBILITY(default) { @@ -189,14 +190,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif // __cpp_lib_atomic_wait }; -/// @cond undocumented -#if __cpp_lib_atomic_value_initialization -# define _GLIBCXX20_
[gcc r15-7838] libstdc++: use if consteval in stable_sort
https://gcc.gnu.org/g:24ea4539300d4926d9f073822e68f0d2f369452d commit r15-7838-g24ea4539300d4926d9f073822e68f0d2f369452d Author: Giuseppe D'Angelo Date: Wed Mar 5 14:34:41 2025 +0100 libstdc++: use if consteval in stable_sort This is a C++ >= 26 codepath for supporting constexpr stable_sort, so we know that we have if consteval available; it just needs protection with the feature-testing macro. Also merge the return in the same statement. Amends r15-7708-gff43f9853d3b10. libstdc++-v3/ChangeLog: * include/bits/stl_algo.h (__stable_sort): Use if consteval instead of is_constant_evaluated. Reviewed-by: Jonathan Wakely Diff: --- libstdc++-v3/include/bits/stl_algo.h | 10 +- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 41b4d0853b71..c3fea76014cb 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -4987,11 +4987,11 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO return; #if _GLIBCXX_HOSTED - if (__is_constant_evaluated()) - { - std::__inplace_stable_sort(__first, __last, __comp); - return; - } +# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26 + if consteval { + return std::__inplace_stable_sort(__first, __last, __comp); + } +# endif typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf; // __stable_sort_adaptive sorts the range in two halves,
[gcc r15-8970] libstdc++: add constexpr stable_partition
https://gcc.gnu.org/g:aba3018af8e025a62a87c704ccad6714b13bc811 commit r15-8970-gaba3018af8e025a62a87c704ccad6714b13bc811 Author: Giuseppe D'Angelo Date: Sat Mar 15 00:15:36 2025 +0100 libstdc++: add constexpr stable_partition This completes the implementation of P2562R1 for C++26. Unlike the other constexpr algorithms of the same family, stable_partition does not have a constexpr-friendly version "ready to use" during constant evaluation. In fact, it is not even available on freestanding, because it always allocates a temporary memory buffer. This commit implements the simplest possible strategy: during constant evaluation allocate a buffer of length 1 on the stack, and use that as a working area. libstdc++-v3/ChangeLog: * include/bits/algorithmfwd.h (stable_partition): Mark it as constexpr for C++26. * include/bits/ranges_algo.h (__stable_partition_fn): Likewise. * include/bits/stl_algo.h (stable_partition): Mark it as constexpr for C++26; during constant evaluation use a new codepath where a temporary buffer of 1 element is used. * testsuite/25_algorithms/headers/algorithm/synopsis.cc (stable_partition): Add constexpr. * testsuite/25_algorithms/stable_partition/constexpr.cc: New test. Diff: --- libstdc++-v3/include/bits/algorithmfwd.h | 1 + libstdc++-v3/include/bits/ranges_algo.h| 2 + libstdc++-v3/include/bits/stl_algo.h | 21 ++- .../25_algorithms/headers/algorithm/synopsis.cc| 1 + .../25_algorithms/stable_partition/constexpr.cc| 44 ++ 5 files changed, 67 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/algorithmfwd.h b/libstdc++-v3/include/bits/algorithmfwd.h index 05894b580028..a727b2783814 100644 --- a/libstdc++-v3/include/bits/algorithmfwd.h +++ b/libstdc++-v3/include/bits/algorithmfwd.h @@ -649,6 +649,7 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) #if _GLIBCXX_HOSTED template +_GLIBCXX26_CONSTEXPR _BIter stable_partition(_BIter, _BIter, _Predicate); #endif diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index 2814d90061cc..f36e7dd59911 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -2389,6 +2389,7 @@ namespace ranges typename _Proj = identity, indirect_unary_predicate> _Pred> requires permutable<_Iter> + _GLIBCXX26_CONSTEXPR subrange<_Iter> operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const @@ -2404,6 +2405,7 @@ namespace ranges indirect_unary_predicate, _Proj>> _Pred> requires permutable> + _GLIBCXX26_CONSTEXPR borrowed_subrange_t<_Range> operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const { diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index bb7dbfbd8e04..71ead103d2bf 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -1447,6 +1447,7 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) /// move-assign an element onto itself. template +_GLIBCXX26_CONSTEXPR _ForwardIterator __stable_partition_adaptive(_ForwardIterator __first, _ForwardIterator __last, @@ -1507,6 +1508,7 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) } template +_GLIBCXX26_CONSTEXPR _ForwardIterator __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) @@ -1521,11 +1523,25 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType; + const _DistanceType __len = std::distance(__first, __last); + +#if __glibcxx_constexpr_algorithms >= 202306L // >= C++26 + if consteval { + // Simulate a _Temporary_buffer of length 1: + _ValueType __buf = std::move(*__first); + *__first = std::move(__buf); + return std::__stable_partition_adaptive(__first, __last, __pred, + __len, + &__buf, + _DistanceType(1)); + } +#endif + _Temporary_buffer<_ForwardIterator, _ValueType> - __buf(__first, std::distance(__first, __last)); + __buf(__first, __len); return std::__stable_partition_adaptive(__first, __last, __pred, -_DistanceType(__buf.requested_size()), +__len, __buf.begin(), _DistanceType(__buf.size())); } @@ -1548,6 +1564,7 @@ _GLIBCXX_END_