[libcxx-commits] [libcxx] 813e1da - [libc++] implement move_iterator<T*> should be a random access iterator \n Differntial Revision- https://reviews.llvm.org/D135248

Shivam kunwar via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 1 10:17:20 PST 2023


Author: Shivam kunwar
Date: 2023-03-01T23:47:00+05:30
New Revision: 813e1da9749a5d2d604198cf534f750c842eda22

URL: https://github.com/llvm/llvm-project/commit/813e1da9749a5d2d604198cf534f750c842eda22
DIFF: https://github.com/llvm/llvm-project/commit/813e1da9749a5d2d604198cf534f750c842eda22.diff

LOG: [libc++] implement move_iterator<T*> should be a random access iterator \n Differntial Revision- https://reviews.llvm.org/D135248

Added: 
    

Modified: 
    libcxx/docs/FeatureTestMacroTable.rst
    libcxx/docs/ReleaseNotes.rst
    libcxx/docs/Status/Cxx2bPapers.csv
    libcxx/include/__iterator/move_iterator.h
    libcxx/include/iterator
    libcxx/include/version
    libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/iterator_concept_conformance.compile.pass.cpp
    libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/types.pass.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/iterator.version.compile.pass.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
    libcxx/utils/generate_feature_test_macro_components.py

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index f68384dda92dd..f3d26b9f7ed82 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -260,6 +260,8 @@ Status
     ------------------------------------------------- -----------------
     ``__cpp_lib_math_constants``                      ``201907L``
     ------------------------------------------------- -----------------
+    ``__cpp_lib_move_iterator_concept``               ``202207L``
+    ------------------------------------------------- -----------------
     ``__cpp_lib_polymorphic_allocator``               ``201902L``
     ------------------------------------------------- -----------------
     ``__cpp_lib_ranges``                              ``202106L``

diff  --git a/libcxx/docs/ReleaseNotes.rst b/libcxx/docs/ReleaseNotes.rst
index 7fcfb5637bae9..50f7ef58395e9 100644
--- a/libcxx/docs/ReleaseNotes.rst
+++ b/libcxx/docs/ReleaseNotes.rst
@@ -37,7 +37,7 @@ What's New in Libc++ 17.0.0?
 
 Implemented Papers
 ------------------
-
+- P2520R0 - ``move_iterator<T*>`` should be a random access iterator
 - P1328R1 - ``constexpr type_info::operator==()``
 
 Improvements and New Features

diff  --git a/libcxx/docs/Status/Cxx2bPapers.csv b/libcxx/docs/Status/Cxx2bPapers.csv
index 77562e6d9f06d..60f87b83ae083 100644
--- a/libcxx/docs/Status/Cxx2bPapers.csv
+++ b/libcxx/docs/Status/Cxx2bPapers.csv
@@ -84,7 +84,7 @@
 "`P2508R1 <https://wg21.link/P2508R1>`__","LWG","Exposing ``std::basic-format-string``","July 2022","|Complete|","15.0"
 "`P2513R4 <https://wg21.link/P2513R4>`__","LWG","``char8_t`` Compatibility and Portability Fixes","July 2022","",""
 "`P2517R1 <https://wg21.link/P2517R1>`__","LWG","Add a conditional ``noexcept`` specification to ``std::apply``","July 2022","",""
-"`P2520R0 <https://wg21.link/P2520R0>`__","LWG","``move_iterator`` should be a random access iterator","July 2022","","","|ranges|"
+"`P2520R0 <https://wg21.link/P2520R0>`__","LWG","``move_iterator`` should be a random access iterator","July 2022","|Complete|","17.0","|ranges|"
 "`P2540R1 <https://wg21.link/P2540R1>`__","LWG","Empty Product for certain Views","July 2022","","","|ranges|"
 "`P2549R1 <https://wg21.link/P2549R1>`__","LWG","``std::unexpected`` should have ``error()`` as member accessor","July 2022","|Complete|","16.0"
 "`P2553R1 <https://wg21.link/P2553R1>`__","LWG","Make ``mdspan`` ``size_type`` controllable","July 2022","",""

