diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index e4cccf56a61..311439c4160 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -3153,6 +3153,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); } + @TestMetadata("dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt") + public void testDontCreateInconsistentTypeDuringStarProjectionSubstitution() throws Exception { + runTest("compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt"); + } + @TestMetadata("intersectionTypeMultipleBounds.kt") public void testIntersectionTypeMultipleBounds() throws Exception { runTest("compiler/testData/codegen/box/casts/intersectionTypeMultipleBounds.kt"); diff --git a/compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt b/compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt new file mode 100644 index 00000000000..09a5934ae03 --- /dev/null +++ b/compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt @@ -0,0 +1,15 @@ +interface I + +interface Foo + +interface Bar { + fun test(x: Foo<*, T>) +} + +fun foo(x: Any) { + if (x is Bar<*>) { + x::test + } +} + +fun box(): String = "OK" diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index b8c2c493d7a..a6cd64574d1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -3173,6 +3173,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); } + @TestMetadata("dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt") + public void testDontCreateInconsistentTypeDuringStarProjectionSubstitution() throws Exception { + runTest("compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt"); + } + @TestMetadata("intersectionTypeMultipleBounds.kt") public void testIntersectionTypeMultipleBounds() throws Exception { runTest("compiler/testData/codegen/box/casts/intersectionTypeMultipleBounds.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 4e1cb40831b..c84dd5a67f6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -3173,6 +3173,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); } + @TestMetadata("dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt") + public void testDontCreateInconsistentTypeDuringStarProjectionSubstitution() throws Exception { + runTest("compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt"); + } + @TestMetadata("intersectionTypeMultipleBounds.kt") public void testIntersectionTypeMultipleBounds() throws Exception { runTest("compiler/testData/codegen/box/casts/intersectionTypeMultipleBounds.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index f484d9aa4c1..ef8272eb6a5 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -3153,6 +3153,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); } + @TestMetadata("dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt") + public void testDontCreateInconsistentTypeDuringStarProjectionSubstitution() throws Exception { + runTest("compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt"); + } + @TestMetadata("intersectionTypeMultipleBounds.kt") public void testIntersectionTypeMultipleBounds() throws Exception { runTest("compiler/testData/codegen/box/casts/intersectionTypeMultipleBounds.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt b/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt index 8498af270b8..78112a8857d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt @@ -55,7 +55,7 @@ private fun TypeArgument.toTypeProjection(): TypeProjection { } fun removeProjectionIfRedundant(variance: Variance) = if (variance == typeParameter.variance) Variance.INVARIANT else variance return when { - inProjection == outProjection -> TypeProjectionImpl(inProjection) + inProjection == outProjection || typeParameter.variance == Variance.IN_VARIANCE -> TypeProjectionImpl(inProjection) KotlinBuiltIns.isNothing(inProjection) && typeParameter.variance != Variance.IN_VARIANCE -> TypeProjectionImpl(removeProjectionIfRedundant(Variance.OUT_VARIANCE), outProjection) KotlinBuiltIns.isNullableAny(outProjection) -> TypeProjectionImpl(removeProjectionIfRedundant(Variance.IN_VARIANCE), inProjection) diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 6188fc4b272..323ab945eec 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -2498,6 +2498,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); } + @TestMetadata("dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt") + public void testDontCreateInconsistentTypeDuringStarProjectionSubstitution() throws Exception { + runTest("compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt"); + } + @TestMetadata("intersectionTypeMultipleBounds.kt") public void testIntersectionTypeMultipleBounds() throws Exception { runTest("compiler/testData/codegen/box/casts/intersectionTypeMultipleBounds.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 469147a0473..72451570757 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -2498,6 +2498,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); } + @TestMetadata("dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt") + public void testDontCreateInconsistentTypeDuringStarProjectionSubstitution() throws Exception { + runTest("compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt"); + } + @TestMetadata("intersectionTypeMultipleBounds.kt") public void testIntersectionTypeMultipleBounds() throws Exception { runTest("compiler/testData/codegen/box/casts/intersectionTypeMultipleBounds.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 000d0c4d7fe..268e4b2846c 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -2498,6 +2498,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); } + @TestMetadata("dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt") + public void testDontCreateInconsistentTypeDuringStarProjectionSubstitution() throws Exception { + runTest("compiler/testData/codegen/box/casts/dontCreateInconsistentTypeDuringStarProjectionSubstitution.kt"); + } + @TestMetadata("intersectionTypeMultipleBounds.kt") public void testIntersectionTypeMultipleBounds() throws Exception { runTest("compiler/testData/codegen/box/casts/intersectionTypeMultipleBounds.kt");