================
@@ -31,8 +31,10 @@ class WeakInfo {
       : alias(Alias), loc(Loc) {}
   inline const IdentifierInfo *getAlias() const { return alias; }
   inline SourceLocation getLocation() const { return loc; }
-  bool operator==(WeakInfo RHS) const = delete;
-  bool operator!=(WeakInfo RHS) const = delete;
+  bool operator==(const WeakInfo &RHS) const {
+    return getAlias() == RHS.getAlias() && getLocation() == RHS.getLocation();
+  }
----------------
eugeneepshteyn wrote:

(This comment was generated with the help of AI)

This `operator==` disagrees with the set this type is stored in. 
`Sema::WeakUndeclaredIdentifiers` hashes with 
`WeakInfo::DenseMapInfoByAliasOnly`, so a second `#pragma weak` for the same 
alias at another location is rejected by the set, but `find` cannot locate the 
stored entry and `insert` returns `{end(), false}`. The `= delete` was 
deliberate (commit ce21c926f8ef, D121927: one record per alias name). No caller 
reads `.first` yet, so this is latent, but the new contract does not hold for 
any `SetVector` whose element `operator==` is stricter than its set's equality.

```cpp
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SetVector.h"
#include <cstdio>

// operator== is stricter than the set's equality, as with WeakInfo
// (alias + location) vs. WeakInfo::DenseMapInfoByAliasOnly (alias only).
struct Rec {
  int Key, Loc;
  bool operator==(const Rec &O) const { return Key == O.Key && Loc == O.Loc; }
};
struct ByKeyOnly {
  static unsigned getHashValue(const Rec &R) { return R.Key; }
  static bool isEqual(const Rec &L, const Rec &R) { return L.Key == R.Key; }
};

int main() {
  llvm::SetVector<Rec, llvm::SmallVector<Rec, 1>,
                  llvm::SmallDenseSet<Rec, 2, ByKeyOnly>>
      S;
  S.insert({1, 100});
  auto [It, Inserted] = S.insert({1, 200});
  std::printf("inserted=%d iterator_is_end=%d\n", Inserted, It == S.end());
}
```

```console
$ clang++ -std=c++17 -I llvm/include -I build/include weak-equality.cpp 
build/lib/libLLVMSupport.a && ./a.out
inserted=0 iterator_is_end=1
```


https://github.com/llvm/llvm-project/pull/232372
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to