Check HasAbslContainerEq<key_type> before skipping value equality in raw_hash_set::operator== raw_hash_set::operator== sets kKeyEqIsValueEq to true when key_type and value_type are the same and key_equal is hash_default_eq<key_type>, skipping `PolicyTraits::element(slot) == *b_it` after lookup succeeds in the other container. However, when a user-defined key_type provides a nested absl_container_eq functor (for example, comparing only an ID field while key_type::operator== compares the full value), hash_default_eq<key_type> resolves to key_type::absl_container_eq rather than `std::equal_to<key_type>`. Skipping element operator== in that case violates the container equality contract (`std::equal` over value_type::operator==) and causes sets with unequal elements that share the same lookup key to compare equal. Require `!HasAbslContainerEq<key_type>::value` in kKeyEqIsValueEq so `PolicyTraits::element(slot) == *b_it` is still evaluated whenever hash_default_eq<key_type> delegates to a custom absl_container_eq. PiperOrigin-RevId: 1034219617
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 426a92f..92aaa4e 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h
@@ -3974,6 +3974,7 @@ // could ignore some fields that aren't ignored by operator==. static constexpr bool kKeyEqIsValueEq = std::is_same_v<key_type, value_type> && + !HasAbslContainerEq<key_type>::value && std::is_same_v<key_equal, hash_default_eq<key_type>>; if (!kKeyEqIsValueEq && !(unchecked_deref(it) == elem)) return false; }
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index d3bd363..666ea24 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc
@@ -3945,6 +3945,40 @@ u.insert(std::begin(v2), std::end(v2)); EXPECT_NE(u, t); } + +struct KeyWithCustomContainerEq { + int id; + int payload; + + struct Hash { + using is_transparent = void; + size_t operator()(const KeyWithCustomContainerEq& k) const { + return absl::HashOf(k.id); + } + }; + struct Eq { + using is_transparent = void; + bool operator()(const KeyWithCustomContainerEq& a, + const KeyWithCustomContainerEq& b) const { + return a.id == b.id; + } + }; + using absl_container_hash = Hash; + using absl_container_eq = Eq; + + bool operator==(const KeyWithCustomContainerEq& other) const { + return id == other.id && payload == other.payload; + } +}; + +TEST(Table, EqualityComparesValueWhenUsingAbslContainerEq) { + absl::flat_hash_set<KeyWithCustomContainerEq> a = {{1, 10}}; + absl::flat_hash_set<KeyWithCustomContainerEq> b = {{1, 20}}; + absl::flat_hash_set<KeyWithCustomContainerEq> c = {{1, 10}}; + EXPECT_NE(a, b); + EXPECT_EQ(a, c); +} + TEST(Table, NoThrowMoveConstruct) { ASSERT_TRUE( std::is_nothrow_copy_constructible_v<absl::Hash<absl::string_view>>);