Skip to content

Commit d407ef1

Browse files
authored
Cherry-picks for LTS 20260107 (#1990)
* Fix unused variable warning in GCC7 PiperOrigin-RevId: 853321697 Change-Id: I44b09e10e6ca1bb9b9f30a01a0f5acd5923371c1 * Fix self-move handling in absl::linked_hash_{set|map} PiperOrigin-RevId: 853340964 Change-Id: I818695f01d53b5515be24122e1988e69ad6421d4
1 parent 8d0221c commit d407ef1

5 files changed

Lines changed: 84 additions & 25 deletions

File tree

absl/container/linked_hash_map.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,25 @@ class linked_hash_map {
248248
}
249249

250250
linked_hash_map& operator=(const linked_hash_map& other) {
251-
if (this == &other) return *this;
252-
// Make a new set, with other's hash/eq/alloc.
253-
set_ = SetType(other.bucket_count(), other.set_.hash_function(),
254-
other.set_.key_eq(), other.get_allocator());
255-
// Copy the list, with other's allocator.
256-
list_ = ListType(other.get_allocator());
257-
CopyFrom(other);
251+
if (this != &other) {
252+
// Make a new set, with other's hash/eq/alloc.
253+
set_ = SetType(other.bucket_count(), other.set_.hash_function(),
254+
other.set_.key_eq(), other.get_allocator());
255+
// Copy the list, with other's allocator.
256+
list_ = ListType(other.get_allocator());
257+
CopyFrom(other);
258+
}
258259
return *this;
259260
}
260261

261262
linked_hash_map& operator=(linked_hash_map&& other) noexcept {
262-
// underlying containers will handle progagate_on_container_move details
263-
set_ = std::move(other.set_);
264-
list_ = std::move(other.list_);
265-
other.set_.clear();
266-
other.list_.clear();
263+
if (this != &other) {
264+
// underlying containers will handle progagate_on_container_move details
265+
set_ = std::move(other.set_);
266+
list_ = std::move(other.list_);
267+
other.set_.clear();
268+
other.list_.clear();
269+
}
267270
return *this;
268271
}
269272

absl/container/linked_hash_map_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ TEST(LinkedHashMapTest, Assign) {
9898
FAIL() << "Assigned map's find method returned an invalid iterator.";
9999
}
100100

101+
// Tests that self-assignment works.
102+
TEST(LinkedHashMapTest, SelfAssign) {
103+
linked_hash_map<int, int> a{{1, 1}, {2, 2}, {3, 3}};
104+
auto& a_ref = a;
105+
a = a_ref;
106+
EXPECT_THAT(a, ElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3)));
107+
}
108+
101109
// Tests that move constructor works.
102110
TEST(LinkedHashMapTest, Move) {
103111
// Use unique_ptr as an example of a non-copyable type.
@@ -108,6 +116,14 @@ TEST(LinkedHashMapTest, Move) {
108116
EXPECT_THAT(n, ElementsAre(Pair(2, Pointee(12)), Pair(3, Pointee(13))));
109117
}
110118

119+
// Tests that self-moving works.
120+
TEST(LinkedHashMapTest, SelfMove) {
121+
linked_hash_map<int, int> a{{1, 1}, {2, 2}, {3, 3}};
122+
auto& a_ref = a;
123+
a = std::move(a_ref);
124+
EXPECT_THAT(a, ElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3)));
125+
}
126+
111127
TEST(LinkedHashMapTest, CanInsertMoveOnly) {
112128
linked_hash_map<int, std::unique_ptr<int>> m;
113129
struct Data {
@@ -512,6 +528,13 @@ TEST(LinkedHashMapTest, Swap) {
512528
ASSERT_EQ(2, m2.size());
513529
}
514530

531+
TEST(LinkedHashMapTest, SelfSwap) {
532+
linked_hash_map<int, int> a{{1, 1}, {2, 2}, {3, 3}};
533+
using std::swap;
534+
swap(a, a);
535+
EXPECT_THAT(a, ElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3)));
536+
}
537+
515538
TEST(LinkedHashMapTest, InitializerList) {
516539
linked_hash_map<int, int> m{{1, 2}, {3, 4}};
517540
ASSERT_EQ(2, m.size());

absl/container/linked_hash_set.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,24 @@ class linked_hash_set {
238238
}
239239

240240
linked_hash_set& operator=(const linked_hash_set& other) {
241-
if (this == &other) return *this;
242-
// Make a new set, with other's hash/eq/alloc.
243-
set_ = SetType(other.bucket_count(), other.set_.hash_function(),
244-
other.set_.key_eq(), other.get_allocator());
245-
// Copy the list, with other's allocator.
246-
list_ = ListType(other.get_allocator());
247-
CopyFrom(other);
241+
if (this != &other) {
242+
// Make a new set, with other's hash/eq/alloc.
243+
set_ = SetType(other.bucket_count(), other.set_.hash_function(),
244+
other.set_.key_eq(), other.get_allocator());
245+
// Copy the list, with other's allocator.
246+
list_ = ListType(other.get_allocator());
247+
CopyFrom(other);
248+
}
248249
return *this;
249250
}
250251

251252
linked_hash_set& operator=(linked_hash_set&& other) noexcept {
252-
set_ = std::move(other.set_);
253-
list_ = std::move(other.list_);
254-
other.set_.clear();
255-
other.list_.clear();
253+
if (this != &other) {
254+
set_ = std::move(other.set_);
255+
list_ = std::move(other.list_);
256+
other.set_.clear();
257+
other.list_.clear();
258+
}
256259
return *this;
257260
}
258261

absl/container/linked_hash_set_test.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ TEST(LinkedHashSetTest, Assign) {
9191
FAIL() << "Assigned set's find method returned an invalid iterator.";
9292
}
9393

94+
// Tests that self-assignment works.
95+
TEST(LinkedHashSetTest, SelfAssign) {
96+
linked_hash_set<int> a{1, 2, 3};
97+
auto& a_ref = a;
98+
a = a_ref;
99+
100+
EXPECT_TRUE(a.contains(2));
101+
auto found = a.find(2);
102+
ASSERT_TRUE(found != a.end());
103+
for (auto iter = a.begin(); iter != a.end(); ++iter) {
104+
if (iter == found) return;
105+
}
106+
FAIL() << "Assigned set's find method returned an invalid iterator.";
107+
}
108+
94109
// Tests that move constructor works.
95110
TEST(LinkedHashSetTest, Move) {
96111
// Use unique_ptr as an example of a non-copyable type.
@@ -101,6 +116,14 @@ TEST(LinkedHashSetTest, Move) {
101116
EXPECT_THAT(n, ElementsAre(Pointee(2), Pointee(3)));
102117
}
103118

119+
// Tests that self-moving works.
120+
TEST(LinkedHashSetTest, SelfMove) {
121+
linked_hash_set<int> a{1, 2, 3};
122+
auto& a_ref = a;
123+
a = std::move(a_ref);
124+
EXPECT_THAT(a, ElementsAre(1, 2, 3));
125+
}
126+
104127
struct IntUniquePtrHash {
105128
size_t operator()(const std::unique_ptr<int>& p) const {
106129
return static_cast<size_t>(*p);
@@ -521,6 +544,13 @@ TEST(LinkedHashSetTest, Swap) {
521544
ASSERT_EQ(2, m2.size());
522545
}
523546

547+
TEST(LinkedHashSetTest, SelfSwap) {
548+
linked_hash_set<int> a{1, 2, 3};
549+
using std::swap;
550+
swap(a, a);
551+
EXPECT_THAT(a, ElementsAre(1, 2, 3));
552+
}
553+
524554
TEST(LinkedHashSetTest, InitializerList) {
525555
linked_hash_set<int> m{1, 3};
526556
ASSERT_EQ(2, m.size());

absl/strings/internal/str_format/float_conversion.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ void FormatEPositiveExpSlow(uint128 mantissa, int exp, bool uppercase,
17451745
bool change_to_zeros = false;
17461746
if (nines + 1 >= digits_to_go) {
17471747
// Everything we need to print is in the first DigitRun
1748-
auto [next_digit_opt, next_nines] = GetDigits(btd, digits_view);
1748+
auto next_digit_opt = GetDigits(btd, digits_view).digit;
17491749
if (nines == state.precision) {
17501750
change_to_zeros = next_digit_opt.value_or(0) > 4;
17511751
} else {
@@ -1793,7 +1793,7 @@ void FormatEPositiveExpSlow(uint128 mantissa, int exp, bool uppercase,
17931793
digits_to_go -= curr_nines + 1;
17941794
} else {
17951795
bool need_round_up = false;
1796-
auto [next_digit_opt, next_nines] = GetDigits(btd, digits_view);
1796+
auto next_digit_opt = GetDigits(btd, digits_view).digit;
17971797
if (digits_to_go == 1) {
17981798
need_round_up = curr_nines > 0 || next_digit_opt > 4;
17991799
} else if (digits_to_go == curr_nines + 1) {

0 commit comments

Comments
 (0)