diff  --git a/libcxx/include/__iterator/move_iterator.h b/libcxx/include/__iterator/move_iterator.h
index 2079634bc442b..e3939cc0b5b22 100644
--- a/libcxx/include/__iterator/move_iterator.h
+++ b/libcxx/include/__iterator/move_iterator.h
@@ -67,10 +67,25 @@ class _LIBCPP_TEMPLATE_VIS move_iterator
     : public __move_iter_category_base<_Iter>
 #endif
 {
+    #if _LIBCPP_STD_VER >= 20
+private:
+    _LIBCPP_HIDE_FROM_ABI
+    static constexpr auto __get_iter_concept() {
+        if constexpr (random_access_iterator<_Iter>) {
+            return random_access_iterator_tag{};
+        } else if constexpr (bidirectional_iterator<_Iter>) {
+            return bidirectional_iterator_tag{};
+        } else if constexpr (forward_iterator<_Iter>) {
+            return forward_iterator_tag{};
+        } else {
+            return input_iterator_tag{};
+        }
+    }
+#endif // _LIBCPP_STD_VER >= 20
 public:
 #if _LIBCPP_STD_VER >= 20
     using iterator_type = _Iter;
-    using iterator_concept = input_iterator_tag;
+    using iterator_concept = decltype(__get_iter_concept());
     // iterator_category is inherited and not always present
     using value_type = iter_value_t<_Iter>;
     using 
diff erence_type = iter_
diff erence_t<_Iter>;

diff  --git a/libcxx/include/iterator b/libcxx/include/iterator
index 35eca67b4a5e8..d07ebcb074f9f 100644
--- a/libcxx/include/iterator
+++ b/libcxx/include/iterator
@@ -387,7 +387,7 @@ template <class Iterator>
 class move_iterator {
 public:
     using iterator_type     = Iterator;
-    using iterator_concept  = input_iterator_tag; // From C++20
+    using iterator_concept  = see below; // From C++20
     using iterator_category = see below; // not always present starting from C++20
     using value_type        = iter_value_t<Iterator>; // Until C++20, iterator_traits<Iterator>::value_type
     using 
diff erence_type   = iter_
diff erence_t<Iterator>; // Until C++20, iterator_traits<Iterator>::
diff erence_type;

diff  --git a/libcxx/include/version b/libcxx/include/version
index aa759c7994940..33a615b9f1452 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -122,6 +122,7 @@ __cpp_lib_map_try_emplace                               201411L <map>
 __cpp_lib_math_constants                                201907L <numbers>
 __cpp_lib_math_special_functions                        201603L <cmath>
 __cpp_lib_memory_resource                               201603L <memory_resource>
+__cpp_lib_move_iterator_concept                         202207L <iterator>
 __cpp_lib_move_only_function                            202110L <functional>
 __cpp_lib_node_extract                                  201606L <map> <set> <unordered_map>
                                                                 <unordered_set>
@@ -356,6 +357,7 @@ __cpp_lib_void_t                                        201411L <type_traits>
 # endif
 # define __cpp_lib_list_remove_return_type              201806L
 # define __cpp_lib_math_constants                       201907L
+# define __cpp_lib_move_iterator_concept                202207L
 # define __cpp_lib_polymorphic_allocator                201902L
 # define __cpp_lib_ranges                               202106L
 # define __cpp_lib_remove_cvref                         201711L

diff  --git a/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/iterator_concept_conformance.compile.pass.cpp b/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/iterator_concept_conformance.compile.pass.cpp
index 844da40b8efcb..1b21fd7d7fba2 100644
--- a/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/iterator_concept_conformance.compile.pass.cpp
+++ b/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/iterator_concept_conformance.compile.pass.cpp
@@ -56,8 +56,8 @@ void test()
 
     static_assert( std::default_initializable<iterator>);
     static_assert( std::copyable<iterator>);
-    static_assert( std::input_iterator<iterator>);
-    static_assert(!std::forward_iterator<iterator>);
+    static_assert( std::forward_iterator<iterator>);
+    static_assert(!std::bidirectional_iterator<iterator>);
     static_assert( std::sentinel_for<iterator, iterator>);
     static_assert(!std::sized_sentinel_for<iterator, iterator>);
     static_assert(!std::indirectly_movable<int*, iterator>);
@@ -73,8 +73,8 @@ void test()
 
     static_assert( std::default_initializable<iterator>);
     static_assert( std::copyable<iterator>);
-    static_assert( std::input_iterator<iterator>);
-    static_assert(!std::forward_iterator<iterator>);
+    static_assert( std::bidirectional_iterator<iterator>);
+    static_assert(!std::random_access_iterator<iterator>);
     static_assert( std::sentinel_for<iterator, iterator>);
     static_assert(!std::sized_sentinel_for<iterator, iterator>);
     static_assert(!std::indirectly_movable<int*, iterator>);
@@ -90,8 +90,8 @@ void test()
 
     static_assert( std::default_initializable<iterator>);
     static_assert( std::copyable<iterator>);
-    static_assert( std::input_iterator<iterator>);
-    static_assert(!std::forward_iterator<iterator>);
+    static_assert( std::random_access_iterator<iterator>);
+    static_assert(!std::contiguous_iterator<iterator>);
     static_assert( std::sentinel_for<iterator, iterator>);
     static_assert( std::sized_sentinel_for<iterator, iterator>);
     static_assert(!std::indirectly_movable<int*, iterator>);
@@ -107,8 +107,8 @@ void test()
 
     static_assert( std::default_initializable<iterator>);
     static_assert( std::copyable<iterator>);
-    static_assert( std::input_iterator<iterator>);
-    static_assert(!std::forward_iterator<iterator>);
+    static_assert( std::random_access_iterator<iterator>);
+    static_assert(!std::contiguous_iterator<iterator>);
     static_assert( std::sentinel_for<iterator, iterator>);
     static_assert( std::sized_sentinel_for<iterator, iterator>);
     static_assert(!std::indirectly_movable<int*, iterator>);
@@ -124,8 +124,8 @@ void test()
 
     static_assert( std::default_initializable<iterator>);
     static_assert( std::copyable<iterator>);
-    static_assert( std::input_iterator<iterator>);
-    static_assert(!std::forward_iterator<iterator>);
+    static_assert( std::random_access_iterator<iterator>);
+    static_assert(!std::contiguous_iterator<iterator>);
     static_assert( std::sentinel_for<iterator, iterator>);
     static_assert( std::sized_sentinel_for<iterator, iterator>);
     static_assert(!std::indirectly_movable<int*, iterator>);

diff  --git a/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/types.pass.cpp b/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/types.pass.cpp
index 5676564a8446f..9adf7d8696273 100644
--- a/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/types.pass.cpp
+++ b/libcxx/test/std/iterators/predef.iterators/move.iterators/move.iterator/types.pass.cpp
@@ -16,7 +16,7 @@
 // class move_iterator {
 // public:
 //  using iterator_type     = Iterator;
-//  using iterator_concept  = input_iterator_tag; // From C++20
+//  using iterator_concept  = see below; // From C++20
 //  using iterator_category = see below; // not always present starting from C++20
 //  using value_type        = iter_value_t<Iterator>; // Until C++20, iterator_traits<Iterator>::value_type
 //  using 
diff erence_type   = iter_
diff erence_t<Iterator>; // Until C++20, iterator_traits<Iterator>::
diff erence_type;
@@ -99,7 +99,8 @@ void test() {
 #endif
 
 #if TEST_STD_VER > 17
-  static_assert(std::is_same_v<typename R::iterator_concept, std::input_iterator_tag>);
+ static_assert(std::is_same_v<typename R::iterator_concept, std::conditional_t<std::is_same_v<typename R::iterator_concept, 
+std::contiguous_iterator_tag>, std::random_access_iterator_tag, typename R::iterator_concept>>);
 #endif
 }
 
@@ -142,11 +143,11 @@ int main(int, char**) {
 
 #if TEST_STD_VER > 17
   test<contiguous_iterator<char*>>();
-  static_assert(std::is_same_v<typename std::move_iterator<forward_iterator<char*>>::iterator_concept, std::input_iterator_tag>);
-  static_assert(std::is_same_v<typename std::move_iterator<bidirectional_iterator<char*>>::iterator_concept, std::input_iterator_tag>);
-  static_assert(std::is_same_v<typename std::move_iterator<random_access_iterator<char*>>::iterator_concept, std::input_iterator_tag>);
-  static_assert(std::is_same_v<typename std::move_iterator<contiguous_iterator<char*>>::iterator_concept, std::input_iterator_tag>);
-  static_assert(std::is_same_v<typename std::move_iterator<char*>::iterator_concept, std::input_iterator_tag>);
+  static_assert(std::is_same_v<typename std::move_iterator<forward_iterator<char*>>::iterator_concept, std::forward_iterator_tag>);
+  static_assert(std::is_same_v<typename std::move_iterator<bidirectional_iterator<char*>>::iterator_concept, std::bidirectional_iterator_tag>);
+  static_assert(std::is_same_v<typename std::move_iterator<random_access_iterator<char*>>::iterator_concept, std::random_access_iterator_tag>);
+  static_assert(std::is_same_v<typename std::move_iterator<contiguous_iterator<char*>>::iterator_concept, std::random_access_iterator_tag>);
+  static_assert(std::is_same_v<typename std::move_iterator<char*>::iterator_concept, std::random_access_iterator_tag>);
 #endif
 
   return 0;

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/iterator.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/iterator.version.compile.pass.cpp
index bf7d03b68f26b..a490a8a7dad6f 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/iterator.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/iterator.version.compile.pass.cpp
@@ -20,6 +20,7 @@
                                             201811L [C++20]
     __cpp_lib_constexpr_iterator            201811L [C++20]
     __cpp_lib_make_reverse_iterator         201402L [C++14]
+    __cpp_lib_move_iterator_concept         202207L [C++20]
     __cpp_lib_nonmember_container_access    201411L [C++17]
     __cpp_lib_null_iterators                201304L [C++14]
     __cpp_lib_ranges                        202106L [C++20]
@@ -43,6 +44,10 @@
 #   error "__cpp_lib_make_reverse_iterator should not be defined before c++14"
 # endif
 
+# ifdef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should not be defined before c++20"
+# endif
+
 # ifdef __cpp_lib_nonmember_container_access
 #   error "__cpp_lib_nonmember_container_access should not be defined before c++17"
 # endif
@@ -76,6 +81,10 @@
 #   error "__cpp_lib_make_reverse_iterator should have the value 201402L in c++14"
 # endif
 
+# ifdef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should not be defined before c++20"
+# endif
+
 # ifdef __cpp_lib_nonmember_container_access
 #   error "__cpp_lib_nonmember_container_access should not be defined before c++17"
 # endif
@@ -115,6 +124,10 @@
 #   error "__cpp_lib_make_reverse_iterator should have the value 201402L in c++17"
 # endif
 
+# ifdef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should not be defined before c++20"
+# endif
+
 # ifndef __cpp_lib_nonmember_container_access
 #   error "__cpp_lib_nonmember_container_access should be defined in c++17"
 # endif
@@ -160,6 +173,13 @@
 #   error "__cpp_lib_make_reverse_iterator should have the value 201402L in c++20"
 # endif
 
+# ifndef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should be defined in c++20"
+# endif
+# if __cpp_lib_move_iterator_concept != 202207L
+#   error "__cpp_lib_move_iterator_concept should have the value 202207L in c++20"
+# endif
+
 # ifndef __cpp_lib_nonmember_container_access
 #   error "__cpp_lib_nonmember_container_access should be defined in c++20"
 # endif
@@ -211,6 +231,13 @@
 #   error "__cpp_lib_make_reverse_iterator should have the value 201402L in c++2b"
 # endif
 
+# ifndef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should be defined in c++2b"
+# endif
+# if __cpp_lib_move_iterator_concept != 202207L
+#   error "__cpp_lib_move_iterator_concept should have the value 202207L in c++2b"
+# endif
+
 # ifndef __cpp_lib_nonmember_container_access
 #   error "__cpp_lib_nonmember_container_access should be defined in c++2b"
 # endif

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index d23270edf14d1..13cdf711f4b35 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -116,6 +116,7 @@
     __cpp_lib_math_constants                       201907L [C++20]
     __cpp_lib_math_special_functions               201603L [C++17]
     __cpp_lib_memory_resource                      201603L [C++17]
+    __cpp_lib_move_iterator_concept                202207L [C++20]
     __cpp_lib_move_only_function                   202110L [C++2b]
     __cpp_lib_node_extract                         201606L [C++17]
     __cpp_lib_nonmember_container_access           201411L [C++17]
@@ -578,6 +579,10 @@
 #   error "__cpp_lib_memory_resource should not be defined before c++17"
 # endif
 
+# ifdef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should not be defined before c++20"
+# endif
+
 # ifdef __cpp_lib_move_only_function
 #   error "__cpp_lib_move_only_function should not be defined before c++2b"
 # endif
@@ -1250,6 +1255,10 @@
 #   error "__cpp_lib_memory_resource should not be defined before c++17"
 # endif
 
+# ifdef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should not be defined before c++20"
+# endif
+
 # ifdef __cpp_lib_move_only_function
 #   error "__cpp_lib_move_only_function should not be defined before c++2b"
 # endif
@@ -2072,6 +2081,10 @@
 #   error "__cpp_lib_memory_resource should have the value 201603L in c++17"
 # endif
 
+# ifdef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should not be defined before c++20"
+# endif
+
 # ifdef __cpp_lib_move_only_function
 #   error "__cpp_lib_move_only_function should not be defined before c++2b"
 # endif
@@ -3176,6 +3189,13 @@
 #   error "__cpp_lib_memory_resource should have the value 201603L in c++20"
 # endif
 
+# ifndef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should be defined in c++20"
+# endif
+# if __cpp_lib_move_iterator_concept != 202207L
+#   error "__cpp_lib_move_iterator_concept should have the value 202207L in c++20"
+# endif
+
 # ifdef __cpp_lib_move_only_function
 #   error "__cpp_lib_move_only_function should not be defined before c++2b"
 # endif
@@ -4415,6 +4435,13 @@
 #   error "__cpp_lib_memory_resource should have the value 201603L in c++2b"
 # endif
 
+# ifndef __cpp_lib_move_iterator_concept
+#   error "__cpp_lib_move_iterator_concept should be defined in c++2b"
+# endif
+# if __cpp_lib_move_iterator_concept != 202207L
+#   error "__cpp_lib_move_iterator_concept should have the value 202207L in c++2b"
+# endif
+
 # if !defined(_LIBCPP_VERSION)
 #   ifndef __cpp_lib_move_only_function
 #     error "__cpp_lib_move_only_function should be defined in c++2b"

diff  --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 39af3bec170bf..db76a9eefd85f 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -488,6 +488,10 @@ def add_version_header(tc):
     "values": { "c++17": 201603 },
     "headers": ["memory_resource"],
   }, {
+    "name": "__cpp_lib_move_iterator_concept",
+    "values": { "c++20": 202207 },
+    "headers": ["iterator"],
+   }, {
     "name": "__cpp_lib_move_only_function",
     "values": { "c++2b": 202110 },
     "headers": ["functional"],


        


More information about the libcxx-commits mailing list