Skip to content

Commit ef80c8e

Browse files
committed
refactor(diag): allow Diag_List to be moved
1 parent 7ad72b3 commit ef80c8e

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/quick-lint-js/diag/diag-list.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ static_assert(alignof(Diag_List::Node) == alignof(Diag_List::Node_Base),
2323

2424
Diag_List::Diag_List(Memory_Resource *memory) : memory_(memory) {}
2525

26+
Diag_List::Diag_List(Diag_List &&other)
27+
: memory_(other.memory_), first_(other.first_), last_(other.last_) {
28+
other.clear();
29+
}
30+
2631
Diag_List::~Diag_List() { this->clear(); }
2732

2833
void Diag_List::add_many(const Diag_List &other) {

src/quick-lint-js/diag/diag-list.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ class Diag_List {
3636

3737
explicit Diag_List(Memory_Resource *);
3838

39-
Diag_List(Diag_List &&) = delete;
40-
Diag_List &operator=(Diag_List &&) = delete;
39+
Diag_List(const Diag_List &) = delete;
40+
Diag_List &operator=(const Diag_List &) = delete;
41+
42+
Diag_List(Diag_List &&);
43+
Diag_List &operator=(Diag_List &&) = delete; // TODO(strager)
4144

4245
~Diag_List();
4346

test/test-diag-list.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <quick-lint-js/diag-matcher.h>
88
#include <quick-lint-js/diag/diag-list.h>
99
#include <quick-lint-js/diag/diagnostic-types.h>
10+
#include <quick-lint-js/diagnostic-assertion.h>
1011
#include <quick-lint-js/fe/lex.h>
1112
#include <quick-lint-js/identifier-support.h>
1213
#include <type_traits>
@@ -189,6 +190,41 @@ TEST(Test_Diag_List, pretty_print_one_diag) {
189190
EXPECT_EQ(ss.str(), "1 diagnostic: {\n Diag_Let_With_No_Bindings,\n}");
190191
}
191192
}
193+
194+
TEST(Test_Diag_List, moving_diag_list_clears_original) {
195+
Linked_Bump_Allocator memory("test");
196+
Diag_List diags_1(&memory);
197+
Padded_String code(u8"hello"_sv);
198+
diags_1.add(Diag_Let_With_No_Bindings{.where = span_of(code)});
199+
Diag_List diags_2 = std::move(diags_1);
200+
EXPECT_EQ(diags_1.size(), 0);
201+
}
202+
203+
TEST(Test_Diag_List, moving_diag_list_keeps_single_diag) {
204+
Linked_Bump_Allocator memory("test");
205+
Diag_List diags_1(&memory);
206+
Padded_String code(u8"hello"_sv);
207+
diags_1.add(Diag_Let_With_No_Bindings{.where = span_of(code)});
208+
209+
Diag_List diags_2 = std::move(diags_1);
210+
EXPECT_EQ(diags_2.size(), 1);
211+
}
212+
213+
TEST(Test_Diag_List, moving_diag_list_keeps_multiple_diags) {
214+
Linked_Bump_Allocator memory("test");
215+
Diag_List diags_1(&memory);
216+
Padded_String code(u8"hello"_sv);
217+
diags_1.add(Diag_Let_With_No_Bindings{.where = span_of(code)});
218+
diags_1.add(Diag_Expected_Parenthesis_Around_If_Condition{
219+
.where = span_of(code),
220+
.token = u8')',
221+
});
222+
223+
Diag_List diags_2 = std::move(diags_1);
224+
assert_diagnostics(&code, diags_2,
225+
{u8"Diag_Let_With_No_Bindings"_diag,
226+
u8"Diag_Expected_Parenthesis_Around_If_Condition"_diag});
227+
}
192228
}
193229
}
194230

0 commit comments

Comments
 (0)