[llvm] b59461a - [ADT] Add back ability to compare StringSet (#91374)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 8 09:40:19 PDT 2024


Author: Aleksandr Platonov
Date: 2024-05-08T09:40:16-07:00
New Revision: b59461ac63aa1770a617f96bab31010442bd2090

URL: https://github.com/llvm/llvm-project/commit/b59461ac63aa1770a617f96bab31010442bd2090
DIFF: https://github.com/llvm/llvm-project/commit/b59461ac63aa1770a617f96bab31010442bd2090.diff

LOG: [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()))
```

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringMap.h
    llvm/unittests/ADT/StringSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h
index 453d91349e358..daaf82654e094 100644
--- a/llvm/include/llvm/ADT/StringMap.h
+++ b/llvm/include/llvm/ADT/StringMap.h
@@ -291,8 +291,10 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
       if (FindInRHS == RHS.end())
         return false;
 
-      if (!(KeyValue.getValue() == FindInRHS->getValue()))
-        return false;
+      if constexpr (!std::is_same_v<ValueTy, std::nullopt_t>) {
+        if (!(KeyValue.getValue() == FindInRHS->getValue()))
+          return false;
+      }
     }
 
     return true;

diff  --git a/llvm/unittests/ADT/StringSetTest.cpp b/llvm/unittests/ADT/StringSetTest.cpp
index e3703f6f01508..a804c1f17d1ce 100644
--- a/llvm/unittests/ADT/StringSetTest.cpp
+++ b/llvm/unittests/ADT/StringSetTest.cpp
@@ -73,4 +73,12 @@ TEST_F(StringSetTest, Contains) {
   EXPECT_FALSE(Set.contains("test"));
 }
 
+TEST_F(StringSetTest, Equal) {
+  StringSet<> A = {"A"};
+  StringSet<> B = {"B"};
+  ASSERT_TRUE(A != B);
+  ASSERT_FALSE(A == B);
+  ASSERT_TRUE(A == A);
+}
+
 } // end anonymous namespace


        


More information about the llvm-commits mailing list