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

[Backport] Overloading resolution: Handle SAM types more like Java and Scala 2 #12131

Merged
merged 3 commits into from
Apr 17, 2021

Conversation

smarter
Copy link
Member

@smarter smarter commented Apr 17, 2021

This backports #12097.

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.
@smarter smarter added this to the 3.0.0-RC3 milestone Apr 17, 2021
@smarter smarter requested a review from odersky April 17, 2021 17:35
@odersky odersky removed their assignment Apr 17, 2021
@smarter smarter merged commit aed8a87 into scala:release-3.0.0 Apr 17, 2021
@smarter smarter deleted the backport-fix-11938-new branch April 17, 2021 18:37
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.

None yet

2 participants