From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Tue, 24 Sep 2024 23:36:16 -0400 Subject: [PATCH 38/38] StringMap: fix structured bindings with move-only types --- llvm/include/llvm/ADT/StringMapEntry.h | 12 +++++++++++- llvm/unittests/ADT/StringMapTest.cpp | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h index 011806f5fd89ff738ed805a82b3ddbc6fc9b08ce..5b8eeb167c53c7ab15d05b0c71b75cd7f5b496c5 100644 --- a/llvm/include/llvm/ADT/StringMapEntry.h +++ b/llvm/include/llvm/ADT/StringMapEntry.h @@ -159,7 +159,17 @@ decltype(auto) get(const StringMapEntry &E) { if constexpr (Index == 0) return E.first(); else - return E.second; + return (E.second); +} + +// Allow structured bindings on StringMapEntry. +template +decltype(auto) get(StringMapEntry &E) { + static_assert(Index < 2); + if constexpr (Index == 0) + return E.first(); + else + return (E.second); } } // end namespace llvm diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index 0d83669a580408e925ec6308410ebe7c01b48b12..f1834bec65afec6be4365e52c4e21aa61cbb1d12 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -526,6 +526,16 @@ TEST_F(StringMapTest, StructuredBindings) { } } +TEST_F(StringMapTest, StructuredBindingsMoveOnly) { + StringMap A; + A.insert(std::make_pair("a", MoveOnly(42))); + + for (auto &&[Key, Value] : A) { + EXPECT_EQ("a", Key); + EXPECT_EQ(42, Value.i); + } +} + namespace { // Simple class that counts how many moves and copy happens when growing a map struct CountCtorCopyAndMove {