Skip to content

Commit

Permalink
Fixed #3110: splated arguments instrumentation
Browse files Browse the repository at this point in the history
Brian J. Cardiff committed Aug 8, 2016

Verified

This commit was signed with the committer’s verified signature.
headius Charles Oliver Nutter
1 parent 349c90b commit 27b5652
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/crystal/tools/playground_spec.cr
Original file line number Diff line number Diff line change
@@ -131,6 +131,8 @@ describe Playground::AgentInstrumentorTransformer do
it "instrument puts with args" do
assert_agent %(puts 3), %(puts($p.i(1) { 3 }))
assert_agent %(puts a, 2, b), %(puts(*$p.i(1, ["a", "2", "b"]) { {a, 2, b} }))
assert_agent %(puts *{3}), %(puts(*$p.i(1, ["3"]) { {3} }))
assert_agent %(puts *{3,a}), %(puts(*$p.i(1, ["3", "a"]) { {3,a} }))
assert_agent_eq %(puts), %(puts)
end

Original file line number Diff line number Diff line change
@@ -70,13 +70,16 @@ module Crystal

private def instrument(node, add_as_typeof = false)
if (location = node.location) && location.line_number != ignore_line
splat = node.is_a?(Splat)
node = node.exp if node.is_a?(Splat)

This comment has been minimized.

Copy link
@Sija

Sija Aug 9, 2016

Contributor

you could drop 2nd is_a? check by using already defined splat variable.

node = node.exp if splat

This comment has been minimized.

Copy link
@asterite

asterite Aug 9, 2016

Member

Nope, because the compiler doesn't know that if splat is true then node is a Splat.

This comment has been minimized.

Copy link
@Sija

Sija Aug 9, 2016

Contributor

Ahaa, so it's either repeated is_a? call or as(Splat)?
PS. I hope you don't mind me bothering you about the code too much ;)

This comment has been minimized.

Copy link
@bcardiff

bcardiff Aug 9, 2016

Member

@Sija exactly. And regarding you looking at the code: not at all. It's totally understandable your doubt here.

This comment has been minimized.

Copy link
@asterite

asterite Aug 9, 2016

Member

@Sija on the contrary, it's really nice to have a pair of eyes reviewing our commits :-)

This comment has been minimized.

Copy link
@Sija

Sija Aug 10, 2016

Contributor

Cool, I'm using Crystal just for a month and I'm already sleep deprived, but deep in love ;)
Thanks for your constructive attitude guys, and for the language of course!

@nested_block_visitor.not_nil!.accept(node)
args = [NumberLiteral.new(location.line_number)] of ASTNode
if node.is_a?(TupleLiteral)
args << ArrayLiteral.new(node.elements.map { |e| StringLiteral.new(e.to_s).as(ASTNode) })
end
call = Call.new(Global.new("$p"), "i", args, Block.new([] of Var, node.as(ASTNode)))
call = Cast.new(call, TypeOf.new([node.clone] of ASTNode)) if add_as_typeof
call = Splat.new(call) if splat
call
else
node

0 comments on commit 27b5652

Please sign in to comment.