From 2b0a99b7b01bc3fa9d00d78696a69027c617549b Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Fri, 11 Dec 2020 11:43:43 +0100 Subject: [PATCH] IC Mangling: Use correct java field type if the type is inline class in old JVM BE. #KT-26445 --- .../kotlin/codegen/ExpressionCodegen.java | 12 ++++++- .../ir/FirBlackBoxCodegenTestGenerated.java | 10 ++++++ .../javaInterop/inlineClasInSignature.kt | 14 ++++---- .../inlineClasInSignatureNonNull.kt | 34 +++++++++++++++++++ .../inlineClasInSignatureNullable.kt | 34 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 ++++++ .../LightAnalysisModeTestGenerated.java | 10 ++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 10 ++++++ 8 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt create mode 100644 compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index f2c109bf80d..0f6f65eb51a 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -54,6 +54,7 @@ import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor; import org.jetbrains.kotlin.diagnostics.Errors; import org.jetbrains.kotlin.lexer.KtTokens; import org.jetbrains.kotlin.load.java.DescriptorsJvmAbiUtil; +import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor; import org.jetbrains.kotlin.load.kotlin.DescriptorBasedTypeSignatureMappingKt; import org.jetbrains.kotlin.load.kotlin.MethodSignatureMappingKt; import org.jetbrains.kotlin.name.Name; @@ -2404,9 +2405,14 @@ public class ExpressionCodegen extends KtVisitor impleme fieldName = KotlinTypeMapper.mapDefaultFieldName(propertyDescriptor, isDelegatedProperty); } + KotlinType propertyType = propertyDescriptor.getOriginal().getType(); + if (propertyDescriptor instanceof JavaPropertyDescriptor && InlineClassesUtilsKt.isInlineClassType(propertyType)) { + propertyType = TypeUtils.makeNullable(propertyType); + } + return StackValue.property( propertyDescriptor, backingFieldOwner, - typeMapper.mapType(isDelegatedProperty && forceField ? delegateType : propertyDescriptor.getOriginal().getType()), + typeMapper.mapType(isDelegatedProperty && forceField ? delegateType : propertyType), isStaticBackingField, fieldName, callableGetter, callableSetter, receiver, this, resolvedCall, skipLateinitAssertion, isDelegatedProperty && forceField ? delegateType : null ); @@ -4375,6 +4381,10 @@ public class ExpressionCodegen extends KtVisitor impleme Type exprType = expressionType(expr); KotlinType exprKotlinType = kotlinType(expr); + if (exprKotlinType != null && InlineClassesUtilsKt.isInlineClassType(exprKotlinType) && + FlexibleTypesKt.isNullabilityFlexible(exprKotlinType)) { + exprKotlinType = TypeUtils.makeNullable(exprKotlinType); + } StackValue value; if (compileTimeConstant != null) { value = StackValue.constant(compileTimeConstant.getValue(), exprType, exprKotlinType); diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 7f5538d7c35..95a931926c1 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -15129,6 +15129,16 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT public void testInlineClasInSignature() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt"); } + + @TestMetadata("inlineClasInSignatureNonNull.kt") + public void testInlineClasInSignatureNonNull() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt"); + } + + @TestMetadata("inlineClasInSignatureNullable.kt") + public void testInlineClasInSignatureNullable() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt"); + } } @TestMetadata("compiler/testData/codegen/box/inlineClasses/jvm8DefaultInterfaceMethods") diff --git a/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt b/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt index 4e0731238fa..105c8b34707 100644 --- a/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt +++ b/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt @@ -5,16 +5,14 @@ // FILE: WithInlineClass.java import kotlin.UInt; -import org.jetbrains.annotations.NotNull; public class WithInlineClass { - private static UInt UINT = null; + public static UInt UINT = null; - public static void acceptsUInt(@NotNull UInt u) { + public static void acceptsUInt(UInt u) { UINT = u; } - @NotNull public static UInt provideUInt() { return UINT; } @@ -24,6 +22,10 @@ public class WithInlineClass { fun box(): String { WithInlineClass.acceptsUInt(1u) - val res = WithInlineClass.provideUInt() - return if (res == 1u) "OK" else "FAIL $res" + var res = WithInlineClass.provideUInt() + if (res != 1u) return "FAIL 1 $res" + WithInlineClass.UINT = 2u + res = WithInlineClass.UINT + if (res != 2u) return "FAIL 2 $res" + return "OK" } \ No newline at end of file diff --git a/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt b/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt new file mode 100644 index 00000000000..74ce893cbd3 --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt @@ -0,0 +1,34 @@ +// LANGUAGE: +InlineClasses +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +// FILE: WithInlineClass.java + +import kotlin.UInt; +import org.jetbrains.annotations.NotNull; + +public class WithInlineClass { + @NotNull + public static UInt UINT = null; + + public static void acceptsUInt(@NotNull UInt u) { + UINT = u; + } + + @NotNull + public static UInt provideUInt() { + return UINT; + } +} + +// FILE: box.kt + +fun box(): String { + WithInlineClass.acceptsUInt(1u) + var res = WithInlineClass.provideUInt() + if (res != 1u) return "FAIL 1 $res" + WithInlineClass.UINT = 2u + res = WithInlineClass.UINT + if (res != 2u) return "FAIL 2 $res" + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt b/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt new file mode 100644 index 00000000000..f8f2aad8bc7 --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt @@ -0,0 +1,34 @@ +// LANGUAGE: +InlineClasses +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +// FILE: WithInlineClass.java + +import kotlin.UInt; +import org.jetbrains.annotations.Nullable; + +public class WithInlineClass { + @Nullable + public static UInt UINT = null; + + public static void acceptsUInt(@Nullable UInt u) { + UINT = u; + } + + @Nullable + public static UInt provideUInt() { + return UINT; + } +} + +// FILE: box.kt + +fun box(): String { + WithInlineClass.acceptsUInt(1u) + var res = WithInlineClass.provideUInt() + if (res != 1u) return "FAIL 1 $res" + WithInlineClass.UINT = 2u + res = WithInlineClass.UINT + if (res != 2u) return "FAIL 2 $res" + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 394dd5e72e9..67a85554678 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -15129,6 +15129,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { public void testInlineClasInSignature() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt"); } + + @TestMetadata("inlineClasInSignatureNonNull.kt") + public void testInlineClasInSignatureNonNull() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt"); + } + + @TestMetadata("inlineClasInSignatureNullable.kt") + public void testInlineClasInSignatureNullable() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt"); + } } @TestMetadata("compiler/testData/codegen/box/inlineClasses/jvm8DefaultInterfaceMethods") diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 2539216d812..3d89f468b2a 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -15129,6 +15129,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes public void testInlineClasInSignature() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt"); } + + @TestMetadata("inlineClasInSignatureNonNull.kt") + public void testInlineClasInSignatureNonNull() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt"); + } + + @TestMetadata("inlineClasInSignatureNullable.kt") + public void testInlineClasInSignatureNullable() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt"); + } } @TestMetadata("compiler/testData/codegen/box/inlineClasses/jvm8DefaultInterfaceMethods") diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 2445277dba9..5c2dee7064d 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -15129,6 +15129,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes public void testInlineClasInSignature() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignature.kt"); } + + @TestMetadata("inlineClasInSignatureNonNull.kt") + public void testInlineClasInSignatureNonNull() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNonNull.kt"); + } + + @TestMetadata("inlineClasInSignatureNullable.kt") + public void testInlineClasInSignatureNullable() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/javaInterop/inlineClasInSignatureNullable.kt"); + } } @TestMetadata("compiler/testData/codegen/box/inlineClasses/jvm8DefaultInterfaceMethods")