Skip to content

[libc++] Fix implementation of iota_view::size - #67819

Merged
ldionne merged 1 commit into
llvm:mainfrom
ldionne:review/fix-ranges-iota_view-size
Oct 4, 2023
Merged

[libc++] Fix implementation of iota_view::size#67819
ldionne merged 1 commit into
llvm:mainfrom
ldionne:review/fix-ranges-iota_view-size

Conversation

@ldionne

@ldionne ldionne commented Sep 29, 2023

Copy link
Copy Markdown
Member

We were incorrectly deducing the return type of size() because we were not using ternary operators in the implementation (as the spec says). Instead of deducing the common type of the expressions in the spec, we would deduce potentially different return types and fail to compile.

Fixes #67551

We were incorrectly deducing the return type of size() because we were
not using ternary operators in the implementation (as the spec says).
Instead of deducing the common type of the expressions in the spec, we
would deduce potentially different return types and fail to compile.

Fixes llvm#67551
@ldionne
ldionne requested a review from a team as a code owner September 29, 2023 15:26
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 29, 2023
@llvmbot

llvmbot commented Sep 29, 2023

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-libcxx

Changes

We were incorrectly deducing the return type of size() because we were not using ternary operators in the implementation (as the spec says). Instead of deducing the common type of the expressions in the spec, we would deduce potentially different return types and fail to compile.

Fixes #67551


Full diff: https://github.com/llvm/llvm-project/pull/67819.diff

2 Files Affected:

  • (modified) libcxx/include/__ranges/iota_view.h (+7-8)
  • (modified) libcxx/test/std/ranges/range.factories/range.iota.view/size.pass.cpp (+11-1)
diff --git a/libcxx/include/__ranges/iota_view.h b/libcxx/include/__ranges/iota_view.h
index f372688abfdaf32..ccf0c7a8e8d50e4 100644
--- a/libcxx/include/__ranges/iota_view.h
+++ b/libcxx/include/__ranges/iota_view.h
@@ -363,15 +363,14 @@ namespace ranges {
               (integral<_Start> && integral<_BoundSentinel>) || sized_sentinel_for<_BoundSentinel, _Start>
     {
       if constexpr (__integer_like<_Start> && __integer_like<_BoundSentinel>) {
-        if (__value_ < 0) {
-          if (__bound_sentinel_ < 0) {
-            return std::__to_unsigned_like(-__value_) - std::__to_unsigned_like(-__bound_sentinel_);
-          }
-          return std::__to_unsigned_like(__bound_sentinel_) + std::__to_unsigned_like(-__value_);
-        }
-        return std::__to_unsigned_like(__bound_sentinel_) - std::__to_unsigned_like(__value_);
+        return (__value_ < 0)
+                 ? ((__bound_sentinel_ < 0)
+                        ? std::__to_unsigned_like(-__value_) - std::__to_unsigned_like(-__bound_sentinel_)
+                        : std::__to_unsigned_like(__bound_sentinel_) + std::__to_unsigned_like(-__value_))
+                 : std::__to_unsigned_like(__bound_sentinel_) - std::__to_unsigned_like(__value_);
+      } else {
+        return std::__to_unsigned_like(__bound_sentinel_ - __value_);
       }
-      return std::__to_unsigned_like(__bound_sentinel_ - __value_);
     }
   };
 
diff --git a/libcxx/test/std/ranges/range.factories/range.iota.view/size.pass.cpp b/libcxx/test/std/ranges/range.factories/range.iota.view/size.pass.cpp
index 07092b74be87389..b894bc542be10b6 100644
--- a/libcxx/test/std/ranges/range.factories/range.iota.view/size.pass.cpp
+++ b/libcxx/test/std/ranges/range.factories/range.iota.view/size.pass.cpp
@@ -10,9 +10,10 @@
 
 // constexpr auto size() const requires see below;
 
-#include <ranges>
 #include <cassert>
+#include <concepts>
 #include <limits>
+#include <ranges>
 
 #include "test_macros.h"
 #include "types.h"
@@ -89,6 +90,15 @@ constexpr bool test() {
     assert(io.size() == 0);
   }
 
+  // Make sure iota_view<short, short> works properly. For details,
+  // see https://github.com/llvm/llvm-project/issues/67551.
+  {
+    static_assert(std::ranges::sized_range<std::ranges::iota_view<short, short>>);
+    std::ranges::iota_view<short, short> io(10, 20);
+    std::same_as<unsigned int> auto sz = io.size();
+    assert(sz == 10);
+  }
+
   return true;
 }
 

@phyBrackets phyBrackets left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

@ldionne
ldionne merged commit b09551f into llvm:main Oct 4, 2023
@ldionne
ldionne deleted the review/fix-ranges-iota_view-size branch October 4, 2023 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc++] [ranges] sized_range of iota_view<short, short>

3 participants