Commit e9ca8d1c by Evan Brown Committed by Copybara-Service

Distinguish the debug message for the case of self-move-assigned swiss tables.

PiperOrigin-RevId: 671484965
Change-Id: Ia1da7db0db1f776d48c74efaeab7252445208088
parent 043fe3c1
...@@ -545,7 +545,10 @@ enum InvalidCapacity : size_t { ...@@ -545,7 +545,10 @@ enum InvalidCapacity : size_t {
kAboveMaxValidCapacity = ~size_t{} - 100, kAboveMaxValidCapacity = ~size_t{} - 100,
kReentrance, kReentrance,
kDestroyed, kDestroyed,
// These two must be last because we use `>= kMovedFrom` to mean moved-from.
kMovedFrom, kMovedFrom,
kSelfMovedFrom,
}; };
// Returns a pointer to a control byte group that can be used by empty tables. // Returns a pointer to a control byte group that can be used by empty tables.
...@@ -2911,7 +2914,7 @@ class raw_hash_set { ...@@ -2911,7 +2914,7 @@ class raw_hash_set {
ABSL_ATTRIBUTE_REINITIALIZES void clear() { ABSL_ATTRIBUTE_REINITIALIZES void clear() {
if (SwisstableGenerationsEnabled() && if (SwisstableGenerationsEnabled() &&
capacity() == InvalidCapacity::kMovedFrom) { capacity() >= InvalidCapacity::kMovedFrom) {
common().set_capacity(DefaultCapacity()); common().set_capacity(DefaultCapacity());
} }
AssertNotDebugCapacity(); AssertNotDebugCapacity();
...@@ -3596,7 +3599,7 @@ class raw_hash_set { ...@@ -3596,7 +3599,7 @@ class raw_hash_set {
inline void destructor_impl() { inline void destructor_impl() {
if (SwisstableGenerationsEnabled() && if (SwisstableGenerationsEnabled() &&
capacity() == InvalidCapacity::kMovedFrom) { capacity() >= InvalidCapacity::kMovedFrom) {
return; return;
} }
if (capacity() == 0) return; if (capacity() == 0) return;
...@@ -3778,7 +3781,8 @@ class raw_hash_set { ...@@ -3778,7 +3781,8 @@ class raw_hash_set {
// than using NDEBUG) to avoid issues in which NDEBUG is enabled in some // than using NDEBUG) to avoid issues in which NDEBUG is enabled in some
// translation units but not in others. // translation units but not in others.
if (SwisstableGenerationsEnabled()) { if (SwisstableGenerationsEnabled()) {
that.common().set_capacity(InvalidCapacity::kMovedFrom); that.common().set_capacity(this == &that ? InvalidCapacity::kSelfMovedFrom
: InvalidCapacity::kMovedFrom);
} }
if (!SwisstableGenerationsEnabled() || capacity() == DefaultCapacity() || if (!SwisstableGenerationsEnabled() || capacity() == DefaultCapacity() ||
capacity() > kAboveMaxValidCapacity) { capacity() > kAboveMaxValidCapacity) {
...@@ -3908,7 +3912,12 @@ class raw_hash_set { ...@@ -3908,7 +3912,12 @@ class raw_hash_set {
assert(capacity() != InvalidCapacity::kDestroyed && assert(capacity() != InvalidCapacity::kDestroyed &&
"Use of destroyed hash table."); "Use of destroyed hash table.");
if (SwisstableGenerationsEnabled() && if (SwisstableGenerationsEnabled() &&
ABSL_PREDICT_FALSE(capacity() == InvalidCapacity::kMovedFrom)) { ABSL_PREDICT_FALSE(capacity() >= InvalidCapacity::kMovedFrom)) {
if (capacity() == InvalidCapacity::kSelfMovedFrom) {
// If this log triggers, then a hash table was move-assigned to itself
// and then used again later without being reinitialized.
ABSL_RAW_LOG(FATAL, "Use of self-move-assigned hash table.");
}
ABSL_RAW_LOG(FATAL, "Use of moved-from hash table."); ABSL_RAW_LOG(FATAL, "Use of moved-from hash table.");
} }
} }
......
...@@ -2093,7 +2093,7 @@ TEST(Table, MoveSelfAssign) { ...@@ -2093,7 +2093,7 @@ TEST(Table, MoveSelfAssign) {
t = std::move(*&t); t = std::move(*&t);
if (SwisstableGenerationsEnabled()) { if (SwisstableGenerationsEnabled()) {
// NOLINTNEXTLINE(bugprone-use-after-move) // NOLINTNEXTLINE(bugprone-use-after-move)
EXPECT_DEATH_IF_SUPPORTED(t.contains("a"), ""); EXPECT_DEATH_IF_SUPPORTED(t.contains("a"), "self-move-assigned");
} }
// As long as we don't crash, it's fine. // As long as we don't crash, it's fine.
} }
...@@ -3689,14 +3689,14 @@ TEST(Table, MovedFromCallsFail) { ...@@ -3689,14 +3689,14 @@ TEST(Table, MovedFromCallsFail) {
t1.insert(1); t1.insert(1);
t2 = std::move(t1); t2 = std::move(t1);
// NOLINTNEXTLINE(bugprone-use-after-move) // NOLINTNEXTLINE(bugprone-use-after-move)
EXPECT_DEATH_IF_SUPPORTED(t1.contains(1), ""); EXPECT_DEATH_IF_SUPPORTED(t1.contains(1), "moved-from");
} }
{ {
ABSL_ATTRIBUTE_UNUSED IntTable t1; ABSL_ATTRIBUTE_UNUSED IntTable t1;
t1.insert(1); t1.insert(1);
ABSL_ATTRIBUTE_UNUSED IntTable t2(std::move(t1)); ABSL_ATTRIBUTE_UNUSED IntTable t2(std::move(t1));
// NOLINTNEXTLINE(bugprone-use-after-move) // NOLINTNEXTLINE(bugprone-use-after-move)
EXPECT_DEATH_IF_SUPPORTED(t1.contains(1), ""); EXPECT_DEATH_IF_SUPPORTED(t1.contains(1), "moved-from");
t1.clear(); // Clearing a moved-from table is allowed. t1.clear(); // Clearing a moved-from table is allowed.
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment