From d3d4e94cd6eb9f4bd0a7abfe0a8ec369b618c5a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Tue, 20 Apr 2021 21:18:30 +0200 Subject: [PATCH] JVM: Fix constant folding for unsigned values --- .../FirBlackBoxCodegenTestGenerated.java | 6 +++ .../evaluate/ConstantExpressionEvaluator.kt | 6 ++- .../box/constants/foldingBinaryOpsUnsigned.kt | 1 - .../foldingBinaryOpsUnsignedConst.kt | 42 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++ .../LightAnalysisModeTestGenerated.java | 15 ++++--- .../resolve/constants/constantValues.kt | 17 +++----- .../IrJsCodegenBoxES6TestGenerated.java | 5 +++ .../IrJsCodegenBoxTestGenerated.java | 5 +++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++ .../IrCodegenBoxWasmTestGenerated.java | 5 +++ 12 files changed, 99 insertions(+), 20 deletions(-) create mode 100644 compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.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 84562679595..d92a7e6ca82 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 @@ -7172,6 +7172,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); } + @Test + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @Test @TestMetadata("kt9532.kt") public void testKt9532() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt index e185805cc36..664fa4ea8fb 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt @@ -12,6 +12,7 @@ import com.intellij.psi.util.TypeConversionUtil import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.StandardNames +import org.jetbrains.kotlin.builtins.UnsignedType import org.jetbrains.kotlin.builtins.UnsignedTypes import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageVersionSettings @@ -401,7 +402,7 @@ private class ConstantExpressionEvaluatorVisitor( return when (constantValue) { is ErrorValue, is EnumValue -> return null is NullValue -> StringValue("null") - else -> StringValue(constantValue.stringTemplateValue()) + else -> StringValue(constantValue.boxedValue().toString()) }.wrap(compileTimeConstant.parameters) } @@ -984,7 +985,8 @@ private class ConstantExpressionEvaluatorVisitor( ): OperationArgument? { val compileTimeConstant = constantExpressionEvaluator.evaluateExpression(expression, trace, parameterType) ?: return null if (compileTimeConstant is TypedCompileTimeConstant && !compileTimeConstant.type.isSubtypeOf(parameterType)) return null - val evaluationResult = compileTimeConstant.getValue(parameterType) ?: return null + val constantValue = compileTimeConstant.toConstantValue(parameterType) + val evaluationResult = (if (compileTimeType == ANY) constantValue.boxedValue() else constantValue.value) ?: return null return OperationArgument(evaluationResult, compileTimeType, expression) } diff --git a/compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt b/compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt index 0c84fd37519..64d47ca4b04 100644 --- a/compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt +++ b/compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt @@ -1,5 +1,4 @@ // WITH_RUNTIME -// IGNORE_BACKEND: JVM // IGNORE_BACKEND: WASM val a = "INT " + 0x8fffffffU diff --git a/compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt b/compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt new file mode 100644 index 00000000000..c0bcdaf295f --- /dev/null +++ b/compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt @@ -0,0 +1,42 @@ +// WITH_RUNTIME +// IGNORE_BACKEND_FIR: JVM_IR +// IGNORE_BACKEND: WASM + +const val a = "INT " + 0x8fffffffU +const val b = "BYTE " + 0x8ffU +const val c = "LONG " + 0xffff_ffff_ffffU + +const val uint = 0x8fffffffU +const val ubyte = 0x8ffU +const val ulong = 0xffff_ffff_ffffU + +const val aa = "INT " + uint +const val bb = "BYTE " + ubyte +const val cc = "LONG " + ulong + + +fun box(): String { + if (a != "INT 2415919103") { + return "FAIL 0: $a" + } + if (aa != "INT 2415919103") { + return "FAIL 1: $aa" + } + + if (b != "BYTE 2303") { + return "FAIL 2: $b" + } + if (bb != "BYTE 2303") { + return "FAIL 3: $bb" + } + + + if (c != "LONG 281474976710655") { + return "FAIL 4: $c" + } + if (cc != "LONG 281474976710655") { + return "FAIL 5: $cc" + } + + 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 f6d5e29781e..f995651f1b0 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 @@ -7172,6 +7172,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); } + @Test + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @Test @TestMetadata("kt9532.kt") public void testKt9532() 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 bf9a539406d..937981e4292 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 @@ -7172,6 +7172,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); } + @Test + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @Test @TestMetadata("kt9532.kt") public void testKt9532() 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 3ff6095eed1..f2338b03088 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -5387,11 +5387,6 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class Constants extends AbstractLightAnalysisModeTest { - @TestMetadata("foldingBinaryOpsUnsigned.kt") - public void ignoreFoldingBinaryOpsUnsigned() throws Exception { - runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); - } - private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); } @@ -5430,6 +5425,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/constants/float.kt"); } + @TestMetadata("foldingBinaryOpsUnsigned.kt") + public void testFoldingBinaryOpsUnsigned() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); + } + + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @TestMetadata("kt9532.kt") public void testKt9532() throws Exception { runTest("compiler/testData/codegen/box/constants/kt9532.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt index 1400ce39fdd..9af294c7c29 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt @@ -43,7 +43,7 @@ abstract class ConstantValue(open val value: T) { override fun toString(): String = value.toString() - open fun stringTemplateValue(): String = value.toString() + open fun boxedValue(): Any? = value } abstract class IntegerValueConstant protected constructor(value: T) : ConstantValue(value) @@ -269,7 +269,7 @@ class UByteValue(byteValue: Byte) : UnsignedValueConstant(byteValue) { override fun toString() = "$value.toUByte()" - override fun stringTemplateValue(): String = (value.toInt() and 0xFF).toString() + override fun boxedValue(): Any = value.toUByte() } class UShortValue(shortValue: Short) : UnsignedValueConstant(shortValue) { @@ -282,7 +282,7 @@ class UShortValue(shortValue: Short) : UnsignedValueConstant(shortValue) override fun toString() = "$value.toUShort()" - override fun stringTemplateValue(): String = (value.toInt() and 0xFFFF).toString() + override fun boxedValue(): Any = value.toUShort() } class UIntValue(intValue: Int) : UnsignedValueConstant(intValue) { @@ -295,7 +295,7 @@ class UIntValue(intValue: Int) : UnsignedValueConstant(intValue) { override fun toString() = "$value.toUInt()" - override fun stringTemplateValue(): String = (value.toLong() and 0xFFFFFFFFL).toString() + override fun boxedValue(): Any = value.toUInt() } class ULongValue(longValue: Long) : UnsignedValueConstant(longValue) { @@ -308,12 +308,5 @@ class ULongValue(longValue: Long) : UnsignedValueConstant(longValue) { override fun toString() = "$value.toULong()" - override fun stringTemplateValue(): String { - if (value >= 0) return value.toString() - - val div10 = (value ushr 1) / 5 - val mod10 = value - 10 * div10 - - return "$div10$mod10" - } + override fun boxedValue(): Any = value.toULong() } 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 c1d1eec6128..3f09b682219 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 @@ -4839,6 +4839,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); } + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @TestMetadata("kt9532.kt") public void testKt9532() throws Exception { runTest("compiler/testData/codegen/box/constants/kt9532.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 bb48df6d256..c95b80f830c 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 @@ -4250,6 +4250,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); } + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @TestMetadata("kt9532.kt") public void testKt9532() throws Exception { runTest("compiler/testData/codegen/box/constants/kt9532.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 be3a1b9c1e0..36a78dad8b4 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 @@ -4250,6 +4250,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); } + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @TestMetadata("kt9532.kt") public void testKt9532() throws Exception { runTest("compiler/testData/codegen/box/constants/kt9532.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 c0e431ff193..72dbcda88cd 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 @@ -3061,6 +3061,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt"); } + @TestMetadata("foldingBinaryOpsUnsignedConst.kt") + public void testFoldingBinaryOpsUnsignedConst() throws Exception { + runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt"); + } + @TestMetadata("long.kt") public void testLong() throws Exception { runTest("compiler/testData/codegen/box/constants/long.kt");