From 12f7d232969e8e69fdb9ecc168da34642aa37761 Mon Sep 17 00:00:00 2001 From: Jakub Kuderski Date: Sun, 9 Mar 2025 15:31:27 -0400 Subject: [PATCH] [ADT] Use `adl_being`/`end` in `hasSingleElement 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. Now that I have a threadripper system, I'm landing this piece-by-piece. --- llvm/include/llvm/ADT/STLExtras.h | 3 ++- llvm/unittests/ADT/STLExtrasTest.cpp | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index ace5f60b572d7..fe1d010574e31 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -320,7 +320,8 @@ template class Callable { /// Returns true if the given container only contains a single element. template bool hasSingleElement(ContainerTy &&C) { - auto B = std::begin(C), E = std::end(C); + auto B = adl_begin(C); + auto E = adl_end(C); return B != E && std::next(B) == E; } diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp index 406ff2bc16073..e107cb570641b 100644 --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -934,10 +934,19 @@ TEST(STLExtrasTest, hasSingleElement) { const std::vector V0 = {}, V1 = {1}, V2 = {1, 2}; const std::vector V10(10); - EXPECT_EQ(hasSingleElement(V0), false); - EXPECT_EQ(hasSingleElement(V1), true); - EXPECT_EQ(hasSingleElement(V2), false); - EXPECT_EQ(hasSingleElement(V10), false); + EXPECT_FALSE(hasSingleElement(V0)); + EXPECT_TRUE(hasSingleElement(V1)); + EXPECT_FALSE(hasSingleElement(V2)); + EXPECT_FALSE(hasSingleElement(V10)); + + // Make sure that we use the `begin`/`end` functions + // from `some_namespace`, using ADL. + some_namespace::some_struct S; + EXPECT_FALSE(hasSingleElement(S)); + S.data = V1; + EXPECT_TRUE(hasSingleElement(S)); + S.data = V2; + EXPECT_FALSE(hasSingleElement(S)); } TEST(STLExtrasTest, hasNItems) {