[libcxx-commits] [libcxx] [libc++] Make sure LWG2070 is implemented as a DR (PR #65998)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 11 12:00:12 PDT 2023


https://github.com/ldionne created https://github.com/llvm/llvm-project/pull/65998:

When we implemented C++20's P0674R1, we didn't enable the part of P0674R1 that was resolving LWG2070 as a DR. This patch fixes that and makes sure that we consistently go through the allocator when constructing and destroying the underlying object in std::allocate_shared.

Fixes #54365.

>From 64cb84577b1f4c6c1da8b026339018a82c03a93a Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 11 Sep 2023 14:56:33 -0400
Subject: [PATCH] [libc++] Make sure LWG2070 is implemented as a DR

When we implemented C++20's P0674R1, we didn't enable the part of P0674R1
that was resolving LWG2070 as a DR. This patch fixes that and makes sure
that we consistently go through the allocator when constructing and
destroying the underlying object in std::allocate_shared.

Fixes #54365.
---
 libcxx/include/__memory/shared_ptr.h                      | 8 ++++++--
 ...onstruct.pass.cpp => allocate_shared.lwg2070.pass.cpp} | 8 +++++---
 2 files changed, 11 insertions(+), 5 deletions(-)
 rename libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/{allocate_shared_construct.pass.cpp => allocate_shared.lwg2070.pass.cpp} (92%)

diff --git a/libcxx/include/__memory/shared_ptr.h b/libcxx/include/__memory/shared_ptr.h
index abdf9aa274d4e42..f1c7723b4f15dd7 100644
--- a/libcxx/include/__memory/shared_ptr.h
+++ b/libcxx/include/__memory/shared_ptr.h
@@ -299,7 +299,9 @@ struct __shared_ptr_emplace
             allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
         }
 #else
-        ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
+        using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
+        _TpAlloc __tmp(*__get_alloc());
+        allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
 #endif
     }
 
@@ -320,7 +322,9 @@ struct __shared_ptr_emplace
             allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
         }
 #else
-        __get_elem()->~_Tp();
+        using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
+        _TpAlloc __tmp(*__get_alloc());
+        allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
 #endif
     }
 
diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_construct.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.lwg2070.pass.cpp
similarity index 92%
rename from libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_construct.pass.cpp
rename to libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.lwg2070.pass.cpp
index 4281cc1aa9e0db8..89b6cb6c11a361f 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_construct.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.lwg2070.pass.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: c++03, c++11, c++14
 
 // <memory>
 
@@ -15,8 +15,10 @@
 // template<class T, class A, class... Args>
 // shared_ptr<T> allocate_shared(const A& a, Args&&... args);
 
-// This test checks that allocator_traits::construct is used in allocate_shared
-// as requested in C++20 (via P0674R1).
+// This test checks that allocator_traits::construct and allocator_traits::destroy
+// are used in allocate_shared as requested for the resolution of LWG2070. Note
+// that LWG2070 was resolved by P0674R1 (which is a C++20 paper), but we implement
+// LWG issue resolutions as DRs per our policy.
 
 #include "test_macros.h"
 



More information about the libcxx-commits mailing list