From 2c5a4dcb985ef2c91787d5bfd459fefbdbc47c95 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Mon, 10 May 2021 15:08:54 +0200 Subject: [PATCH] [JVM IR] Fix constant folding to use basic types always. Fixes KT-46540. --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++ .../optimizations/FoldConstantLowering.kt | 21 +++++++------ .../testData/codegen/box/fullJdk/kt46540.kt | 30 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ 6 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 compiler/testData/codegen/box/fullJdk/kt46540.kt 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 3ad6bf2dac2..c72cf8b5f35 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 @@ -15566,6 +15566,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/fullJdk/kt434.kt"); } + @Test + @TestMetadata("kt46540.kt") + public void testKt46540() throws Exception { + runTest("compiler/testData/codegen/box/fullJdk/kt46540.kt"); + } + @Test @TestMetadata("platformTypeAssertionStackTrace.kt") public void testPlatformTypeAssertionStackTrace() throws Exception { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt index 810433ada42..d987d48727b 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt @@ -105,18 +105,17 @@ class FoldConstantLowering( } private fun buildIrConstant(startOffset: Int, endOffset: Int, type: IrType, v: Any?): IrConst<*> { - val constType = type.makeNotNull() return when (type.getPrimitiveType()) { - PrimitiveType.BOOLEAN -> IrConstImpl.boolean(startOffset, endOffset, constType, v as Boolean) - PrimitiveType.CHAR -> IrConstImpl.char(startOffset, endOffset, constType, v as Char) - PrimitiveType.BYTE -> IrConstImpl.byte(startOffset, endOffset, constType, (v as Number).toByte()) - PrimitiveType.SHORT -> IrConstImpl.short(startOffset, endOffset, constType, (v as Number).toShort()) - PrimitiveType.INT -> IrConstImpl.int(startOffset, endOffset, constType, (v as Number).toInt()) - PrimitiveType.FLOAT -> fromFloatConstSafe(startOffset, endOffset, type, v) - PrimitiveType.LONG -> IrConstImpl.long(startOffset, endOffset, constType, (v as Number).toLong()) - PrimitiveType.DOUBLE -> IrConstImpl.double(startOffset, endOffset, constType, (v as Number).toDouble()) + PrimitiveType.BOOLEAN -> IrConstImpl.boolean(startOffset, endOffset, context.irBuiltIns.booleanType, v as Boolean) + PrimitiveType.CHAR -> IrConstImpl.char(startOffset, endOffset, context.irBuiltIns.charType, v as Char) + PrimitiveType.BYTE -> IrConstImpl.byte(startOffset, endOffset, context.irBuiltIns.byteType, (v as Number).toByte()) + PrimitiveType.SHORT -> IrConstImpl.short(startOffset, endOffset, context.irBuiltIns.shortType, (v as Number).toShort()) + PrimitiveType.INT -> IrConstImpl.int(startOffset, endOffset, context.irBuiltIns.intType, (v as Number).toInt()) + PrimitiveType.FLOAT -> fromFloatConstSafe(startOffset, endOffset, context.irBuiltIns.floatType, v) + PrimitiveType.LONG -> IrConstImpl.long(startOffset, endOffset, context.irBuiltIns.longType, (v as Number).toLong()) + PrimitiveType.DOUBLE -> IrConstImpl.double(startOffset, endOffset, context.irBuiltIns.doubleType, (v as Number).toDouble()) else -> when { - constType.isString() -> IrConstImpl.string(startOffset, endOffset, constType, v as String) + type.isStringClassType() -> IrConstImpl.string(startOffset, endOffset, context.irBuiltIns.stringType, v as String) else -> throw IllegalArgumentException("Unexpected IrCall return type") } } @@ -289,4 +288,4 @@ class FoldConstantLowering( } }) } -} \ No newline at end of file +} diff --git a/compiler/testData/codegen/box/fullJdk/kt46540.kt b/compiler/testData/codegen/box/fullJdk/kt46540.kt new file mode 100644 index 00000000000..fd5311e19cc --- /dev/null +++ b/compiler/testData/codegen/box/fullJdk/kt46540.kt @@ -0,0 +1,30 @@ +// TARGET_BACKEND: JVM +// SKIP_JDK6 +// FULL_JDK + +import java.util.function.Supplier + +class A { + val xLong: Supplier + get() = Supplier { 30 * 30 } + + val xShort: Supplier + get() = Supplier { 30 * 30 } + + val xInt: Supplier + get() = Supplier { 30 * 30 } + + val xByte: Supplier + get() = Supplier { 3 * 3 } +} + +fun box(): String { + val a = A() + if (a.xLong.get() != 900L) return "FAIL1" + var expectedShort: Short = 900 + if (a.xShort.get() != expectedShort) return "FAIL2" + if (a.xInt.get() != 900) return "FAIL3" + val expectedByte: Byte = 9 + if (a.xByte.get() != expectedByte) return "FAIL4" + return "OK" +} 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 1949f55b6b8..1624a6288d2 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 @@ -15542,6 +15542,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/fullJdk/kt434.kt"); } + @Test + @TestMetadata("kt46540.kt") + public void testKt46540() throws Exception { + runTest("compiler/testData/codegen/box/fullJdk/kt46540.kt"); + } + @Test @TestMetadata("platformTypeAssertionStackTrace.kt") public void testPlatformTypeAssertionStackTrace() 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 d4db1feb7cc..45e257e5f43 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 @@ -15566,6 +15566,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fullJdk/kt434.kt"); } + @Test + @TestMetadata("kt46540.kt") + public void testKt46540() throws Exception { + runTest("compiler/testData/codegen/box/fullJdk/kt46540.kt"); + } + @Test @TestMetadata("platformTypeAssertionStackTrace.kt") public void testPlatformTypeAssertionStackTrace() 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 3b94800a85a..d30d3dca426 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -12797,6 +12797,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/fullJdk/kt434.kt"); } + @TestMetadata("kt46540.kt") + public void testKt46540() throws Exception { + runTest("compiler/testData/codegen/box/fullJdk/kt46540.kt"); + } + @TestMetadata("platformTypeAssertionStackTrace.kt") public void testPlatformTypeAssertionStackTrace() throws Exception { runTest("compiler/testData/codegen/box/fullJdk/platformTypeAssertionStackTrace.kt");