-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Overloading resolution: Handle SAM types more like Java and Scala 2 #12097
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Previously we caught this in Typer, but doing it in the `SAMType` extractor is more robust because it is used in overloading resolution, and this will be needed in a subsequent commit of this PR to avoid selecting the wrong overload in some cases. This required dropping the final flag from the compiler-generated ContextFunctionN, instead we explicitly prevent subclasses in RefChecks.
The two pass approach was originally introduced in 295c981 to lower the priority of implicit conversions, but just a month later in 6253125 a similar two pass approach was used to call resolveOverloaded which itself calls narrowByTrees, rendering the first commit moot. Therefore, we can just remove this first pass and all associated code without affecting anything.
`resolveOverloaded` is called twice, first with implicit conversions off, then on. Before this commit, turning off implicit conversions also turned off SAM conversions, this behavior does not match Java or Scala 2 which means we could end up picking a different overload than they do (cf scala#11938). This commit enables SAM conversions in the first pass, _except_ for conversions to PartialFunction as that would break a lot of existing code and wouldn't match how either Java or Scala 2 pick overloads. This is an alternative to scala#11945 (which special-cased SAM types with an explicit `@FunctionalInterfaces` annotation) and scala#11990 (which special-cased SAM types that subtype a scala Function type). Special-casing PartialFunction instead seems more defensible since it's already special-cased in Scala 2 and is not considered a SAM type by Java. Fixes scala#11938.
cfafc83
to
6cc5b49
Compare
odersky
requested changes
Apr 17, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First three commits are good for backporting.
Let's split off the last commit and make that a separate PR.
I am not 100% sure how precise the closure detection in the last commit is. So let's avoid any risk and do exactly like Scala 2 for the RC.
OK, sounds good to me. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
resolveOverloaded
is called twice, first with implicit conversionsoff, then on. Before this commit, turning off implicit conversions also
turned off SAM conversions, this behavior does not match Java or Scala 2
which means we could end up picking a different overload than they
do (cf #11938).
This commit enables SAM conversions in the first pass, except for
conversions to PartialFunction as that would break a lot of existing
code and wouldn't match how either Java or Scala 2 pick overloads.
This is an alternative to #11945 (which special-cased
SAM types with an explicit
@FunctionalInterfaces
annotation) and #11990(which special-cased SAM types that subtype a scala Function type).
Special-casing PartialFunction instead seems more defensible since it's
already special-cased in Scala 2 and is not considered a SAM type by
Java.
Fixes #11938.