[libcxx-commits] [libcxx] 347f69c - [libc++] Revert the std::to_address change to avoid relying on element_type.

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 4 15:50:11 PDT 2021


Author: Louis Dionne
Date: 2021-05-04T18:50:05-04:00
New Revision: 347f69c55f45acd80070f522bed7417d402f84b0

URL: https://github.com/llvm/llvm-project/commit/347f69c55f45acd80070f522bed7417d402f84b0
DIFF: https://github.com/llvm/llvm-project/commit/347f69c55f45acd80070f522bed7417d402f84b0.diff

LOG: [libc++] Revert the std::to_address change to avoid relying on element_type.

This reverts commit da456167, which broke the Clang build. I'm able to
reproduce it but I want to give myself a bit more time to investigate.

Differential Revision: https://reviews.llvm.org/D101638

Added: 
    

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

Removed: 
    libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp
    libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
    libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp


################################################################################
diff  --git a/libcxx/include/__memory/pointer_traits.h b/libcxx/include/__memory/pointer_traits.h
index e67e77b90a60f..439c658b50766 100644
--- a/libcxx/include/__memory/pointer_traits.h
+++ b/libcxx/include/__memory/pointer_traits.h
@@ -164,12 +164,11 @@ struct __rebind_pointer {
 
 // to_address
 
-template <bool _UsePointerTraits>
-struct __to_address_helper {
+template <bool _UsePointerTraits> struct __to_address_helper;
+
+template <> struct __to_address_helper<true> {
     template <class _Pointer>
-    using __return_type = typename decay<
-        decltype(pointer_traits<_Pointer>::to_address(declval<const _Pointer&>()))
-    >::type;
+    using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
 
     template <class _Pointer>
     _LIBCPP_CONSTEXPR
@@ -199,9 +198,7 @@ __to_address(const _Pointer& __p) _NOEXCEPT
 
 template <> struct __to_address_helper<false> {
     template <class _Pointer>
-    using __return_type = typename decay<
-        decltype(_VSTD::__to_address(declval<const _Pointer&>().operator->()))
-    >::type;
+    using __return_type = typename pointer_traits<_Pointer>::element_type*;
 
     template <class _Pointer>
     _LIBCPP_CONSTEXPR
@@ -209,6 +206,7 @@ template <> struct __to_address_helper<false> {
     __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
 };
 
+
 #if _LIBCPP_STD_VER > 17
 template <class _Tp>
 inline _LIBCPP_INLINE_VISIBILITY constexpr

diff  --git a/libcxx/include/iterator b/libcxx/include/iterator
index 95a39e7f7ec8c..97677feee030e 100644
--- a/libcxx/include/iterator
+++ b/libcxx/include/iterator
@@ -1352,7 +1352,7 @@ public:
         _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
                        "Attempted to dereference a non-dereferenceable iterator");
 #endif
-        return _VSTD::__to_address(__i);
+        return (pointer)_VSTD::addressof(*__i);
     }
     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
     {

diff  --git a/libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp b/libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp
deleted file mode 100644
index c3c1528ed9fe1..0000000000000
--- a/libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address.pass.cpp
+++ /dev/null
@@ -1,97 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <memory>
-
-// template <class T> constexpr T* __to_address(T* p) noexcept;
-// template <class Ptr> constexpr auto __to_address(const Ptr& p) noexcept;
-
-#include <memory>
-#include <cassert>
-#include "test_macros.h"
-
-struct Irrelevant {};
-
-struct P1 {
-    using element_type = Irrelevant;
-    TEST_CONSTEXPR explicit P1(int *p) : p_(p) { }
-    TEST_CONSTEXPR int *operator->() const { return p_; }
-    int *p_;
-};
-
-struct P2 {
-    using element_type = Irrelevant;
-    TEST_CONSTEXPR explicit P2(int *p) : p_(p) { }
-    TEST_CONSTEXPR P1 operator->() const { return p_; }
-    P1 p_;
-};
-
-struct P3 {
-    TEST_CONSTEXPR explicit P3(int *p) : p_(p) { }
-    int *p_;
-};
-
-template<>
-struct std::pointer_traits<P3> {
-    static TEST_CONSTEXPR int *to_address(const P3& p) { return p.p_; }
-};
-
-struct P4 {
-    TEST_CONSTEXPR explicit P4(int *p) : p_(p) { }
-    int *operator->() const;  // should never be called
-    int *p_;
-};
-
-template<>
-struct std::pointer_traits<P4> {
-    static TEST_CONSTEXPR int *to_address(const P4& p) { return p.p_; }
-};
-
-struct P5 {
-    using element_type = Irrelevant;
-    int const* const& operator->() const;
-};
-
-struct P6 {};
-
-template<>
-struct std::pointer_traits<P6> {
-    static int const* const& to_address(const P6&);
-};
-
-TEST_CONSTEXPR_CXX14 bool test() {
-    int i = 0;
-    ASSERT_NOEXCEPT(std::__to_address(&i));
-    assert(std::__to_address(&i) == &i);
-    P1 p1(&i);
-    ASSERT_NOEXCEPT(std::__to_address(p1));
-    assert(std::__to_address(p1) == &i);
-    P2 p2(&i);
-    ASSERT_NOEXCEPT(std::__to_address(p2));
-    assert(std::__to_address(p2) == &i);
-    P3 p3(&i);
-    ASSERT_NOEXCEPT(std::__to_address(p3));
-    assert(std::__to_address(p3) == &i);
-    P4 p4(&i);
-    ASSERT_NOEXCEPT(std::__to_address(p4));
-    assert(std::__to_address(p4) == &i);
-
-    ASSERT_SAME_TYPE(decltype(std::__to_address(std::declval<int const*>())), int const*);
-    ASSERT_SAME_TYPE(decltype(std::__to_address(std::declval<P5>())), int const*);
-    ASSERT_SAME_TYPE(decltype(std::__to_address(std::declval<P6>())), int const*);
-
-    return true;
-}
-
-int main(int, char**) {
-    test();
-#if TEST_STD_VER >= 14
-    static_assert(test(), "");
-#endif
-    return 0;
-}

diff  --git a/libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp b/libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
deleted file mode 100644
index 5eed12d19c072..0000000000000
--- a/libcxx/test/libcxx/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <memory>
-
-// template <class T> constexpr T* __to_address(T* p) noexcept;
-// template <class Ptr> constexpr auto __to_address(const Ptr& p) noexcept;
-
-#include <memory>
-
-#include <array>
-#include <cassert>
-#include <span>
-#include <string>
-#include <string_view>
-#include <valarray>
-#include <vector>
-#include "test_macros.h"
-
-template<class C>
-void test_container_iterators(C c)
-{
-    const C& cc = c;
-    assert(std::__to_address(c.begin()) == c.data());
-    assert(std::__to_address(c.end()) == c.data() + c.size());
-    assert(std::__to_address(cc.begin()) == cc.data());
-    assert(std::__to_address(cc.end()) == cc.data() + cc.size());
-}
-
-void test_valarray_iterators()
-{
-    std::valarray<int> v(100);
-    int *p = std::__to_address(std::begin(v));
-    int *q = std::__to_address(std::end(v));
-    assert(q - p == 100);
-}
-
-int main(int, char**) {
-    test_container_iterators(std::array<int, 3>());
-    test_container_iterators(std::vector<int>(3));
-    test_container_iterators(std::string("abc"));
-#if TEST_STD_VER >= 17
-    test_container_iterators(std::string_view("abc"));
-#endif
-#if TEST_STD_VER >= 20
-    test_container_iterators(std::span<const char>("abc"));
-#endif
-    test_valarray_iterators();
-
-    return 0;
-}

diff  --git a/libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp b/libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp
index 841b10c594ea7..26eba0e06cffe 100644
--- a/libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp
+++ b/libcxx/test/std/utilities/memory/pointer.conversion/to_address.pass.cpp
@@ -17,81 +17,110 @@
 #include <cassert>
 #include "test_macros.h"
 
-struct Irrelevant {};
+class P1
+{
+public:
+    using element_type = int;
 
-struct P1 {
-    using element_type = Irrelevant;
-    constexpr explicit P1(int *p) : p_(p) { }
-    constexpr int *operator->() const { return p_; }
-    int *p_;
+    constexpr explicit P1(int* p)
+    : p_(p) { }
+
+    constexpr int* operator->() const noexcept
+    { return p_; }
+
+private:
+    int* p_;
 };
 
-struct P2 {
-    using element_type = Irrelevant;
-    constexpr explicit P2(int *p) : p_(p) { }
-    constexpr P1 operator->() const { return p_; }
+class P2
+{
+public:
+    using element_type = int;
+
+    constexpr explicit P2(int* p)
+    : p_(p) { }
+
+    constexpr P1 operator->() const noexcept
+    { return p_; }
+
+private:
     P1 p_;
 };
 
-struct P3 {
-    constexpr explicit P3(int *p) : p_(p) { }
-    int *p_;
-};
+class P3
+{
+public:
+    constexpr explicit P3(int* p)
+    : p_(p) { }
 
-template<>
-struct std::pointer_traits<P3> {
-    static constexpr int *to_address(const P3& p) { return p.p_; }
-};
+    constexpr int* get() const noexcept
+    { return p_; }
 
-struct P4 {
-    constexpr explicit P4(int *p) : p_(p) { }
-    int *operator->() const;  // should never be called
-    int *p_;
+private:
+    int* p_;
 };
 
+namespace std
+{
 template<>
-struct std::pointer_traits<P4> {
-    static constexpr int *to_address(const P4& p) { return p.p_; }
+struct pointer_traits<::P3>
+{
+    static constexpr int* to_address(const ::P3& p) noexcept
+    { return p.get(); }
 };
+}
 
-struct P5 {
-    using element_type = Irrelevant;
-    int const* const& operator->() const;
-};
+class P4
+{
+public:
+    constexpr explicit P4(int* p)
+    : p_(p) { }
 
-struct P6 {};
+    constexpr int* operator->() const noexcept
+    { return nullptr; }
 
+    constexpr int* get() const noexcept
+    { return p_; }
+
+private:
+    int* p_;
+};
+
+namespace std
+{
 template<>
-struct std::pointer_traits<P6> {
-    static int const* const& to_address(const P6&);
+struct pointer_traits<::P4>
+{
+    constexpr static int* to_address(const ::P4& p) noexcept
+    { return p.get(); }
 };
+}
+
+int n = 0;
+static_assert(std::to_address(&n) == &n);
 
 constexpr bool test() {
-    int i = 0;
-    ASSERT_NOEXCEPT(std::to_address(&i));
-    assert(std::to_address(&i) == &i);
-    P1 p1(&i);
-    ASSERT_NOEXCEPT(std::to_address(p1));
-    assert(std::to_address(p1) == &i);
-    P2 p2(&i);
-    ASSERT_NOEXCEPT(std::to_address(p2));
-    assert(std::to_address(p2) == &i);
-    P3 p3(&i);
-    ASSERT_NOEXCEPT(std::to_address(p3));
-    assert(std::to_address(p3) == &i);
-    P4 p4(&i);
-    ASSERT_NOEXCEPT(std::to_address(p4));
-    assert(std::to_address(p4) == &i);
-
-    ASSERT_SAME_TYPE(decltype(std::to_address(std::declval<int const*>())), int const*);
-    ASSERT_SAME_TYPE(decltype(std::to_address(std::declval<P5>())), int const*);
-    ASSERT_SAME_TYPE(decltype(std::to_address(std::declval<P6>())), int const*);
-
-    return true;
+  int i = 0;
+  ASSERT_NOEXCEPT(std::to_address(&i));
+  assert(std::to_address(&i) == &i);
+  P1 p1(&i);
+  ASSERT_NOEXCEPT(std::to_address(p1));
+  assert(std::to_address(p1) == &i);
+  P2 p2(&i);
+  ASSERT_NOEXCEPT(std::to_address(p2));
+  assert(std::to_address(p2) == &i);
+  P3 p3(&i);
+  ASSERT_NOEXCEPT(std::to_address(p3));
+  assert(std::to_address(p3) == &i);
+  P4 p4(&i);
+  ASSERT_NOEXCEPT(std::to_address(p4));
+  assert(std::to_address(p4) == &i);
+
+  return true;
 }
 
 int main(int, char**) {
-    test();
-    static_assert(test());
-    return 0;
+  test();
+  static_assert(test());
+  return 0;
 }

diff  --git a/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp b/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
deleted file mode 100644
index 45d034c5c232d..0000000000000
--- a/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
+++ /dev/null
@@ -1,58 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <memory>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17
-
-// TODO: We should enable this test in Debug mode once we fix __wrap_iter
-//       to be a proper contiguous_iterator.
-// UNSUPPORTED: LIBCXX-DEBUG-FIXME
-
-// template <class T> constexpr T* to_address(T* p) noexcept;
-// template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept;
-
-#include <memory>
-
-#include <array>
-#include <cassert>
-#include <span>
-#include <string>
-#include <string_view>
-#include <valarray>
-#include <vector>
-#include "test_macros.h"
-
-template<class C>
-void test_container_iterators(C c)
-{
-    const C& cc = c;
-    assert(std::to_address(c.begin()) == c.data());
-    assert(std::to_address(c.end()) == c.data() + c.size());
-    assert(std::to_address(cc.begin()) == cc.data());
-    assert(std::to_address(cc.end()) == cc.data() + cc.size());
-}
-
-void test_valarray_iterators()
-{
-    std::valarray<int> v(100);
-    int *p = std::to_address(std::begin(v));
-    int *q = std::to_address(std::end(v));
-    assert(q - p == 100);
-}
-
-int main(int, char**) {
-    test_container_iterators(std::array<int, 3>());
-    test_container_iterators(std::vector<int>(3));
-    test_container_iterators(std::string("abc"));
-    test_container_iterators(std::string_view("abc"));
-    test_container_iterators(std::span<const char>("abc"));
-    test_valarray_iterators();
-
-    return 0;
-}


        


More information about the libcxx-commits mailing list