Skip to content

More robust trailing expressions newline implementation #15614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Blacksmoke16
Copy link
Member

Follow up to #15305 to handle another edge case I noticed.

Before this PR if you had a macro expression whose body is an Expressions node, and the last node of those expressions was a Block or If statement, the end keyword would be on the wrong line. For example

{%
  to_process = [] of Nil

  to_process.each do
    b = 2
    a = 1
  end
%}

would be stringified as:

{%
  to_process = [] of Nil

  to_process.each do
    b = 2
    a = 1 end
%}

This PR resolves this and adds more test coverage on similar cases.

Supersedes #15613

@straight-shoota
Copy link
Member

Why tag this as topic:lang:macros? This doesn't change macros on the language level. It's just a compiler change related to syntax representation, which is usually tagged as compiler:parser. So I think this was previously correct.

@Blacksmoke16
Copy link
Member Author

Blacksmoke16 commented Apr 27, 2025

My thinking was that stringification is related to macros so this might results in more accurate macro expansion errors. But not entirely sure that's accurate.

Yea, if you have:

macro finished
  {% verbatim do %}
    {%
      to_process = [] of Nil

      to_process.each do
        b = 2
        a = 1
      end

      raise "oh noes"
    %}
  {% end %}
end

Without this PR:

$ crystal run --error-trace test.cr
In test.cr:1:1

 1 | macro finished
     ^
Error: expanding macro

...

 >  1 |
 >  2 |     {%
 >  3 |   to_process = [] of Nil
 >  4 |
 >  5 |   to_process.each do
 >  6 |     b = 2
 >  7 |     a = 1  end
 >  8 |
 >  9 |   raise("oh noes")
 > 10 | %}
 > 11 |
Error: oh noes

With this PR:

$ ccrystal run --error-trace test.cr
Using compiled compiler at /home/george/dev/git/crystal/.build/crystal
In test.cr:1:1

 1 | macro finished
     ^
Error: expanding macro

...

 >  1 |
 >  2 |     {%
 >  3 |   to_process = [] of Nil
 >  4 |
 >  5 |   to_process.each do
 >  6 |     b = 2
 >  7 |     a = 1
 >  8 |   end
 >  9 |
 > 10 |   raise("oh noes")
 > 11 | %}
 > 12 |
Error: oh noes

@straight-shoota
Copy link
Member

No, this is really just compiler behaviour. Not a language feature change.

@straight-shoota straight-shoota merged commit f62b373 into crystal-lang:master Apr 29, 2025
36 checks passed
@Blacksmoke16 Blacksmoke16 deleted the nested-expressions-newlines branch April 29, 2025 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants