Skip to content

Commit d236068

Browse files
authored
[NFC] Wrap Bool::Element (#8149)
Rather than using `bool` directly. This avoids problems with getting the `std::vector<bool>` overload by accident.
1 parent f99fdb7 commit d236068

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

src/analysis/lattices/bool.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,30 @@ namespace wasm::analysis {
2424
// A lattice with two elements: the top element is `true` and the bottom element
2525
// is `false`.
2626
struct Bool {
27-
using Element = bool;
27+
// Use a wrapper around bool to avoid triggering bool specializations of e.g.
28+
// std::vector.
29+
struct Element {
30+
bool val = false;
31+
Element(bool val) : val(val) {}
32+
Element() = default;
33+
Element(const Element& other) = default;
34+
Element(Element&& other) = default;
35+
operator bool() const noexcept { return val; }
36+
bool operator==(const Element& other) const noexcept {
37+
return val == other.val;
38+
}
39+
bool operator==(bool other) const noexcept { return val == other; }
40+
bool operator!=(const Element& other) const noexcept {
41+
return !(*this == other);
42+
}
43+
bool operator!=(bool other) const noexcept { return !(*this == other); }
44+
Element& operator=(const Element& other) = default;
45+
Element& operator=(Element&& other) = default;
46+
Element& operator=(bool other) {
47+
val = other;
48+
return *this;
49+
}
50+
};
2851
Element getBottom() const noexcept { return false; }
2952
Element getTop() const noexcept { return true; }
3053
LatticeComparison compare(Element a, Element b) const noexcept {
@@ -37,13 +60,19 @@ struct Bool {
3760
}
3861
return false;
3962
}
63+
bool join(Element& joinee, bool joiner) const noexcept {
64+
return join(joinee, Element(joiner));
65+
}
4066
bool meet(Element& meetee, Element meeter) const noexcept {
4167
if (meetee && !meeter) {
4268
meetee = meeter;
4369
return true;
4470
}
4571
return false;
4672
}
73+
bool meet(Element& meetee, bool meeter) const noexcept {
74+
return meet(meetee, Element(meeter));
75+
}
4776
};
4877

4978
#if __cplusplus >= 202002L

test/gtest/lattices.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ TEST(BoolLattice, Compare) {
161161

162162
TEST(BoolLattice, Join) {
163163
analysis::Bool lattice;
164-
bool elem = false;
164+
analysis::Bool::Element elem = false;
165165

166166
EXPECT_FALSE(lattice.join(elem, false));
167167
ASSERT_FALSE(elem);
@@ -178,7 +178,7 @@ TEST(BoolLattice, Join) {
178178

179179
TEST(BoolLattice, Meet) {
180180
analysis::Bool lattice;
181-
bool elem = true;
181+
analysis::Bool::Element elem = true;
182182

183183
EXPECT_FALSE(lattice.meet(elem, true));
184184
ASSERT_TRUE(elem);
@@ -276,7 +276,7 @@ TEST(InvertedLattice, Compare) {
276276

277277
TEST(InvertedLattice, Join) {
278278
analysis::Inverted inverted(analysis::Bool{});
279-
bool elem = true;
279+
analysis::Bool::Element elem = true;
280280

281281
EXPECT_FALSE(inverted.join(elem, true));
282282
ASSERT_TRUE(elem);
@@ -293,7 +293,7 @@ TEST(InvertedLattice, Join) {
293293

294294
TEST(InvertedLattice, Meet) {
295295
analysis::Inverted inverted(analysis::Bool{});
296-
bool elem = false;
296+
analysis::Bool::Element elem = false;
297297

298298
EXPECT_FALSE(inverted.meet(elem, false));
299299
ASSERT_FALSE(elem);
@@ -445,12 +445,14 @@ TEST(LiftLattice, Join) {
445445

446446
TEST(ArrayLattice, GetBottom) {
447447
analysis::Array<analysis::Bool, 2> array{analysis::Bool{}};
448-
EXPECT_EQ(array.getBottom(), (std::array<bool, 2>{false, false}));
448+
EXPECT_EQ(array.getBottom(),
449+
(std::array<analysis::Bool::Element, 2>{false, false}));
449450
}
450451

451452
TEST(ArrayLattice, GetTop) {
452453
analysis::Array<analysis::Bool, 2> array{analysis::Bool{}};
453-
EXPECT_EQ(array.getTop(), (std::array<bool, 2>{true, true}));
454+
EXPECT_EQ(array.getTop(),
455+
(std::array<analysis::Bool::Element, 2>{true, true}));
454456
}
455457

456458
TEST(ArrayLattice, Compare) {
@@ -473,12 +475,14 @@ TEST(ArrayLattice, Meet) {
473475

474476
TEST(VectorLattice, GetBottom) {
475477
analysis::Vector<analysis::Bool> vector{analysis::Bool{}, 2};
476-
EXPECT_EQ(vector.getBottom(), (std::vector<bool>{false, false}));
478+
EXPECT_EQ(vector.getBottom(),
479+
(std::vector<analysis::Bool::Element>{false, false}));
477480
}
478481

479482
TEST(VectorLattice, GetTop) {
480483
analysis::Vector<analysis::Bool> vector{analysis::Bool{}, 2};
481-
EXPECT_EQ(vector.getTop(), (std::vector<bool>{true, true}));
484+
EXPECT_EQ(vector.getTop(),
485+
(std::vector<analysis::Bool::Element>{true, true}));
482486
}
483487

484488
TEST(VectorLattice, Compare) {
@@ -505,10 +509,10 @@ TEST(VectorLattice, JoinSingleton) {
505509
auto elem = vector.getBottom();
506510

507511
EXPECT_FALSE(vector.join(elem, Vec::SingletonElement(0, false)));
508-
EXPECT_EQ(elem, (std::vector{false, false}));
512+
EXPECT_EQ(elem, (std::vector<analysis::Bool::Element>{false, false}));
509513

510514
EXPECT_TRUE(vector.join(elem, Vec::SingletonElement(1, true)));
511-
EXPECT_EQ(elem, (std::vector{false, true}));
515+
EXPECT_EQ(elem, (std::vector<analysis::Bool::Element>{false, true}));
512516
}
513517

514518
TEST(VectorLattice, MeetSingleton) {
@@ -517,10 +521,10 @@ TEST(VectorLattice, MeetSingleton) {
517521
auto elem = vector.getTop();
518522

519523
EXPECT_FALSE(vector.meet(elem, Vec::SingletonElement(1, true)));
520-
EXPECT_EQ(elem, (std::vector{true, true}));
524+
EXPECT_EQ(elem, (std::vector<analysis::Bool::Element>{true, true}));
521525

522526
EXPECT_TRUE(vector.meet(elem, Vec::SingletonElement(0, false)));
523-
EXPECT_EQ(elem, (std::vector{false, true}));
527+
EXPECT_EQ(elem, (std::vector<analysis::Bool::Element>{false, true}));
524528
}
525529

526530
TEST(TupleLattice, GetBottom) {
@@ -707,7 +711,7 @@ TEST(SharedLattice, JoinVecSingleton) {
707711

708712
auto elem = shared.getBottom();
709713
EXPECT_TRUE(shared.join(elem, Vec::SingletonElement(1, true)));
710-
EXPECT_EQ(*elem, (std::vector{false, true}));
714+
EXPECT_EQ(*elem, (std::vector<analysis::Bool::Element>{false, true}));
711715
}
712716

713717
TEST(SharedLattice, JoinInvertedVecSingleton) {
@@ -717,7 +721,7 @@ TEST(SharedLattice, JoinInvertedVecSingleton) {
717721

718722
auto elem = shared.getBottom();
719723
EXPECT_TRUE(shared.join(elem, Vec::SingletonElement(1, false)));
720-
EXPECT_EQ(*elem, (std::vector{true, false}));
724+
EXPECT_EQ(*elem, (std::vector<analysis::Bool::Element>{true, false}));
721725
}
722726

723727
TEST(StackLattice, GetBottom) {

0 commit comments

Comments
 (0)