Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

lbonn
Copy link

@lbonn lbonn commented Sep 12, 2025

The outer iterator needs to move to the next segment when calling __compose.

Without this change, find_segment_if would never reach the end of the join_view which caused erroneous result when calling ranges::find on a join_view of bidirectional ranges.

Other specializations using the segmented iterator trait were likely to be affected as well.

Fixes #158279, #93180

cc @philnik777

The outer iterator needs to move to the next segment when calling
__compose.

Without this change, `find_segment_if` would never reach the end of the
join_view which caused erroneous result when calling `ranges::find` on a
join_view of bidirectional ranges.

Other specializations using the segmented iterator trait were likely to
be affected as well.
@lbonn lbonn requested a review from a team as a code owner September 12, 2025 18:56
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2025

@llvm/pr-subscribers-libcxx

Author: None (lbonn)

Changes

The outer iterator needs to move to the next segment when calling __compose.

Without this change, find_segment_if would never reach the end of the join_view which caused erroneous result when calling ranges::find on a join_view of bidirectional ranges.

Other specializations using the segmented iterator trait were likely to be affected as well.

Fixes #158279, #93180

cc @philnik777


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

2 Files Affected:

  • (modified) libcxx/include/__ranges/join_view.h (+6-2)
  • (added) libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp (+37)
diff --git a/libcxx/include/__ranges/join_view.h b/libcxx/include/__ranges/join_view.h
index 327b349f476a7..6097754f403ec 100644
--- a/libcxx/include/__ranges/join_view.h
+++ b/libcxx/include/__ranges/join_view.h
@@ -410,8 +410,12 @@ struct __segmented_iterator_traits<_JoinViewIterator> {
 
   static constexpr _LIBCPP_HIDE_FROM_ABI _JoinViewIterator
   __compose(__segment_iterator __seg_iter, __local_iterator __local_iter) {
-    return _JoinViewIterator(
-        std::move(__seg_iter).__get_data(), std::move(__seg_iter).__get_iter(), std::move(__local_iter));
+    auto&& __outer = std::move(__seg_iter).__get_iter();
+    if (__local_iter == ranges::end(*__outer)) {
+      ++__outer;
+      return _JoinViewIterator(*std::move(__seg_iter).__get_data(), __outer);
+    }
+    return _JoinViewIterator(std::move(__seg_iter).__get_data(), __outer, std::move(__local_iter));
   }
 };
 
diff --git a/libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp
new file mode 100644
index 0000000000000..f8bee2227d0b9
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <algorithm>
+#include <cassert>
+#include <ranges>
+#include <type_traits>
+
+#include "../types.h"
+
+constexpr bool test() {
+  // Test the segmented iterator implementation of join_view
+  // https://github.com/llvm/llvm-project/issues/158279
+  {
+    int buffer1[2][1] = {{1}, {2}};
+    auto joined       = std::views::join(buffer1);
+    assert(std::ranges::find(joined, 1) == std::ranges::begin(joined));
+    assert(std::ranges::find(joined, 2) == std::ranges::next(std::ranges::begin(joined)));
+    assert(std::ranges::find(joined, 3) == std::ranges::end(joined));
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

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++] Incorrect behavior of ranges::find with join_view
2 participants