diff --git a/compiler/testData/codegen/box/inlineClasses/kt26103.kt b/compiler/testData/codegen/box/inlineClasses/kt26103.kt new file mode 100644 index 00000000000..20b515f5a52 --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/kt26103.kt @@ -0,0 +1,45 @@ +// !LANGUAGE: +InlineClasses +// WITH_RUNTIME +// IGNORE_BACKEND: JVM_IR, JS_IR + +inline class Bar(val y: Int) + +inline class Foo(val x: Int) +inline class Foo2(val x: Foo) +inline class Foo3(val x: Bar) + +fun testValueParameter(z: Foo) = z.x +fun testValueParameter2(z: Foo2) = z.x.x +fun testValueParameter3(z: Foo3) = z.x.y + +fun testReturnType(x: Int) = Foo(x) +fun testReturnType2(x: Int) = Foo2(Foo(x)) +fun testReturnType3(x: Int) = Foo3(Bar(x)) + +fun testGenericTypeArgumentInValueParameter(zs: List>) = zs[0].x +fun testGenericTypeArgumentInValueParameter2(zs: List>) = zs[0].x.x +fun testGenericTypeArgumentInValueParameter3(zs: List>) = zs[0].x.y + +fun testGenericTypeArgumentInReturnType(x: Int) = listOf(Foo(x)) +fun testGenericTypeArgumentInReturnType2(x: Int) = listOf(Foo2(Foo(x))) +fun testGenericTypeArgumentInReturnType3(x: Int) = listOf(Foo3(Bar(x))) + +fun box(): String { + if (testValueParameter(Foo(42)) != 42) throw AssertionError() + if (testValueParameter2(Foo2(Foo(42))) != 42) throw AssertionError() + if (testValueParameter3(Foo3(Bar(42))) != 42) throw AssertionError() + + if (testReturnType(42).x != 42) throw AssertionError() + if (testReturnType2(42).x.x != 42) throw AssertionError() + if (testReturnType3(42).x.y != 42) throw AssertionError() + + if (testGenericTypeArgumentInValueParameter(listOf(Foo(42))) != 42) throw AssertionError() + if (testGenericTypeArgumentInValueParameter2(listOf(Foo2(Foo(42)))) != 42) throw AssertionError() + if (testGenericTypeArgumentInValueParameter3(listOf(Foo3(Bar(42)))) != 42) throw AssertionError() + + if (testGenericTypeArgumentInReturnType(42)[0].x != 42) throw AssertionError() + if (testGenericTypeArgumentInReturnType2(42)[0].x.x != 42) throw AssertionError() + if (testGenericTypeArgumentInReturnType3(42)[0].x.y != 42) throw AssertionError() + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/inlineClasses/kt26103_original.kt b/compiler/testData/codegen/box/inlineClasses/kt26103_original.kt new file mode 100644 index 00000000000..22c0f5efc6d --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/kt26103_original.kt @@ -0,0 +1,13 @@ +// !LANGUAGE: +InlineClasses +// WITH_RUNTIME +// IGNORE_BACKEND: JVM_IR + +inline class Foo(val x: Int) + +class Bar(val y: Foo) + +fun box(): String { + if (Bar(Foo(42)).y.x != 42) throw AssertionError() + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 8e0e7ab0dec..bd63770b1e8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -11548,6 +11548,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt"); } + @TestMetadata("kt26103.kt") + public void testKt26103() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt"); + } + + @TestMetadata("kt26103_original.kt") + public void testKt26103_original() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt"); + } + @TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt") public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 33a7be294f8..abec48634f4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -11548,6 +11548,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt"); } + @TestMetadata("kt26103.kt") + public void testKt26103() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt"); + } + + @TestMetadata("kt26103_original.kt") + public void testKt26103_original() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt"); + } + @TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt") public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 3690d42e33d..733e8c865f8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -11548,6 +11548,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt"); } + @TestMetadata("kt26103.kt") + public void testKt26103() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt"); + } + + @TestMetadata("kt26103_original.kt") + public void testKt26103_original() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt"); + } + @TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt") public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt"); diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt index d2b80c61040..cd87a1f6a17 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt @@ -103,6 +103,13 @@ class TypeMappingMode private constructor( ): TypeMappingMode { if (type.arguments.isEmpty()) return DEFAULT + if (type.isInlineClassType() && shouldUseUnderlyingType(type)) { + val underlyingType = computeUnderlyingType(type) + if (underlyingType != null) { + return getOptimalModeForSignaturePart(underlyingType, isForAnnotationParameter, canBeUsedInSupertypePosition) + } + } + val contravariantArgumentMode = if (!canBeUsedInSupertypePosition) TypeMappingMode( diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt index b4d40669471..800530acfde 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt @@ -240,7 +240,7 @@ private fun mapBuiltInType( return null } -private fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? { +internal fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? { if (!shouldUseUnderlyingType(inlineClassType)) return null val descriptor = inlineClassType.unsubstitutedUnderlyingType()?.constructor?.declarationDescriptor ?: return null @@ -250,7 +250,7 @@ private fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? { inlineClassType.substitutedUnderlyingType() } -private fun shouldUseUnderlyingType(inlineClassType: KotlinType): Boolean { +internal fun shouldUseUnderlyingType(inlineClassType: KotlinType): Boolean { val underlyingType = inlineClassType.unsubstitutedUnderlyingType() ?: return false return !inlineClassType.isMarkedNullable || diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index 32963b899e2..b8c601f20e0 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -10118,6 +10118,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt"); } + @TestMetadata("kt26103.kt") + public void testKt26103() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt"); + } + + @TestMetadata("kt26103_original.kt") + public void testKt26103_original() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt"); + } + @TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt") public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.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 7158642e175..0617a3bc43f 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 @@ -11173,6 +11173,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/kt25771.kt"); } + @TestMetadata("kt26103.kt") + public void testKt26103() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103.kt"); + } + + @TestMetadata("kt26103_original.kt") + public void testKt26103_original() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt26103_original.kt"); + } + @TestMetadata("noAssertionsOnInlineClassBasedOnNullableType.kt") public void testNoAssertionsOnInlineClassBasedOnNullableType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/noAssertionsOnInlineClassBasedOnNullableType.kt");