Skip to content

Commit 5d3e324

Browse files
committed
Fix assertion on comments near end of file.
1 parent 9440687 commit 5d3e324

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
main: () -> int = {
3+
x := crash_m0();
4+
_ = x;
5+
}
6+
7+
crash_m0: type = {
8+
operator-: (this, _) -> int = 0;/* Comment starts here
9+
And continues here
10+
*/
11+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-bugfix-for-late-comments.cpp2"
10+
11+
#line 7 "pure2-bugfix-for-late-comments.cpp2"
12+
class crash_m0;
13+
14+
15+
//=== Cpp2 type definitions and function declarations ===========================
16+
17+
#line 1 "pure2-bugfix-for-late-comments.cpp2"
18+
19+
#line 2 "pure2-bugfix-for-late-comments.cpp2"
20+
[[nodiscard]] auto main() -> int;
21+
22+
#line 7 "pure2-bugfix-for-late-comments.cpp2"
23+
class crash_m0 {
24+
public: [[nodiscard]] auto operator-([[maybe_unused]] auto const& unnamed_param_2) const& -> int;
25+
public: crash_m0() = default;
26+
public: crash_m0(crash_m0 const&) = delete; /* No 'that' constructor, suppress copy */
27+
public: auto operator=(crash_m0 const&) -> void = delete;
28+
29+
30+
#line 11 "pure2-bugfix-for-late-comments.cpp2"
31+
};
32+
33+
34+
//=== Cpp2 function definitions =================================================
35+
36+
#line 1 "pure2-bugfix-for-late-comments.cpp2"
37+
38+
#line 2 "pure2-bugfix-for-late-comments.cpp2"
39+
[[nodiscard]] auto main() -> int{
40+
auto x {crash_m0()};
41+
static_cast<void>(cpp2::move(x));
42+
}
43+
44+
#line 8 "pure2-bugfix-for-late-comments.cpp2"
45+
[[nodiscard]] auto crash_m0::operator-([[maybe_unused]] auto const& unnamed_param_2) const& -> int { return 0; }/* Comment starts here
46+
And continues here
47+
*/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-late-comments.cpp2... ok (all Cpp2, passes safety checks)
2+

source/to_cpp1.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class positional_printer
432432
c.dbg_was_printed = true;
433433
}
434434

435-
auto flush_comments( source_position pos )
435+
auto flush_comments( source_position pos, bool print_remaining_comments = false )
436436
-> void
437437
{
438438
if (!pcomments) {
@@ -444,7 +444,7 @@ class positional_printer
444444

445445
// Add unprinted comments and blank lines as needed to catch up vertically
446446
//
447-
while (curr_pos.lineno < pos.lineno)
447+
while (print_remaining_comments ? (next_comment < std::ssize(comments)) : (curr_pos.lineno < pos.lineno))
448448
{
449449
// If a comment goes on this line, print it
450450
if (
@@ -468,7 +468,8 @@ class positional_printer
468468
)
469469
{
470470
print_comment( comments[next_comment] );
471-
assert(curr_pos.lineno <= pos.lineno); // we shouldn't have overshot
471+
if (!print_remaining_comments)
472+
assert(curr_pos.lineno <= pos.lineno); // we shouldn't have overshot
472473
}
473474

474475
++next_comment;
@@ -479,6 +480,9 @@ class positional_printer
479480
print("\n");
480481
}
481482
}
483+
// And catch up.
484+
while (curr_pos.lineno < pos.lineno)
485+
print("\n");
482486
}
483487

484488
auto print_unprinted_comments()
@@ -589,7 +593,7 @@ class positional_printer
589593
&& psource->has_cpp2()
590594
)
591595
{
592-
flush_comments( {curr_pos.lineno+1, 1} );
596+
flush_comments( {curr_pos.lineno+1, 1}, print_remaining_comments );
593597

594598
if (print_remaining_comments) {
595599
print_unprinted_comments();

0 commit comments

Comments
 (0)