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

fix java array argument variance #1458

Merged
merged 1 commit into from
Jul 18, 2023
Merged

Conversation

neetopia
Copy link
Contributor

fixes #1456

It is a little bit messed up regarding flexible type.

By Java specs, array should have a single variance of covariance for its type arguments. However, from kotlin compiler, the said type is resolved to a flexible type with a lower bound of invariant type argument and an upper bound of covariant type argument. It might make sense from kotlinc since invariant is more restrictive hence works for a lower bound. It is just that when compiler is dealing with KoltinType.arguments, it only looks at lower bound which causes trouble because the lowerbound argument of an array actually discarded the covariant part.

It might look to be better respect Java spec by making it lower bound covariant as well, but attempting to do that caused a lot of failures elsewhere and trying to work against compiler seems a bad idea, following kotlinc's result is easier to implement here.

However it is still reasonable to add a special handling in reference element to reflect the covariant nature of array.

@neetopia neetopia requested a review from ting-yuan July 18, 2023 00:47
@ting-yuan
Copy link
Collaborator

May I know how this fixes #1456, where no array is involved in the example?

return if (type.psi is PsiArrayType) {
resolved
} else {
resolved.replace(type.element.typeArguments)
Copy link
Contributor Author

@neetopia neetopia Jul 18, 2023

Choose a reason for hiding this comment

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

fix for #1456 is at this line. However introducing this line causes issues elsewhere due to array issue mentioned in this PR. As the issue on array turned out to be a bigger issue the PR is named after that.

resolved
} else {
resolved.replace(type.element.typeArguments)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a little bit confused. Why do you special-case Array here?

Also, I'm worried that replacing the type arguments in this way would effectively change the semantics of platform types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replacing argument is because for java types, resolveJavaType can't return the correct argument variance as per illustrated in #1456 , special handling for array is caused from the main issue, where the flexible type returned from compiler is of invariant lowerbound, covariant upperbound, and trying to simply follow practice of replacing argument from reference element will cause issues in other places which is stated in the original description, therefore we keep it as is.

@neetopia neetopia merged commit 9ea19d9 into google:main Jul 18, 2023
3 checks passed
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.

KSTypeArgument missing out variance.
2 participants