-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refine parameter adaptation logic for arrays #23591
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
+101
−9
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
342a28c
Refine parameter adaptation logic for arrays
bracevac f2f9445
Fix #23179 for Scala.js and add regression tests
bracevac 05c812f
Move Scala.js array adaptation fix from tpd.AnonClass to ExpandSAMs
bracevac 2bfb5c1
Address review comments
bracevac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Test that reverse cases of #23179 are properly rejected by the type checker. | ||
| // The original issue was about SAM expecting Array[T] but impl having Array[? >: T]. | ||
| // Due to array invariance, the reverse cases cannot occur. | ||
|
|
||
| object TestReverse { | ||
| trait A { def f(a: Array[? <: AnyRef]): Any } | ||
| def g(a: A) = a.f(Array.empty[AnyRef]) | ||
|
|
||
| def test(): Unit = { | ||
| g((x: Array[AnyRef]) => x.headOption) // error: Array[AnyRef] is not a subtype of Array[? <: AnyRef] | ||
| } | ||
| } | ||
|
|
||
| object TestResult { | ||
| trait A { def f(a: Int): Array[AnyRef] } | ||
| def g(a: A) = a.f(1) | ||
|
|
||
| def test(): Unit = { | ||
| val arr: Array[? >: AnyRef] = Array("hello") | ||
| g((x: Int) => arr) // error: Array[? >: AnyRef] is not a subtype of Array[AnyRef] | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| object Test { | ||
| // Basic case: SAM expects Array[AnyRef], impl has Array[? >: AnyRef] | ||
| trait A { def f(a: Array[AnyRef]): Any } | ||
| def g(a: A) = a.f(Array.empty[AnyRef]) | ||
|
|
||
| // Multiple array parameters | ||
| trait MultiParam { def f(a: Array[AnyRef], b: Array[AnyRef]): Any } | ||
| def gMulti(a: MultiParam) = a.f(Array.empty[AnyRef], Array.empty[AnyRef]) | ||
|
|
||
| // Mixed parameters: only some need adaptation | ||
| trait MixedParam { def f(a: Array[AnyRef], b: Int): Any } | ||
| def gMixed(a: MixedParam) = a.f(Array.empty[AnyRef], 42) | ||
|
|
||
| // Generic return type | ||
| trait GenericReturn[T] { def f(a: Array[AnyRef]): T } | ||
| def gGeneric[T](a: GenericReturn[T]): T = a.f(Array.empty[AnyRef]) | ||
|
|
||
| def main(args: Array[String]): Unit = { | ||
| // Basic case | ||
| g((x: Array[? >: AnyRef]) => x.headOption) | ||
|
|
||
| // Multiple array parameters - both need adaptation | ||
| gMulti((x: Array[? >: AnyRef], y: Array[? >: AnyRef]) => (x.headOption, y.headOption)) | ||
|
|
||
| // Mixed - only first param needs adaptation | ||
| gMixed((x: Array[? >: AnyRef], n: Int) => (x.headOption, n)) | ||
|
|
||
| // Generic return type | ||
| val result: Option[?] = gGeneric((x: Array[? >: AnyRef]) => x.headOption) | ||
| } | ||
| } |
Oops, something went wrong.
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.
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.
What if it's hidden behind a
PolyType? Or in a second term param list?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.
Do you have a concrete example in mind? Those cases do not qualify as SAMs afaik.
I've added more run tests that involve multiple parameters and a generic return type on the trait.
For generics on the method, the compiler rejects both of these:
Same goes for curried argument lists:
Scala 2 rejects these as well (modulo the polymorphic lambda syntax of course).
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.
Right. I missed that. Seems all good.