Skip to content

Commit

Permalink
Clone macro default argument before macro expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and asterite committed Jan 10, 2018
1 parent a3ca37e commit 157eca0
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 @@ -1223,4 +1223,19 @@ describe "Semantic: macro" do
Foo.new.foo
)) { int32 }
end

it "clones default value before expanding" do
assert_type(%(
FOO = {} of String => String?
macro foo(x = {} of String => String)
{% FOO["foo"] = x["foo"] %}
{% x["foo"] = "foo" %}
end
foo
foo
{{ FOO["foo"] }}
)) { nil_type }
end
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module Crystal
next if vars.has_key?(macro_arg.name)

default_value = default_value.expand_node(call.location, call.end_location) if default_value.is_a?(MagicConstant)
vars[macro_arg.name] = default_value
vars[macro_arg.name] = default_value.clone
end

# The named arguments
Expand Down

0 comments on commit 157eca0

Please sign in to comment.