IR: use inlineClassRepresentation in getInlineClassUnderlyingType

Looking for the primary constructor manually doesn't work if it's
private in the other module on JVM, because private declarations are
skipped in IrLazyClass.
This commit is contained in:
Alexander Udalov
2021-08-12 13:13:26 +02:00
parent 66dbd91851
commit 8d4f26cf84
13 changed files with 88 additions and 14 deletions
@@ -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 {
@@ -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
}
@@ -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()
}
@@ -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
}
/**
* 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}")
}
}
@@ -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 {
@@ -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<T> {
fun foo(x: T): String
}
fun foo1(fs: IFoo<Z>) = fs.foo(Z.create("OK"))
// MODULE: main(lib)
// FILE: main.kt
fun box(): String =
foo1 { it.result() }
@@ -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 {
@@ -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 {
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");