From 2d88ff6fb291db8b140e9c87bf06e3675e6c52da Mon Sep 17 00:00:00 2001 From: Svyatoslav Kuzmich Date: Mon, 11 Jan 2021 17:00:05 +0300 Subject: [PATCH] [JS IR] Fix unsgined integer default arguemtns (KT-44180) Const lowering didn't exprect null constants with unsigned number types and crashed with NPE. This commit fixes that. --- .../ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../ir/backend/js/lower/ConstLowering.kt | 2 +- .../box/unsignedTypes/defaultArguments.kt | 20 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ .../IrCodegenBoxWasmTestGenerated.java | 5 +++++ 10 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt 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 6849164f38c..38d146b334b 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 @@ -32343,6 +32343,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.kt"); diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ConstLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ConstLowering.kt index fc30602d3b5..267eff257db 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ConstLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ConstLowering.kt @@ -45,7 +45,7 @@ class ConstTransformer(private val context: JsIrBackendContext) : IrElementTrans override fun visitConst(expression: IrConst): IrExpression { with(context.intrinsics) { - if (expression.type.isUnsigned()) { + if (expression.type.isUnsigned() && expression.kind != IrConstKind.Null) { return when (expression.type.classifierOrNull) { uByteClassSymbol -> lowerConst(uByteClassSymbol, IrConstImpl.Companion::byte, IrConstKind.Byte.valueOf(expression)) diff --git a/compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt b/compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt new file mode 100644 index 00000000000..1c73f971e30 --- /dev/null +++ b/compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt @@ -0,0 +1,20 @@ +// WITH_RUNTIME + +// Case of KT-44180 + +fun foo( + p0: UByte = 1u, + p1: UShort = 1u, + p2: UInt = 1u, + p4: ULong = 1uL, +): ULong { + return p0.toULong() + p1.toUShort() + p2.toUInt() + p4 +} + +fun box(): String { + if (foo() != 4uL) return "Fail 1" + if (foo(p2 = 10u) != 13uL) return "Fail 2" + if (foo(10u, 10u, 10u, 10uL) != 40uL) return "Fail 3" + + 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 7692c587f8e..182df123be2 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -32709,6 +32709,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.kt"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 002c29dda39..c04fc51edf2 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -30348,6 +30348,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.kt"); 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 2c8b40a254d..f2f5dabdc9b 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -32343,6 +32343,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.kt"); 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 57da1c7969c..f0c9b263cd1 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 @@ -26154,6 +26154,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.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 21759f0fc3e..cc9d02119a6 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 @@ -26154,6 +26154,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.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 206472c6123..f956b3453ad 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 @@ -26154,6 +26154,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.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 4ef011f76c0..36b3661044b 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 @@ -14352,6 +14352,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt"); } + @TestMetadata("defaultArguments.kt") + public void testDefaultArguments() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/defaultArguments.kt"); + } + @TestMetadata("equalsImplForInlineClassWrappingNullableInlineClass.kt") public void testEqualsImplForInlineClassWrappingNullableInlineClass() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/equalsImplForInlineClassWrappingNullableInlineClass.kt");