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

Compiler: ensure parentheses inside assign value #5571

Closed
wants to merge 1 commit into from
Closed

Compiler: ensure parentheses inside assign value #5571

wants to merge 1 commit into from

Conversation

asterite
Copy link
Member

Fixes #5568

As the issue mentions, this code:

macro expand
  {{ yield }}
  {{ debug }}
end

def foo
  {:foo => 1}
end

expand do
  x = foo[:foo] += 1
  puts x
end

will print this from the debug part of the macro:

begin
  x = __temp_2 = foo
  __temp_2[:foo] = __temp_2[:foo] + 1

  puts(x)
end

The issue is that x = ... is incorrectly printed with the to_s method of an ASTNode because it's either missing parentheses or begin/end so that its value is the last expression (not the first one here). So this PR fixes that.

Eventually, I think it might be better to make this expansion happen after macro code, so showing that macro code would show the original foo[:foo] += 1. In any case, this fix is more general because fixing the to_s should be done regardless of this other possible enhancement.

@asterite asterite self-assigned this Jan 11, 2018
@asterite
Copy link
Member Author

Since the initial commit broke a few specs, I fixed them but also made the ASTNode#to_scode generate a bit more prettier/concise strings.

@asterite asterite closed this May 28, 2018
@RX14 RX14 reopened this May 28, 2018
@RX14 RX14 closed this May 28, 2018
@RX14
Copy link
Contributor

RX14 commented May 28, 2018

This looks fine to merge as-is if someone can fix the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid macro expansion for x = hash[:key] += 1 inside {{ yield }}
2 participants