K2: Imitate K1 behavior for case of captured types with a raw supertype

See the comments in the code, but mostly the motivation is that once
it was decided to stick with such a legacy thing as raw types,
we are ok with some corner-cases hacks for them
(if there are not too many of them) and they don't break anything
when there are no raw types in the code.

^KT-56616 Fixed
This commit is contained in:
Denis.Zharkov
2023-02-22 13:45:53 +01:00
committed by Space Team
parent eb09a25239
commit 608cb01935
14 changed files with 172 additions and 44 deletions
@@ -0,0 +1,25 @@
// ISSUE: KT-56616
// FILE: StubElement.java
public interface StubElement<T> {
<E> E bar(E v);
}
// FILE: StubBasedPsiElement.java
public interface StubBasedPsiElement<T extends StubElement> {
T foo1();
StubElement foo2();
}
// FILE: test.kt
fun StubBasedPsiElement<*>.foo(): String? {
if ("".hashCode() == 0) {
return foo1().bar("")
}
return <!RETURN_TYPE_MISMATCH!>foo2().bar("")<!>
}
@@ -0,0 +1,25 @@
// ISSUE: KT-56616
// FILE: StubElement.java
public interface StubElement<T> {
<E> E bar(E v);
}
// FILE: StubBasedPsiElement.java
public interface StubBasedPsiElement<T extends StubElement> {
T foo1();
StubElement foo2();
}
// FILE: test.kt
fun StubBasedPsiElement<*>.foo(): String? {
if ("".hashCode() == 0) {
return foo1().bar("")
}
return <!TYPE_MISMATCH!>foo2().<!TYPE_MISMATCH!>bar("")<!><!>
}
@@ -0,0 +1,37 @@
// FIR_IDENTICAL
// WITH_STDLIB
// ISSUE: KT-56616
// FILE: StubElement.java
import org.jetbrains.annotations.NotNull;
public interface StubElement<T extends PsiElement> {
<E extends PsiElement> E @NotNull [] getChildrenByType(@NotNull String filter, final E[] array);
}
// FILE: PsiElement.java
public interface PsiElement
// FILE: StubBasedPsiElement.java
public interface StubBasedPsiElement<Stub extends StubElement> extends PsiElement {
Stub getStub();
}
// FILE: test.kt
private val STRING_TEMPLATE_EMPTY_ARRAY = emptyArray<KtStringTemplateExpression>()
open class KtStringTemplateExpression : PsiElement
fun StubBasedPsiElement<*>.foo(): KtStringTemplateExpression? {
stub?.let {
// K1: Array<KtStringTemplateExpression>, was K2: Array<PsiElement>
val expressions = it.getChildrenByType("", STRING_TEMPLATE_EMPTY_ARRAY)
// Ok in K1, Should not be error in K2
return expressions.firstOrNull()
}
return null
}