Skip to content

Commit

Permalink
Add location to result from macro method interpretation
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Nov 21, 2024
1 parent d57c9d8 commit 2cbca7f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ module Crystal
assert_macro %({{[1, 2, 3].map { |e| e == 2 }}}), "[false, true, false]"
end

it "executes map assigns location" do
assert_macro %({{[1].map { |e| e }.filename }}), %("")
end

it "executes reduce with no initial value" do
assert_macro %({{[1, 2, 3].reduce { |acc, val| acc * val }}}), "6"
end
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ module Crystal
block = node.block.try { |b| @program.normalize(b) }

begin
@last = receiver.interpret(node.name, args, named_args, block, self, node.name_location)
result = receiver.interpret(node.name, args, named_args, block, self, node.name_location)
rescue ex : MacroRaiseException
# Re-raise to avoid the logic in the other rescue blocks and to retain the original location
raise ex
Expand All @@ -376,6 +376,8 @@ module Crystal
rescue ex
node.raise ex.message
end
result.location ||= node.name_location
@last = result
else
# no receiver: special calls
# may raise `Crystal::TopLevelMacroRaiseException`
Expand Down

0 comments on commit 2cbca7f

Please sign in to comment.