From df0e6509bf74421ea68a2e025300bcd6ca63722f Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 10 Dec 2024 00:56:13 +0100 Subject: [PATCH] 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. --- libstdc++-v3/include/bits/shared_ptr_base.h | 2 +- .../20_util/shared_ptr/requirements/1.cc | 33 +++++++++++++++++++ .../requirements/explicit_instantiation/1.cc | 12 +++++++ .../20_util/weak_ptr/requirements/1.cc | 33 +++++++++++++++++++ .../requirements/explicit_instantiation/1.cc | 12 +++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/20_util/shared_ptr/requirements/1.cc create mode 100644 libstdc++-v3/testsuite/20_util/weak_ptr/requirements/1.cc diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index 053857b4c29..3622e029117 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 00000000000..8ddb5d220ac --- /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 6418e0c4bc7..3bd05c36f8f 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 00000000000..04ea837d85a --- /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; + std::weak_ptr ptr2 = ptr; + +#if __cpp_lib_shared_ptr_arrays >= 201611L + std::weak_ptr ptr_array; + std::weak_ptr ptr_array2 = ptr_array; + std::weak_ptr ptr_array3 = ptr_array; +#endif +} + +void +test02() +{ + std::weak_ptr ptr; + std::weak_ptr ptr2 = ptr; + +#if __cpp_lib_shared_ptr_arrays >= 201611L + std::weak_ptr ptr_array; + std::weak_ptr ptr_array2 = ptr_array; + std::weak_ptr ptr_array3 = ptr_array; +#endif +} diff --git a/libstdc++-v3/testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc b/libstdc++-v3/testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc index b2704d659db..24d16ef0e56 100644 --- a/libstdc++-v3/testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc +++ b/libstdc++-v3/testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc @@ -28,3 +28,15 @@ template class std::weak_ptr; template class std::weak_ptr; template class std::weak_ptr; template class std::weak_ptr; + +#if __cpp_lib_shared_ptr_arrays >= 201611L +template class std::weak_ptr; +template class std::weak_ptr; +template class std::weak_ptr; +template class std::weak_ptr; + +template class std::weak_ptr; +template class std::weak_ptr; +template class std::weak_ptr; +template class std::weak_ptr; +#endif