Skip to content

Commit b59461a

Browse files
authored
[ADT] Add back ability to compare StringSet (#91374)
StringSet comparison was broken after moving from llvm::Optional to std::optional because std::nullopt_t is not equality-comparable. Without this patch a try to compare objects of StringSet type leads to compilation error: ``` llvm-project/llvm/include/llvm/ADT/StringMap.h:294:33: error: no match for ‘operator==’ (operand types are ‘std::nullopt_t’ and ‘std::nullopt_t’) 294 | if (!(KeyValue.getValue() == FindInRHS->getValue())) ```
1 parent 3dcbcce commit b59461a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

llvm/include/llvm/ADT/StringMap.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,10 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
291291
if (FindInRHS == RHS.end())
292292
return false;
293293

294-
if (!(KeyValue.getValue() == FindInRHS->getValue()))
295-
return false;
294+
if constexpr (!std::is_same_v<ValueTy, std::nullopt_t>) {
295+
if (!(KeyValue.getValue() == FindInRHS->getValue()))
296+
return false;
297+
}
296298
}
297299

298300
return true;

llvm/unittests/ADT/StringSetTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,12 @@ TEST_F(StringSetTest, Contains) {
7373
EXPECT_FALSE(Set.contains("test"));
7474
}
7575

76+
TEST_F(StringSetTest, Equal) {
77+
StringSet<> A = {"A"};
78+
StringSet<> B = {"B"};
79+
ASSERT_TRUE(A != B);
80+
ASSERT_FALSE(A == B);
81+
ASSERT_TRUE(A == A);
82+
}
83+
7684
} // end anonymous namespace

0 commit comments

Comments
 (0)