Skip to content

Commit 7770d8f

Browse files
authored
[ADT] Use adl_begin/adl_end in make_filter_range (#130512)
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. Also fix potential issue with double-move on the input range.
1 parent e9b2edd commit 7770d8f

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,9 @@ iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>>
574574
make_filter_range(RangeT &&Range, PredicateT Pred) {
575575
using FilterIteratorT =
576576
filter_iterator<detail::IterOfRange<RangeT>, PredicateT>;
577-
return make_range(
578-
FilterIteratorT(std::begin(std::forward<RangeT>(Range)),
579-
std::end(std::forward<RangeT>(Range)), Pred),
580-
FilterIteratorT(std::end(std::forward<RangeT>(Range)),
581-
std::end(std::forward<RangeT>(Range)), Pred));
577+
auto B = adl_begin(Range);
578+
auto E = adl_end(Range);
579+
return make_range(FilterIteratorT(B, E, Pred), FilterIteratorT(E, E, Pred));
582580
}
583581

584582
/// A pseudo-iterator adaptor that is designed to implement "early increment"

llvm/unittests/ADT/IteratorTest.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ using testing::ElementsAre;
2222

2323
namespace {
2424

25+
namespace adl_test {
26+
struct WithFreeBeginEnd {
27+
int data[3] = {21, 22, 23};
28+
};
29+
30+
auto begin(const WithFreeBeginEnd &X) { return std::begin(X.data); }
31+
auto end(const WithFreeBeginEnd &X) { return std::end(X.data); }
32+
33+
struct WithFreeRBeginREnd {
34+
int data[3] = {42, 43, 44};
35+
};
36+
37+
auto rbegin(const WithFreeRBeginREnd &X) { return std::rbegin(X.data); }
38+
auto rend(const WithFreeRBeginREnd &X) { return std::rend(X.data); }
39+
} // namespace adl_test
40+
2541
template <int> struct Shadow;
2642

2743
struct WeirdIter
@@ -364,6 +380,14 @@ TEST(FilterIteratorTest, ReverseFilterRange) {
364380
EXPECT_EQ((SmallVector<int, 4>{6, 4, 2, 0}), Actual4);
365381
}
366382

383+
TEST(FilterIteratorTest, ADL) {
384+
// Make sure that we use the `begin`/`end` functions
385+
// from `adl_test`, using ADL.
386+
adl_test::WithFreeBeginEnd R;
387+
auto IsOdd = [](int N) { return N % 2 != 0; };
388+
EXPECT_THAT(make_filter_range(R, IsOdd), ElementsAre(21, 23));
389+
}
390+
367391
TEST(PointerIterator, Basic) {
368392
int A[] = {1, 2, 3, 4};
369393
pointer_iterator<int *> Begin(std::begin(A)), End(std::end(A));
@@ -395,18 +419,9 @@ TEST(PointerIterator, Range) {
395419
EXPECT_EQ(A + I++, P);
396420
}
397421

398-
namespace rbegin_detail {
399-
struct WithFreeRBegin {
400-
int data[3] = {42, 43, 44};
401-
};
402-
403-
auto rbegin(const WithFreeRBegin &X) { return std::rbegin(X.data); }
404-
auto rend(const WithFreeRBegin &X) { return std::rend(X.data); }
405-
} // namespace rbegin_detail
406-
407422
TEST(ReverseTest, ADL) {
408423
// Check that we can find the rbegin/rend functions via ADL.
409-
rbegin_detail::WithFreeRBegin Foo;
424+
adl_test::WithFreeRBeginREnd Foo;
410425
EXPECT_THAT(reverse(Foo), ElementsAre(44, 43, 42));
411426
}
412427

0 commit comments

Comments
 (0)