-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[ADT] Use adl_begin/end in hasNItems*
#130524
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
Conversation
This is to make sure that ADT helpers consistently use argument dependent lookup when dealing with input ranges. This was a part of llvm#87936 but reverted due to buildbot failures.
|
@llvm/pr-subscribers-llvm-adt Author: Jakub Kuderski (kuhar) ChangesThis is to make sure that ADT helpers consistently use argument dependent lookup when dealing with input ranges. This was a part of #87936 but reverted due to buildbot failures. Full diff: https://github.com/llvm/llvm-project/pull/130524.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index fe1d010574e31..6aa014b129d72 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -2562,19 +2562,19 @@ bool hasNItemsOrLess(
/// Returns true if the given container has exactly N items
template <typename ContainerTy> bool hasNItems(ContainerTy &&C, unsigned N) {
- return hasNItems(std::begin(C), std::end(C), N);
+ return hasNItems(adl_begin(C), adl_end(C), N);
}
/// Returns true if the given container has N or more items
template <typename ContainerTy>
bool hasNItemsOrMore(ContainerTy &&C, unsigned N) {
- return hasNItemsOrMore(std::begin(C), std::end(C), N);
+ return hasNItemsOrMore(adl_begin(C), adl_end(C), N);
}
/// Returns true if the given container has N or less items
template <typename ContainerTy>
bool hasNItemsOrLess(ContainerTy &&C, unsigned N) {
- return hasNItemsOrLess(std::begin(C), std::end(C), N);
+ return hasNItemsOrLess(adl_begin(C), adl_end(C), N);
}
/// Returns a raw pointer that represents the same address as the argument.
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index e107cb570641b..7e6925ab74d90 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -421,6 +421,16 @@ void swap(some_struct &lhs, some_struct &rhs) {
rhs.swap_val = "rhs";
}
+struct List {
+ std::list<int> data;
+};
+
+std::list<int>::const_iterator begin(const List &list) {
+ return list.data.begin();
+}
+
+std::list<int>::const_iterator end(const List &list) { return list.data.end(); }
+
struct requires_move {};
int *begin(requires_move &&) { return nullptr; }
int *end(requires_move &&) { return nullptr; }
@@ -961,6 +971,13 @@ TEST(STLExtrasTest, hasNItems) {
EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 3, [](int x) { return x < 10; }));
EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 0, [](int x) { return x > 10; }));
EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 2, [](int x) { return x < 5; }));
+
+ // Make sure that we use the `begin`/`end` functions from `some_namespace`,
+ // using ADL.
+ some_namespace::List L;
+ L.data = {0, 1, 2};
+ EXPECT_FALSE(hasNItems(L, 2));
+ EXPECT_TRUE(hasNItems(L, 3));
}
TEST(STLExtras, hasNItemsOrMore) {
@@ -983,6 +1000,13 @@ TEST(STLExtras, hasNItemsOrMore) {
hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x > 10; }));
EXPECT_TRUE(
hasNItemsOrMore(V3.begin(), V3.end(), 2, [](int x) { return x < 5; }));
+
+ // Make sure that we use the `begin`/`end` functions from `some_namespace`,
+ // using ADL.
+ some_namespace::List L;
+ L.data = {0, 1, 2};
+ EXPECT_TRUE(hasNItemsOrMore(L, 1));
+ EXPECT_FALSE(hasNItems(L, 4));
}
TEST(STLExtras, hasNItemsOrLess) {
@@ -1016,6 +1040,13 @@ TEST(STLExtras, hasNItemsOrLess) {
hasNItemsOrLess(V3.begin(), V3.end(), 5, [](int x) { return x < 5; }));
EXPECT_FALSE(
hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; }));
+
+ // Make sure that we use the `begin`/`end` functions from `some_namespace`,
+ // using ADL.
+ some_namespace::List L;
+ L.data = {0, 1, 2};
+ EXPECT_FALSE(hasNItemsOrLess(L, 1));
+ EXPECT_TRUE(hasNItemsOrLess(L, 4));
}
TEST(STLExtras, MoveRange) {
|
kazutakahirata
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/65/builds/13533 Here is the relevant piece of the build log for the reference |
This is to make sure that ADT helpers consistently use argument dependent lookup when dealing with input ranges.
This was a part of #87936 but reverted due to buildbot failures.