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

[CS2] Fix handling of parameters that are function calls #4427

Conversation

GeoffreyBooth
Copy link
Collaborator

Fixes #4406. Also revises output of generator functions to be function* rather than function *, to follow accepted idiom.

@connec, any notes?

@GeoffreyBooth GeoffreyBooth added this to the 2.0.0 milestone Jan 21, 2017
@GeoffreyBooth GeoffreyBooth requested a review from lydell January 21, 2017 06:53
Copy link
Collaborator

@lydell lydell left a comment

Choose a reason for hiding this comment

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

I have a little question, but I don't think it should hold up a merge.

@@ -1945,7 +1945,7 @@ exports.Code = class Code extends Base
# encountered, add these other parameters to the list to be output in
# the function definition.
else
if param.isComplex()
if param.isComplex() or param.isCall()
Copy link
Collaborator

Choose a reason for hiding this comment

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

So a call does not count as "complex"? What happens if we change .isComplex() to also check for calls instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just couldn’t remember what isComplex meant 😄 But I did remember that we documented it here, which says:

If node.isComplex() is false, it is safe to use node more than once. Otherwise you need to store the value of node in a variable and output that variable several times instead. Kind of like this: 5 is not complex. returnFive() is complex. It’s all about “do we need to cache this expression?”

So there it is—returnFive() is exactly our use case. Thanks for the suggestion!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

@GeoffreyBooth GeoffreyBooth changed the title Fix handling of parameters that are function calls [CS2] Fix handling of parameters that are function calls Jan 21, 2017
@@ -2110,7 +2112,7 @@ exports.Param = class Param extends Base
@reference = node

isComplex: ->
@name.isComplex()
@name.isComplex() or (@value? and @value instanceof Call)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bit of a nit-pick but the check for @value? is redundant (@value instanceof Call doesn't depend on the existence of @value).

@connec
Copy link
Collaborator

connec commented Jan 21, 2017

Neat fix 👍 made one small change request 😄

@GeoffreyBooth
Copy link
Collaborator Author

Thanks @connec, I simplified it. I guess this is ready for merge?

@lydell, how would you feel about renaming isComplex to needsCaching?

@lydell
Copy link
Collaborator

lydell commented Jan 21, 2017

@lydell, how would you feel about renaming isComplex to needsCaching?

That's a really good idea! However, I'm sure there's one or two places where .isComplex() is used to check for something else – perhaps wrongly. If you feel like it, go through each of the 40 occurrences of isComplex in nodes.coffee and see if it makes sense.

@GeoffreyBooth GeoffreyBooth merged commit 69a07df into jashkenas:2 Jan 22, 2017
@GeoffreyBooth GeoffreyBooth deleted the destructured-parameter-evaluation-order branch January 22, 2017 12:40
@@ -2110,7 +2112,7 @@ exports.Param = class Param extends Base
@reference = node

isComplex: ->
@name.isComplex()
@name.isComplex() or @value instanceof Call
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ugh, pity I didn't think of this sooner: this should probably just be @name.isComplex() or @value.isComplex(), the following is still broken:

i = 0
({ a = ++i }, b = ++i) ->

It generates:

// Generated by CoffeeScript 2.0.0-alpha
var i;

i = 0;

(function(arg, b = ++i) { // 'b' will be set first (b === 1)
  var a, ref;
  a = (ref = arg.a) != null ? ref : ++i; // 'a' will be set second (a === 2)
});

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.

3 participants