Fix loading Java type arguments
Type arguments with use variance in java contradicting to Kotlin declaration-site variance should be loaded as star-projections #KT-11492 Fixed
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
// !CHECK_TYPE
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
// FILE: A.java
|
||||
|
||||
public class A {
|
||||
public static Out<? super CharSequence> foo() { return null; }
|
||||
public static In<? extends CharSequence> bar() { return null; }
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
|
||||
class Out<out E> {
|
||||
fun x(): E = null!!
|
||||
}
|
||||
|
||||
class In<in F> {
|
||||
fun y(f: F) {}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
A.foo().x() checkType { _<Any?>() }
|
||||
A.bar().<!MEMBER_PROJECTED_OUT!>y<!>(null)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package
|
||||
|
||||
public fun test(): kotlin.Unit
|
||||
|
||||
public open class A {
|
||||
public constructor A()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
// Static members
|
||||
public open fun bar(): In<*>!
|
||||
public open fun foo(): Out<*>!
|
||||
}
|
||||
|
||||
public final class In</*0*/ in F> {
|
||||
public constructor In</*0*/ in F>()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
public final fun y(/*0*/ f: F): kotlin.Unit
|
||||
}
|
||||
|
||||
public final class Out</*0*/ out E> {
|
||||
public constructor Out</*0*/ out E>()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
public final fun x(): E
|
||||
}
|
||||
@@ -10323,6 +10323,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("wrongVarianceInJava.kt")
|
||||
public void testWrongVarianceInJava() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/wrongVarianceInJava.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/j+k/brokenCode")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
+8
-2
@@ -224,12 +224,13 @@ class LazyJavaTypeResolver(
|
||||
return when (javaType) {
|
||||
is JavaWildcardType -> {
|
||||
val bound = javaType.bound
|
||||
if (bound == null)
|
||||
val projectionKind = if (javaType.isExtends) OUT_VARIANCE else IN_VARIANCE
|
||||
if (bound == null || projectionKind.isConflictingArgumentFor(typeParameter))
|
||||
makeStarProjection(typeParameter, attr)
|
||||
else {
|
||||
createProjection(
|
||||
type = transformJavaType(bound, UPPER_BOUND.toAttributes()),
|
||||
projectionKind = if (javaType.isExtends) OUT_VARIANCE else IN_VARIANCE,
|
||||
projectionKind = projectionKind,
|
||||
typeParameterDescriptor = typeParameter
|
||||
)
|
||||
}
|
||||
@@ -238,6 +239,11 @@ class LazyJavaTypeResolver(
|
||||
}
|
||||
}
|
||||
|
||||
private fun Variance.isConflictingArgumentFor(typeParameter: TypeParameterDescriptor): Boolean {
|
||||
if (typeParameter.variance == INVARIANT) return false
|
||||
return this != typeParameter.variance
|
||||
}
|
||||
|
||||
override fun getCapabilities(): TypeCapabilities = if (isRaw()) RawTypeCapabilities else TypeCapabilities.NONE
|
||||
|
||||
private val nullable = c.storageManager.createLazyValue l@ {
|
||||
|
||||
Reference in New Issue
Block a user