[libcxx-commits] [PATCH] D114563: [libc++] Fix ssize test that made an assumption about ptrdiff_t

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 24 13:43:40 PST 2021


ldionne created this revision.
Herald added a subscriber: kristof.beyls.
ldionne requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

On some platforms, the size() method of containers returns a type that
is larger than ptrdiff_t, and hence the result of std::ssize ends up
being a signed version of size_t, which is not the same of ptrdiff_t.

This happens for example on 32 bit arm, where ptrdiff_t is int, but
size_t is still unsigned long. As a result, std::ssize returns long,
and this test fails (before the fix).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114563

Files:
  libcxx/test/std/iterators/iterator.container/ssize.pass.cpp


Index: libcxx/test/std/iterators/iterator.container/ssize.pass.cpp
===================================================================
--- libcxx/test/std/iterators/iterator.container/ssize.pass.cpp
+++ libcxx/test/std/iterators/iterator.container/ssize.pass.cpp
@@ -78,14 +78,15 @@
     std::list<int>   l; l.push_back(2);
     std::array<int, 1> a; a[0] = 3;
     std::initializer_list<int> il = { 4 };
+    using SSize = std::common_type_t<std::ptrdiff_t, std::make_signed_t<std::size_t>>;
     test_container ( v );
-    ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(v)));
+    ASSERT_SAME_TYPE(SSize, decltype(std::ssize(v)));
     test_container ( l );
-    ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(l)));
+    ASSERT_SAME_TYPE(SSize, decltype(std::ssize(l)));
     test_container ( a );
-    ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(a)));
+    ASSERT_SAME_TYPE(SSize, decltype(std::ssize(a)));
     test_container ( il );
-    ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(il)));
+    ASSERT_SAME_TYPE(SSize, decltype(std::ssize(il)));
 
     test_const_container ( v );
     test_const_container ( l );
@@ -94,7 +95,7 @@
 
     std::string_view sv{"ABC"};
     test_container ( sv );
-    ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(sv)));
+    ASSERT_SAME_TYPE(SSize, decltype(std::ssize(sv)));
     test_const_container ( sv );
 
     static constexpr int arrA [] { 1, 2, 3 };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114563.389606.patch
Type: text/x-patch
Size: 1408 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211124/8a09d462/attachment.bin>


More information about the libcxx-commits mailing list