diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index f70f14f3ff2..3e2fb2e5769 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -19270,6 +19270,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @Test + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @Test @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt index 3749ad1e401..39173ca61da 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt @@ -481,8 +481,7 @@ internal class LambdaMetafactoryArgumentsBuilder( if (erasedAdapteeClass.isInline) { // Inline classes mapped to non-null reference types are a special case because they can't be boxed trivially. // TODO consider adding a special type annotation to force boxing on an inline class type regardless of its underlying type. - val underlyingAdapteeType = getInlineClassUnderlyingType(erasedAdapteeClass) as? IrSimpleType - ?: throw AssertionError("Underlying type for inline class should be a simple type: ${erasedAdapteeClass.render()}") + val underlyingAdapteeType = getInlineClassUnderlyingType(erasedAdapteeClass) if (!underlyingAdapteeType.hasQuestionMark && !underlyingAdapteeType.isPrimitiveType()) { return TypeAdaptationConstraint.CONFLICT } diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt index 6aab0dddbf2..f130ff41d1c 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt @@ -14,7 +14,6 @@ import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.classifierOrNull import org.jetbrains.kotlin.ir.types.getClass -import org.jetbrains.kotlin.ir.util.getInlineClassUnderlyingType import org.jetbrains.kotlin.ir.util.isInterface import org.jetbrains.kotlin.wasm.ir.* @@ -97,7 +96,7 @@ class WasmTypeTransformer( if (klass != null && klass.hasWasmForeignAnnotation()) { WasmExternRef } else if (ic != null) { - getInlineClassUnderlyingType(ic).toWasmValueType() + context.backendContext.inlineClassesUtils.getInlineClassUnderlyingType(ic).toWasmValueType() } else { this.toWasmGcRefType() } diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/utils/WasmInlineClassesUtils.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/utils/WasmInlineClassesUtils.kt index 714bc9e45b0..8bbef3d48c1 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/utils/WasmInlineClassesUtils.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/utils/WasmInlineClassesUtils.kt @@ -9,10 +9,12 @@ import org.jetbrains.kotlin.backend.wasm.WasmSymbols import org.jetbrains.kotlin.ir.backend.js.InlineClassesUtils import org.jetbrains.kotlin.ir.backend.js.utils.erase import org.jetbrains.kotlin.ir.declarations.IrClass +import org.jetbrains.kotlin.ir.declarations.IrConstructor import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.isNullable +import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable class WasmInlineClassesUtils(private val wasmSymbols: WasmSymbols) : InlineClassesUtils { override fun isTypeInlined(type: IrType): Boolean { @@ -42,4 +44,17 @@ class WasmInlineClassesUtils(private val wasmSymbols: WasmSymbols) : InlineClass override val unboxIntrinsic: IrSimpleFunctionSymbol get() = wasmSymbols.unboxIntrinsic -} \ No newline at end of file + + /** + * Unlike [org.jetbrains.kotlin.ir.util.getInlineClassUnderlyingType], doesn't use [IrClass.inlineClassRepresentation] because + * for some reason it can be called for classes which are not inline, e.g. `kotlin.Double`. + */ + fun getInlineClassUnderlyingType(irClass: IrClass): IrType { + for (declaration in irClass.declarations) { + if (declaration is IrConstructor && declaration.isPrimary) { + return declaration.valueParameters[0].type + } + } + error("Class has no primary constructor: ${irClass.fqNameWhenAvailable}") + } +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/InlineClasses.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/InlineClasses.kt index ecfc42a142d..1435ee5d6b5 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/InlineClasses.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/InlineClasses.kt @@ -6,18 +6,13 @@ package org.jetbrains.kotlin.ir.util import org.jetbrains.kotlin.ir.declarations.IrClass -import org.jetbrains.kotlin.ir.declarations.IrConstructor import org.jetbrains.kotlin.ir.declarations.IrField import org.jetbrains.kotlin.ir.declarations.IrProperty -import org.jetbrains.kotlin.ir.types.IrType +import org.jetbrains.kotlin.ir.types.IrSimpleType -fun getInlineClassUnderlyingType(irClass: IrClass): IrType { - for (declaration in irClass.declarations) { - if (declaration is IrConstructor && declaration.isPrimary) { - return declaration.valueParameters[0].type - } - } - error("Inline class has no primary constructor: ${irClass.fqNameWhenAvailable}") +fun getInlineClassUnderlyingType(irClass: IrClass): IrSimpleType { + val representation = irClass.inlineClassRepresentation ?: error("Not an inline class: ${irClass.render()}") + return representation.underlyingType } fun getInlineClassBackingField(irClass: IrClass): IrField { diff --git a/compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt b/compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt new file mode 100644 index 00000000000..2adb206661c --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt @@ -0,0 +1,23 @@ +// IGNORE_BACKEND: WASM +// MODULE: lib +// FILE: lib.kt + +inline class Z private constructor(private val value: Any?) { + fun result(): String = value as String + + companion object { + fun create(value: Any?): Z = Z(value) + } +} + +fun interface IFoo { + fun foo(x: T): String +} + +fun foo1(fs: IFoo) = fs.foo(Z.create("OK")) + +// MODULE: main(lib) +// FILE: main.kt + +fun box(): String = + foo1 { it.result() } diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 847659d0cb3..bfecb7b16c1 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -19114,6 +19114,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @Test + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @Test @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 45dc7f7a2dc..c0556b0333a 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -19270,6 +19270,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @Test + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @Test @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a0017f0dd69..d8526b62441 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -15867,6 +15867,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/propertyLoweringOrder.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 29d595f6e45..d24754a368d 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -13816,6 +13816,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/propertyLoweringOrder.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 53664e72bdb..693cbb3a1d7 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -13222,6 +13222,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/propertyLoweringOrder.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index b963e8722fd..bc4d0767233 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -13287,6 +13287,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/propertyLoweringOrder.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 385b9263adb..37833c62cb8 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -7277,6 +7277,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/inlineClasses/passInlineClassWithSpreadOperatorToVarargs.kt"); } + @TestMetadata("privateConstructorFunInterfaceMultiModule.kt") + public void testPrivateConstructorFunInterfaceMultiModule() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/privateConstructorFunInterfaceMultiModule.kt"); + } + @TestMetadata("propertyLoweringOrder.kt") public void testPropertyLoweringOrder() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/propertyLoweringOrder.kt");