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

Skip to content

[libc++] Fix flat_{multi}set insert_range #137462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

huixie90
Copy link
Contributor

@huixie90 huixie90 commented Apr 26, 2025

fixes #136656

@huixie90 huixie90 requested a review from a team as a code owner April 26, 2025 15:50
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 26, 2025

@llvm/pr-subscribers-libcxx

Author: Hui (huixie90)

Changes

fixes #136656


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

3 Files Affected:

  • (modified) libcxx/include/__flat_set/utils.h (+3-1)
  • (modified) libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.modifiers/insert_range.pass.cpp (+10)
  • (modified) libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_range.pass.cpp (+10)
diff --git a/libcxx/include/__flat_set/utils.h b/libcxx/include/__flat_set/utils.h
index ed3b4c48580fb..542bfd886aef5 100644
--- a/libcxx/include/__flat_set/utils.h
+++ b/libcxx/include/__flat_set/utils.h
@@ -11,6 +11,7 @@
 #define _LIBCPP___FLAT_SET_UTILS_H
 
 #include <__config>
+#include <__iterator/iterator_traits.h>
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
 #include <__type_traits/container_traits.h>
@@ -60,7 +61,8 @@ struct __flat_set_utils {
       // C++23 Sequence Container should have insert_range member function
       // Note that not all Sequence Containers provide append_range.
       __set.__keys_.insert_range(__set.__keys_.end(), std::forward<_Range>(__rng));
-    } else if constexpr (ranges::common_range<_Range>) {
+    } else if constexpr (ranges::common_range<_Range> &&
+                         __has_input_iterator_category<ranges::iterator_t<_Range>>::value) {
       __set.__keys_.insert(__set.__keys_.end(), ranges::begin(__rng), ranges::end(__rng));
     } else {
       for (auto&& __x : __rng) {
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.modifiers/insert_range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.modifiers/insert_range.pass.cpp
index 566be3921bf77..2f6a68fa0581a 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.modifiers/insert_range.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.modifiers/insert_range.pass.cpp
@@ -18,6 +18,7 @@
 #include <flat_set>
 #include <functional>
 #include <ranges>
+#include <sstream>
 #include <vector>
 
 #include "MinSequenceContainer.h"
@@ -85,6 +86,15 @@ void test() {
     MoveOnly expected[] = {1, 1, 3, 4, 5};
     assert(std::ranges::equal(m, expected));
   }
+  {
+    // https://github.com/llvm/llvm-project/issues/136656
+    MinSequenceContainer<int> v;
+    std::flat_multiset s(v);
+    std::istringstream ints("0 1 1 0");
+    auto r = std::ranges::subrange(std::istream_iterator<int>(ints), std::istream_iterator<int>()) |
+             std::views::transform([](int i) { return i * i; });
+    s.insert_range(r);
+  }
 }
 
 void test_exception() {
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_range.pass.cpp
index 966a696c096b9..3b53ca255ceeb 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_range.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_range.pass.cpp
@@ -18,6 +18,7 @@
 #include <flat_set>
 #include <functional>
 #include <ranges>
+#include <sstream>
 #include <vector>
 
 #include "MinSequenceContainer.h"
@@ -96,6 +97,15 @@ void test() {
     MoveOnly expected[] = {1, 3, 4, 5};
     assert(std::ranges::equal(m, expected));
   }
+  {
+    // https://github.com/llvm/llvm-project/issues/136656
+    MinSequenceContainer<int> v;
+    std::flat_set s(v);
+    std::istringstream ints("0 1 1 0");
+    auto r = std::ranges::subrange(std::istream_iterator<int>(ints), std::istream_iterator<int>()) |
+             std::views::transform([](int i) { return i * i; });
+    s.insert_range(r);
+  }
 }
 
 void test_exception() {

Comment on lines +64 to +65
} else if constexpr (ranges::common_range<_Range> &&
__has_input_iterator_category<ranges::iterator_t<_Range>>::value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

No (functional) change requested. Do we want to say the fallback branches are (pedantically) extensions?

Copy link
Contributor

Choose a reason for hiding this comment

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

On the other hand, it seems that the missing parts of MinSequenceContainer for C++23 sequence container requirements "silently" enforced our flat_meow to contain some extensions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added static_assert in the test case

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I guess these cases should be moved to the libcxx/test/libcxx subdirectory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. moved the tests. leave this open for others to put their opinions on this "extension"

@ldionne ldionne changed the title [libc++] fix flat_{multi}set insert_range [libc++] Fix flat_{multi}set insert_range May 6, 2025
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

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

LGTM but I'd like a LWG issue about it.


// <flat_set>

// https://github.com/llvm/llvm-project/issues/136656
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// https://github.com/llvm/llvm-project/issues/136656
// As an extension, libc++ flat containers support inserting a non forward range into
// a pre-C++23 container that doesn't provide insert_range(...), since many containers
// out there are in that situation.
// https://github.com/llvm/llvm-project/issues/136656


// <flat_set>

// https://github.com/llvm/llvm-project/issues/136656
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// https://github.com/llvm/llvm-project/issues/136656
// As an extension, libc++ flat containers support inserting a non forward range into
// a pre-C++23 container that doesn't provide insert_range(...), since many containers
// out there are in that situation.
// https://github.com/llvm/llvm-project/issues/136656

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
4 participants