From 533f87ac3342db24da0ed594a006bfbd18ab1277 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 21 Aug 2018 12:27:45 +0300 Subject: [PATCH] Use proper KotlinType for prefix increment/decrement Otherwise inline class values (such as UByte) are boxed incorrectly, see KT-26219. --- .../jetbrains/kotlin/codegen/StackValue.java | 12 +- ...ignedTypePrefixIncrementDecrementBoxing.kt | 117 ++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 + .../LightAnalysisModeTestGenerated.java | 5 + .../ir/IrBlackBoxCodegenTestGenerated.java | 5 + .../IrJsCodegenBoxTestGenerated.java | 5 + .../semantics/JsCodegenBoxTestGenerated.java | 5 + 7 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java index 36b6ad8df57..c977c62ba27 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java @@ -698,8 +698,8 @@ public abstract class StackValue { return new PostIncrement(index, increment); } - public static StackValue preIncrementForLocalVar(int index, int increment) { - return new PreIncrementForLocalVar(index, increment); + public static StackValue preIncrementForLocalVar(int index, int increment, @Nullable KotlinType kotlinType) { + return new PreIncrementForLocalVar(index, increment, kotlinType); } public static StackValue preIncrement( @@ -710,7 +710,7 @@ public abstract class StackValue { @NotNull ExpressionCodegen codegen ) { if (stackValue instanceof StackValue.Local && Type.INT_TYPE == stackValue.type) { - return preIncrementForLocalVar(((StackValue.Local) stackValue).index, delta); + return preIncrementForLocalVar(((StackValue.Local) stackValue).index, delta, stackValue.kotlinType); } return new PrefixIncrement(type, stackValue, resolvedCall, codegen); } @@ -1834,8 +1834,8 @@ public abstract class StackValue { private final int index; private final int increment; - public PreIncrementForLocalVar(int index, int increment) { - super(Type.INT_TYPE); + public PreIncrementForLocalVar(int index, int increment, @Nullable KotlinType kotlinType) { + super(Type.INT_TYPE, kotlinType); this.index = index; this.increment = increment; } @@ -1861,7 +1861,7 @@ public abstract class StackValue { ResolvedCall resolvedCall, @NotNull ExpressionCodegen codegen ) { - super(type); + super(type, value.kotlinType); this.value = value; this.resolvedCall = resolvedCall; this.codegen = codegen; diff --git a/compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt b/compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt new file mode 100644 index 00000000000..b4279b0d2b5 --- /dev/null +++ b/compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt @@ -0,0 +1,117 @@ +// WITH_UNSIGNED +// IGNORE_BACKEND: JS_IR, JVM_IR, JS + +fun prefixDecrementUByteLocal(): Any? { + var a: UByte = 0u + return --a +} + +fun prefixDecrementUShortLocal(): Any? { + var a: UShort = 0u + return --a +} + +fun prefixDecrementUIntLocal(): Any? { + var a: UInt = 0u + return --a +} + +fun prefixDecrementULongLocal(): Any? { + var a: ULong = 0u + return --a +} + +fun prefixIncrementUByteLocal(): Any? { + var a: UByte = 0u + return ++a +} + +fun prefixIncrementUShortLocal(): Any? { + var a: UShort = 0u + return ++a +} + +fun prefixIncrementUIntLocal(): Any? { + var a: UInt = 0u + return ++a +} + +fun prefixIncrementULongLocal(): Any? { + var a: ULong = 0u + return ++a +} + +var gb: UByte = 0u +var gs: UShort = 0u +var gi: UInt = 0u +var gl: ULong = 0UL + +fun prefixDecrementUByteProperty(): Any? { + gb = 0u + return --gb +} + +fun prefixDecrementUShortProperty(): Any? { + gs = 0u + return --gs +} + +fun prefixDecrementUIntProperty(): Any? { + gi = 0u + return --gi +} + +fun prefixDecrementULongProperty(): Any? { + gl = 0u + return --gl +} + +fun prefixIncrementUByteProperty(): Any? { + gb = 0u + return ++gb +} + +fun prefixIncrementUShortProperty(): Any? { + gs = 0u + return ++gs +} + +fun prefixIncrementUIntProperty(): Any? { + gi = 0u + return ++gi +} + +fun prefixIncrementULongProperty(): Any? { + gl = 0u + return ++gl +} + +fun check(u: Any?, ts: String, className: String) { + u!! + if (u.toString() != ts) throw AssertionError(u.toString()) + if (u.javaClass.name != className) throw AssertionError(u.javaClass.name) +} + +fun box(): String { + check(prefixDecrementUByteLocal(), 0xFFu.toString(), "kotlin.UByte") + check(prefixDecrementUShortLocal(), 0xFFFFu.toString(), "kotlin.UShort") + check(prefixDecrementUIntLocal(), 0xFFFF_FFFFu.toString(), "kotlin.UInt") + check(prefixDecrementULongLocal(), 0xFFFF_FFFF_FFFF_FFFFUL.toString(), "kotlin.ULong") + + check(prefixIncrementUByteLocal(), "1", "kotlin.UByte") + check(prefixIncrementUShortLocal(), "1", "kotlin.UShort") + check(prefixIncrementUIntLocal(), "1", "kotlin.UInt") + check(prefixIncrementULongLocal(), "1", "kotlin.ULong") + + check(prefixDecrementUByteProperty(), 0xFFu.toString(), "kotlin.UByte") + check(prefixDecrementUShortProperty(), 0xFFFFu.toString(), "kotlin.UShort") + check(prefixDecrementUIntProperty(), 0xFFFF_FFFFu.toString(), "kotlin.UInt") + check(prefixDecrementULongProperty(), 0xFFFF_FFFF_FFFF_FFFFUL.toString(), "kotlin.ULong") + + check(prefixIncrementUByteProperty(), "1", "kotlin.UByte") + check(prefixIncrementUShortProperty(), "1", "kotlin.UShort") + check(prefixIncrementUIntProperty(), "1", "kotlin.UInt") + check(prefixIncrementULongProperty(), "1", "kotlin.ULong") + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 3fa6faddadf..032d101466f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -22020,6 +22020,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); } + @TestMetadata("unsignedTypePrefixIncrementDecrementBoxing.kt") + public void testUnsignedTypePrefixIncrementDecrementBoxing() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt"); + } + @TestMetadata("unsignedTypeValuesInsideStringTemplates.kt") public void testUnsignedTypeValuesInsideStringTemplates() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypeValuesInsideStringTemplates.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 7accc803f05..e7a078c52a7 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -22020,6 +22020,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); } + @TestMetadata("unsignedTypePrefixIncrementDecrementBoxing.kt") + public void testUnsignedTypePrefixIncrementDecrementBoxing() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt"); + } + @TestMetadata("unsignedTypeValuesInsideStringTemplates.kt") public void testUnsignedTypeValuesInsideStringTemplates() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypeValuesInsideStringTemplates.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 1122b771ed5..9d19d2a329e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -22020,6 +22020,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); } + @TestMetadata("unsignedTypePrefixIncrementDecrementBoxing.kt") + public void testUnsignedTypePrefixIncrementDecrementBoxing() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt"); + } + @TestMetadata("unsignedTypeValuesInsideStringTemplates.kt") public void testUnsignedTypeValuesInsideStringTemplates() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypeValuesInsideStringTemplates.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index cec47a54f2e..ce5a8a897df 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -19865,6 +19865,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/unsignedTypes/signedToUnsignedLiteralConversion.kt"); } + @TestMetadata("unsignedTypePrefixIncrementDecrementBoxing.kt") + public void testUnsignedTypePrefixIncrementDecrementBoxing() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt"); + } + @TestMetadata("unsignedTypeValuesInsideStringTemplates.kt") public void testUnsignedTypeValuesInsideStringTemplates() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypeValuesInsideStringTemplates.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index b2d63b21045..94eeab2e04b 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -20925,6 +20925,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/unsignedTypes/signedToUnsignedLiteralConversion.kt"); } + @TestMetadata("unsignedTypePrefixIncrementDecrementBoxing.kt") + public void testUnsignedTypePrefixIncrementDecrementBoxing() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt"); + } + @TestMetadata("unsignedTypeValuesInsideStringTemplates.kt") public void testUnsignedTypeValuesInsideStringTemplates() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypeValuesInsideStringTemplates.kt");