Skip to content

Commit

Permalink
Fix first doc comment inside macro yield (#15050)
Browse files Browse the repository at this point in the history
The following example eats the doc comment:

```cr
macro foo
  {{yield}}
end

foo do
  # doc comment
  def test
  end
end
```

This is because the first line of comment is generated on the same line as the `begin` which is inserted when using `{{yield}}`, like so:

```cr
begin # doc comment
  def test
  end
end
```

Using a newline instead of whitespace after `begin` in macro yield fixes this.

---------

Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Co-authored-by: Julien Portalier <julien@portalier.com>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent 1df4b23 commit a576c9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions spec/compiler/semantic/macro_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,21 @@ describe "Semantic: macro" do
CRYSTAL
end

it "begins with {{ yield }} (#15050)" do
result = top_level_semantic <<-CRYSTAL, wants_doc: true
macro foo
{{yield}}
end
foo do
# doc comment
def test
end
end
CRYSTAL
result.program.defs.try(&.["test"][0].def.doc).should eq "doc comment"
end

it "can return class type in macro def" do
assert_type(<<-CRYSTAL) { types["Int32"].metaclass }
class Foo
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module Crystal
if (loc = @last.location) && loc.filename.is_a?(String) || is_yield
macro_expansion_pragmas = @macro_expansion_pragmas ||= {} of Int32 => Array(Lexer::LocPragma)
(macro_expansion_pragmas[@str.pos.to_i32] ||= [] of Lexer::LocPragma) << Lexer::LocPushPragma.new
@str << "begin " if is_yield
@str << "begin\n" if is_yield
@last.to_s(@str, macro_expansion_pragmas: macro_expansion_pragmas, emit_doc: true)
@str << " end" if is_yield
(macro_expansion_pragmas[@str.pos.to_i32] ||= [] of Lexer::LocPragma) << Lexer::LocPopPragma.new
Expand Down

0 comments on commit a576c9d

Please sign in to comment.