Skip to content
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

Fix first doc comment inside macro yield #15050

Merged

Conversation

RX14
Copy link
Contributor

@RX14 RX14 commented Sep 30, 2024

The following example eats the doc comment:

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:

begin # doc comment
  def test
  end
end

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

@RX14 RX14 added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler topic:lang:macros topic:tools:docs-generator labels Sep 30, 2024
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be changed accordingly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a situation where this could cause harm.

@straight-shoota
Copy link
Member

I added a spec for this using the example from the OP.

spec/compiler/semantic/macro_spec.cr Outdated Show resolved Hide resolved
@straight-shoota straight-shoota added this to the 1.15.0 milestone Dec 16, 2024
@straight-shoota straight-shoota merged commit a576c9d into crystal-lang:master Dec 17, 2024
68 of 69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler topic:lang:macros topic:tools:docs-generator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants