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

Clone macro default argument before macro expansion #5247

Conversation

makenowjust
Copy link
Contributor

Currently macro default argument is not cloned on expanding, so it causes unexpected behavior when mutating default argument. For example:

macro foo(foo = [] of String)
  {% foo.push "foo" %}
  p {{foo}}
end

foo
foo

I expected the below because method's default value is evaluated on every call:

["foo"]
["foo"]

However, crystal run outputs this:

["foo"]
["foo", "foo"]

This PR fixes this behavior.

@Sija
Copy link
Contributor

Sija commented Nov 5, 2017

Will it keep values which shouldn't be cloned intact, like in the example below?

class Foo
  FOO = [] of String

  # here, `baz` argument shouldn't be `#clone`d.
  macro bar(baz = FOO)
    {% baz.resolve.push "foo" %}
  end
end

Foo.bar
Foo.bar

# CORRECT
pp Foo::FOO # => ["foo", "foo"]

# INCORRECT
pp Foo::FOO # => ["foo"]

@makenowjust
Copy link
Contributor Author

ping.

@asterite asterite requested a review from RX14 January 10, 2018 13:59
Copy link
Contributor

@RX14 RX14 left a comment

Choose a reason for hiding this comment

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

Given @Sija's code sample works after this PR.

@asterite
Copy link
Member

Yes, I tried that code and it works fine.

@asterite asterite merged commit 157eca0 into crystal-lang:master Jan 10, 2018
@asterite asterite added this to the Next milestone Jan 10, 2018
@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Jan 10, 2018
@makenowjust makenowjust deleted the fix/crystal/macro-default-clone-before-expanding branch January 10, 2018 14:44
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants