Skip to content

Commit 157eca0

Browse files
makenowjustasterite
authored andcommittedJan 10, 2018
Clone macro default argument before macro expansion
1 parent a3ca37e commit 157eca0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
 

Diff for: ‎spec/compiler/semantic/macro_spec.cr

+15
Original file line numberDiff line numberDiff line change
@@ -1223,4 +1223,19 @@ describe "Semantic: macro" do
12231223
Foo.new.foo
12241224
)) { int32 }
12251225
end
1226+
1227+
it "clones default value before expanding" do
1228+
assert_type(%(
1229+
FOO = {} of String => String?
1230+
1231+
macro foo(x = {} of String => String)
1232+
{% FOO["foo"] = x["foo"] %}
1233+
{% x["foo"] = "foo" %}
1234+
end
1235+
1236+
foo
1237+
foo
1238+
{{ FOO["foo"] }}
1239+
)) { nil_type }
1240+
end
12261241
end

Diff for: ‎src/compiler/crystal/macros/interpreter.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module Crystal
5151
next if vars.has_key?(macro_arg.name)
5252

5353
default_value = default_value.expand_node(call.location, call.end_location) if default_value.is_a?(MagicConstant)
54-
vars[macro_arg.name] = default_value
54+
vars[macro_arg.name] = default_value.clone
5555
end
5656

5757
# The named arguments

0 commit comments

Comments
 (0)
Please sign in to comment.