Skip to content

Commit 30fdeec

Browse files
authored
[ADT] Use adl_being/end in map_range (#130508)
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 add `map_range` unit tests -- there were no pre-existing tests AFAICT.
1 parent 6981f7e commit 30fdeec

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ inline mapped_iterator<ItTy, FuncTy> map_iterator(ItTy I, FuncTy F) {
376376

377377
template <class ContainerTy, class FuncTy>
378378
auto map_range(ContainerTy &&C, FuncTy F) {
379-
return make_range(map_iterator(std::begin(C), F),
380-
map_iterator(std::end(C), F));
379+
return make_range(map_iterator(adl_begin(C), F), map_iterator(adl_end(C), F));
381380
}
382381

383382
/// A base type of mapped iterator, that is useful for building derived

llvm/unittests/ADT/STLExtrasTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,18 @@ TEST(STLExtrasTest, DropEndDefaultTest) {
750750
EXPECT_EQ(i, 4);
751751
}
752752

753+
TEST(STLExtrasTest, MapRangeTest) {
754+
SmallVector<int, 5> Vec{0, 1, 2};
755+
EXPECT_THAT(map_range(Vec, [](int V) { return V + 1; }),
756+
ElementsAre(1, 2, 3));
757+
758+
// Make sure that we use the `begin`/`end` functions
759+
// from `some_namespace`, using ADL.
760+
some_namespace::some_struct S;
761+
S.data = {3, 4, 5};
762+
EXPECT_THAT(map_range(S, [](int V) { return V * 2; }), ElementsAre(6, 8, 10));
763+
}
764+
753765
TEST(STLExtrasTest, EarlyIncrementTest) {
754766
std::list<int> L = {1, 2, 3, 4};
755767

0 commit comments

Comments
 (0)