Fix override checking for Java methods with covariant array #24408
+48
−3
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.
Fixes #24074
When resolving overridden methods from Java interfaces, treat arrays as covariant.
This fixes incorrect method selection when multiple Java interfaces override methods with array return types.
Previously, in the example below, we get the compilation error:
because
Denotations#mergeSingleDenotcreates aJointRefDenotationforLvl1.getData: A[]andLvl2.getData: B[], with a return type ofJArray[A] & JArray[B](since the compiler doesn't recognize that
Lvl2.getDataoverridesLvl1.getData).And because
JArrayisn't recognized as covariant,JArray[A & B] <: JArray[A] & JArray[B]cannot be derived.Consequently,
lvl3.getData.headreturns a value typed asAinstead of neitherBnorA & B, which fails to resolve the methodonlyInB.It seems in Scala2, we use
Types#matches(which does match in Scala3 as well), so Scala3 is more strict?https://github.com/scala/scala/blob/ae6ae4dd59cb90af62093cde52a292e5bd8bb7a8/src/reflect/scala/reflect/internal/Symbols.scala#L2461
https://github.com/scala/scala/blob/ae6ae4dd59cb90af62093cde52a292e5bd8bb7a8/src/reflect/scala/reflect/internal/Symbols.scala#L2445-L2452
https://github.com/scala/scala/blob/ae6ae4dd59cb90af62093cde52a292e5bd8bb7a8/src/reflect/scala/reflect/internal/Types.scala#L862-L874
Previous try #24377