diff --git a/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.kt b/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.kt index 73c623589fa..f65f93e37a8 100644 --- a/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.kt +++ b/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.kt @@ -11,8 +11,8 @@ public class A { fun main(a: A, ml: MutableList, l: List) { a.foo(ml) - a.foo(l) + a.foo(l) - a.bar(ml) - a.bar(l) + a.bar(ml) + a.bar(l) } diff --git a/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.txt b/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.txt index f56957ddef4..ea86c73180a 100644 --- a/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.txt +++ b/compiler/testData/diagnostics/tests/platformTypes/genericVarianceViolation/strangeVariance.txt @@ -4,9 +4,9 @@ public fun main(/*0*/ a: A, /*1*/ ml: kotlin.collections.MutableList!): kotlin.Unit + public/*package*/ open fun bar(/*0*/ x: kotlin.collections.MutableList!): kotlin.Unit public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public/*package*/ open fun foo(/*0*/ x: kotlin.collections.(Mutable)List!): kotlin.Unit + public/*package*/ open fun foo(/*0*/ x: kotlin.collections.MutableList!): kotlin.Unit public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.java b/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.java index 3c256ec7d8b..8fde9712862 100644 --- a/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.java +++ b/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.java @@ -6,4 +6,8 @@ public class MethodWithMappedClasses { public void copy(List dest, List src) { throw new UnsupportedOperationException(); } + + public void copyMap(Map dest, Map src) { + throw new UnsupportedOperationException(); + } } diff --git a/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.txt b/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.txt index 77417965b40..99dce51a025 100644 --- a/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.txt +++ b/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithMappedClasses.txt @@ -2,5 +2,6 @@ package test public open class MethodWithMappedClasses { public constructor MethodWithMappedClasses() - public open fun copy(/*0*/ p0: kotlin.collections.(Mutable)List!, /*1*/ p1: kotlin.collections.(Mutable)List!): kotlin.Unit + public open fun copy(/*0*/ p0: kotlin.collections.MutableList!, /*1*/ p1: kotlin.collections.(Mutable)List!): kotlin.Unit + public open fun copyMap(/*0*/ p0: kotlin.collections.MutableMap!, /*1*/ p1: kotlin.collections.(Mutable)Map!): kotlin.Unit } diff --git a/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithTypeParameters.txt b/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithTypeParameters.txt index f2e783c9acf..9b1237f49af 100644 --- a/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithTypeParameters.txt +++ b/compiler/testData/loadJava/compiledJava/kotlinSignature/MethodWithTypeParameters.txt @@ -2,5 +2,5 @@ package test public open class MethodWithTypeParameters { public constructor MethodWithTypeParameters() - public open fun foo(/*0*/ p0: A!, /*1*/ p1: (kotlin.collections.MutableList..kotlin.collections.List?), /*2*/ p2: kotlin.collections.(Mutable)List!): kotlin.Unit where B : kotlin.collections.(Mutable)List! + public open fun foo(/*0*/ p0: A!, /*1*/ p1: (kotlin.collections.MutableList..kotlin.collections.List?), /*2*/ p2: kotlin.collections.MutableList!): kotlin.Unit where B : kotlin.collections.(Mutable)List! } diff --git a/compiler/testData/loadJava/compiledJava/rendering/Rendering.txt b/compiler/testData/loadJava/compiledJava/rendering/Rendering.txt index 560da5ea607..74fbc1933bf 100644 --- a/compiler/testData/loadJava/compiledJava/rendering/Rendering.txt +++ b/compiler/testData/loadJava/compiledJava/rendering/Rendering.txt @@ -48,7 +48,7 @@ public open class Rendering { public/*package*/ interface I_Wildcard { public abstract fun foo1(/*0*/ p0: (kotlin.collections.MutableList..kotlin.collections.List?)): kotlin.Unit - public abstract fun foo2(/*0*/ p0: kotlin.collections.(Mutable)List!): kotlin.Unit + public abstract fun foo2(/*0*/ p0: kotlin.collections.MutableList!): kotlin.Unit public abstract fun foo3(/*0*/ p0: kotlin.collections.(Mutable)List<*>!): kotlin.Unit public abstract fun foo4(/*0*/ p0: test.Rendering.D_SuperG!): kotlin.Unit public abstract fun foo5(/*0*/ p0: test.Rendering.D_SuperG!): kotlin.Unit diff --git a/compiler/testData/loadJava/compiledJava/signaturePropagation/SubclassWithRawType.txt b/compiler/testData/loadJava/compiledJava/signaturePropagation/SubclassWithRawType.txt index 41842bde905..385be01b877 100644 --- a/compiler/testData/loadJava/compiledJava/signaturePropagation/SubclassWithRawType.txt +++ b/compiler/testData/loadJava/compiledJava/signaturePropagation/SubclassWithRawType.txt @@ -6,7 +6,7 @@ public interface SubclassWithRawType { public abstract override /*1*/ fun array1(): kotlin.Array<(out) kotlin.collections.(Mutable)List!>! public abstract override /*1*/ fun array2(): kotlin.Array<(out) kotlin.collections.(Mutable)List<(raw) kotlin.Any?>!>! public abstract override /*1*/ fun boundWildcard1(): (kotlin.collections.MutableList!>..kotlin.collections.List!>?) - public abstract override /*1*/ fun boundWildcard2(): kotlin.collections.(Mutable)List!>! + public abstract override /*1*/ fun boundWildcard2(): kotlin.collections.MutableList!>! public abstract override /*1*/ fun simple1(): kotlin.collections.(Mutable)List! public abstract override /*1*/ fun simple2(): kotlin.collections.(Mutable)List!>! public abstract override /*1*/ fun simple3(): kotlin.collections.(Mutable)List<(raw) kotlin.Any?>! @@ -17,7 +17,7 @@ public interface SubclassWithRawType { public abstract fun array1(): kotlin.Array<(out) kotlin.collections.(Mutable)List<(raw) kotlin.Any?>!>! public abstract fun array2(): kotlin.Array<(out) kotlin.collections.(Mutable)List!>! public abstract fun boundWildcard1(): (kotlin.collections.MutableList!>..kotlin.collections.List!>?) - public abstract fun boundWildcard2(): kotlin.collections.(Mutable)List!>! + public abstract fun boundWildcard2(): kotlin.collections.MutableList!>! public abstract fun simple1(): kotlin.collections.(Mutable)List<(raw) kotlin.Any?>! public abstract fun simple2(): kotlin.collections.(Mutable)List<(raw) kotlin.Any?>! public abstract fun simple3(): kotlin.collections.(Mutable)List! diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt index 67505655457..8839ec9b55e 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt @@ -146,8 +146,12 @@ class LazyJavaTypeResolver( val kotlinDescriptor = javaToKotlin.mapJavaToKotlin(fqName) ?: return null - if (howThisTypeIsUsedEffectively == MEMBER_SIGNATURE_COVARIANT || howThisTypeIsUsedEffectively == SUPERTYPE) { - if (javaToKotlin.isReadOnly(kotlinDescriptor)) { + if (javaToKotlin.isReadOnly(kotlinDescriptor)) { + if (howThisTypeIsUsedEffectively == MEMBER_SIGNATURE_COVARIANT + || howThisTypeIsUsedEffectively == SUPERTYPE + // Convert (Mutable)List to MutableList + // Same for (Mutable)Map + || javaType.typeArguments.lastOrNull().isSuperWildcard()) { return javaToKotlin.convertReadOnlyToMutable(kotlinDescriptor) } } @@ -155,6 +159,8 @@ class LazyJavaTypeResolver( return kotlinDescriptor } + private fun JavaType?.isSuperWildcard(): Boolean = (this as? JavaWildcardType)?.let { it.bound != null && !it.isExtends } ?: false + private fun isRaw(): Boolean { if (javaType.isRaw) return true