Skip to content

Fix #8290: Make Expr.betaReduce give up when it sees a non-function typed closure #8293

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

Closed
wants to merge 1 commit into from

Conversation

fhackett
Copy link
Contributor

Review by @nicolasstucki

This PR addresses 2 issues with existing betaReduce behaviour:

  • when given a non-function typed closure the previous iteration could easily fail to resolve the correct apply method, or even successfully inline the wrong code (see added test cases)

  • if betaReduce did not successfully inline, it would return a transformed tree. This was fine until the above change made it possible to give up while inside a closureDef, which could insert a type ascription inside the closureDef's block, leading to betaReduce returning invalid trees (the closureDef block can only contain a DefDef and Closure, no type ascriptions).

    Fixing this issue would add meaningless complexity, so instead this commit changes betaReduce to cleanly give up by returning the function tree unchanged, only generating the code necessary to call it.

Note: this change affects a few tests that were checking for betaReduce's slight changes to the function tree.

Testing the correctness of this change is done by adding cases to existing tests for betaReduce's treatment of type ascriptions.

…ion typed closure expression

This commit addresses 2 issues with existing betaReduce behaviour:
- when given a non-function typed closure the previous iteration could easily fail to resolve the correct apply method,
  or even successfully inline the wrong code (see added test cases)
- if betaReduce did not successfully inline, it would return a transformed tree. This was fine until the above change made
  it possible to give up while inside a closureDef, which could insert a type ascription inside the closureDef's block,
  leading to betaReduce returning invalid trees (the closureDef block can only contain a DefDef and Closure, no
  type ascriptions). Fixing this issue would add meaningless complexity, so instead this commit changes betaReduce to
  cleanly give up by returning the function tree unchanged, only generating the code necessary to call it.

Note: this change affects a few tests that were checking for betaReduce's slight changes to the function tree.

Testing the correctness of this change is done by adding cases to existing tests for betaReduce's treatment of type
ascriptions.
Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Commit Messages

We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).

Please stick to these guidelines for commit messages:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Add" instead of "Added")
  7. Wrap the body at 80 characters
  8. Use the body to explain what and why vs. how

adapted from https://chris.beams.io/posts/git-commit

Have an awesome day! ☀️

@nicolasstucki nicolasstucki self-assigned this Feb 12, 2020
@fhackett fhackett closed this Feb 20, 2020
@fhackett fhackett deleted the fhackett-fix-8290 branch February 20, 2020 01:11
@fhackett fhackett restored the fhackett-fix-8290 branch February 20, 2020 01:12
@fhackett
Copy link
Contributor Author

Sorry, I have no idea why git decides that when I push an unrelated thing it should just delete a branch that I pushed before.

@fhackett fhackett reopened this Feb 20, 2020
i = i.+(1)
}
while ({
val x$2: scala.Int = i
f.apply(x$2)
f.apply(i)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change looks fishy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How so? I figured it would make sense for betaReduce to either behave like f.appliedToArgs(...) or a full inlining. (with this case being appliedToArgs)

If the other way is preferred, can you explain why so I can know for future PRs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(the other way being "pull all the args out into vals or defs, then either inline or appliedToArgs")

@nicolasstucki
Copy link
Contributor

I'm working on some major refactoring for this logic in #8457. I also adapted the contents of this PR. The fix looks good.

@nicolasstucki
Copy link
Contributor

@fhackett for your original use case might be better suited for the Lambda extractor from #8457.

@nicolasstucki
Copy link
Contributor

Thank you for the fix

@nicolasstucki
Copy link
Contributor

Moved to #8457

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.

Expr.betaReduce does not correctly handle inlining function literals type-ascribed to subclasses of Function
3 participants