Skip to content

Conversation

@filipsajdak
Copy link
Contributor

@filipsajdak filipsajdak commented Apr 11, 2023

In the current implementation of cppfront (f83ca9b), the following code:

a: type = { } // a
i : int = 42;

or

a: namespace = { } // a
i : int = 42;

End with ICE not all comments were printed error.

This change removes extra new-line printed after namespace or UDT definition that solves this issue.

All regression tests pass (except for two files impacted by missing newlines - updated in this PR).

Fix parsing of the following code:
```cpp
a: type = { } // a
i : int = 42;
```
The current implementation fail with ICE. After this change works and generate correct code.

Only one test change behaviour (due to lack of new line on the end of class definition):
```patch
diff --git a/regression-tests/test-results/pure2-types-order-independence-and-nesting.cpp b/regression-tests/test-results/pure2-types-order-independence-and-nesting.cpp
index 9449c2a..5ace9c1 100644
--- a/regression-tests/test-results/pure2-types-order-independence-and-nesting.cpp
+++ b/regression-tests/test-results/pure2-types-order-independence-and-nesting.cpp
@@ -69,8 +69,8 @@ namespace M {
 template<typename T, typename U> class A {
     public: template<int I> class B {
         public: template<typename V, int J, typename W> static auto f(W const& w) -> void;
-public: B() = default; B(B const&) = delete; auto operator=(B const&) -> void = delete; };
-public: A() = default; A(A const&) = delete; auto operator=(A const&) -> void = delete; };
+public: B() = default; B(B const&) = delete; auto operator=(B const&) -> void = delete; };public: A() = default; A(A const&) = delete; auto operator=(A const&) -> void = delete;
+};

 }
```
The current implementation of cppfront ends with ICE
when parsing the following code:
```cpp
a: namespace = { } // a
i : int = 42;
```

After this change code is parsed and cppfront generates correct code.
@JohelEGP
Copy link
Contributor

JohelEGP commented Apr 11, 2023

Thank you. I still get it on this:

Still ICEs
a: type = {
  b: int = 0;
}
c: a = (); // Directly initializes `b` from `0`.

// If we could make an aggregate:
// d: type = {
//   public e: int;
// }
// f: d = (0); // Directly initializes `e` from `0`.

g: type = {
  h: int;
  operator=: (out this, x: int) = h = x;
}
i: g = 0; // Initializes `x` from `0`, then `h` from `x`.

j: type = { }
k: type = {
  l: int = 0;
  this: j = ();
  m: int = 0;
  operator=: (out this) = { }
  operator=: (out this, x: int, y: int) = {
    l = x;
    m = y;
  }
}
// Initializes `QoI` from `0`, then `l` from `std::move(QoI)`.
n: k = (); // Directly initializes `m` from `0`.
// Initializes `x` from `0`, then `QoI` from `x`, then `l` from `std::move(QoI)`.
o: k = (0, 1); // Initializes `y` from `1`, then `m` from `y`.

The last line suffices for the ICE:

o: k = (0, 1); // Initializes `y` from `1`, then `m` from `y`.

@filipsajdak
Copy link
Contributor Author

@JohelEGP yes, I have noticed that. I am looking for a fix.

@filipsajdak filipsajdak changed the title Fix unpublished comments ICE error - closes #344 Fix unpublished comments ICE error Apr 11, 2023
@filipsajdak
Copy link
Contributor Author

#344 is not fixed by this PR (I fixed two other issues) - more description about the original issue here: #344 (comment)

@hsutter
Copy link
Owner

hsutter commented Apr 14, 2023

Thanks! BTW I made that comment check such a noisy ICE because it's super important to me to never drop anything the programmer wrote, including (or even especially?) comments. Thanks for looking into this.

@hsutter hsutter closed this in d7adb8f Apr 15, 2023
@hsutter
Copy link
Owner

hsutter commented Apr 15, 2023

Thanks for the bug reports and repros! These should all work now, fixed as a separate commit (see commit message), please check... and I did take the part of the suggested PRs which was to do the extra finalizing at the end of each section (not just at the very end of the file). 🙏

@filipsajdak
Copy link
Contributor Author

Checking...

@filipsajdak filipsajdak deleted the fsajdak-fix-unpublished-comments branch April 15, 2023 23:45
@filipsajdak
Copy link
Contributor Author

I confirm that it works on my tests as well.

zaucy pushed a commit to zaucy/cppfront that referenced this pull request Dec 5, 2023
…loses hsutter#351, closes hsutter#354

Thanks for these test cases. The common issue was that occasionally an emitted piece of Cpp1 contained a newline character, which was not accounted for in the code that did comment merging.

Thanks also for the PRs for proposed fixes. I looked at those too and tried them out. In the end, I found that what worked best and most generally was to break apart strings that contain newlines into one chunk per line, and this allowed the comment merging to happen correctly by avoiding the hidden-to-the-comment-merger line change.

So this commit is going with that solution rather than the other proposed PRs, but the other PRs helped find this root cause and shed light on a general solution, as did the test cases. Thanks again for both!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants