[libcxx-commits] [PATCH] D109331: [libc++] Fix std::to_address(arr)

Arthur O'Dwyer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 6 11:37:27 PDT 2021


Quuxplusone created this revision.
Quuxplusone added reviewers: ldionne, zoecarver, libc++.
Quuxplusone added a project: libc++.
Quuxplusone requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added 1 blocking reviewer(s): libc++.

There were basically two bugs here:

- When C++20 `to_address` is called on `int arr[10]`, then `const _Ptr&` becomes a reference to a const array, and then we dispatch to `__to_address<const int(&)[10]>`, which, oops, gives us a `const int*` result instead of an `int*` result. Solution: We need to provide the two standard-specified overloads of `std::to_address` in exactly the same way that we provide two overloads of `__to_address`.
- When `__to_address` is called on a pointer type, `__to_address(const _Ptr&)` is disabled so we successfully avoid trying to instantiate `pointer_traits` of that pointer type. But when it's called on an array type, `__to_address(const _Ptr&)` is not disabled for array types, so we go ahead and instantiate `pointer_traits<int[10]>`, which goes boom. Solution: We need to disable `__to_address(const _Ptr&)` for both pointer //and// array types.

Maybe we should disable it for function types too? Those are supposed not to work, but it'd affect the wording of the error message we gave.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109331

Files:
  libcxx/include/__memory/pointer_traits.h
  libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp
  libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp


Index: libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp
===================================================================
--- libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp
+++ libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp
@@ -139,6 +139,14 @@
     assert(std::to_address(p8b) == p8_nil);
     ASSERT_SAME_TYPE(decltype(std::to_address(p8b)), decltype(p8_nil));
 
+    int p9[2] = {};
+    assert(std::to_address(p9) == p9);
+    ASSERT_SAME_TYPE(decltype(std::to_address(p9)), int*);
+
+    const int p10[2] = {};
+    assert(std::to_address(p10) == p10);
+    ASSERT_SAME_TYPE(decltype(std::to_address(p10)), const int*);
+
     return true;
 }
 
Index: libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp
===================================================================
--- libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp
+++ libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp
@@ -137,6 +137,14 @@
     assert(std::__to_address(p8b) == p8_nil);
     ASSERT_SAME_TYPE(decltype(std::__to_address(p8b)), decltype(p8_nil));
 
+    int p9[2] = {};
+    assert(std::__to_address(p9) == p9);
+    ASSERT_SAME_TYPE(decltype(std::__to_address(p9)), int*);
+
+    const int p10[2] = {};
+    assert(std::__to_address(p10) == p10);
+    ASSERT_SAME_TYPE(decltype(std::__to_address(p10)), const int*);
+
     return true;
 }
 
Index: libcxx/include/__memory/pointer_traits.h
===================================================================
--- libcxx/include/__memory/pointer_traits.h
+++ libcxx/include/__memory/pointer_traits.h
@@ -173,7 +173,7 @@
 }
 
 // enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
-template <class _Pointer, class = _EnableIf<!is_pointer<_Pointer>::value> >
+template <class _Pointer, class = _EnableIf<!is_pointer<_Pointer>::value && !is_array<_Pointer>::value> >
 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 typename decay<decltype(__to_address_helper<_Pointer>::__call(declval<const _Pointer&>()))>::type
 __to_address(const _Pointer& __p) _NOEXCEPT {
@@ -199,6 +199,12 @@
 };
 
 #if _LIBCPP_STD_VER > 17
+template <class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+auto to_address(_Tp *__p) noexcept {
+    return _VSTD::__to_address(__p);
+}
+
 template <class _Pointer>
 inline _LIBCPP_INLINE_VISIBILITY constexpr
 auto to_address(const _Pointer& __p) noexcept {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109331.370945.patch
Type: text/x-patch
Size: 2491 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210906/dddb255f/attachment-0001.bin>


More information about the libcxx-commits mailing list