From 3be3ae48955c51e4b12e9963c5d27b59fb47de39 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Wed, 7 Dec 2022 19:51:43 +0100 Subject: [PATCH] [Wasm] Fix invalid boxing for non-primitive typed vararg --- .../FirBlackBoxCodegenTestGenerated.java | 6 +++ .../backend/js/lower/AutoboxingTransformer.kt | 10 ++++- .../wasm/lower/WasmTypeOperatorLowering.kt | 4 +- .../box/vararg/boxingArgumentsForVararg.kt | 40 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++ .../LightAnalysisModeTestGenerated.java | 5 +++ .../js/test/JsCodegenBoxTestGenerated.java | 6 +++ .../fir/FirJsCodegenBoxTestGenerated.java | 6 +++ .../test/ir/IrJsCodegenBoxTestGenerated.java | 6 +++ .../IrCodegenBoxWasmTestGenerated.java | 5 +++ .../NativeCodegenBoxTestGenerated.java | 6 +++ 12 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.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 787bce512e6..b25b03bfdc7 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 @@ -50733,6 +50733,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/vararg/assigningArrayToVarargInAnnotation.kt"); } + @Test + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @Test @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() throws Exception { diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt index e6df7e9286b..91ad2f10ced 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt @@ -114,7 +114,7 @@ abstract class AbstractValueUsageLowering(val context: JsCommonBackendContext) : if (icUtils.isTypeInlined(element.type) && !icUtils.isTypeInlined(expression.type) && !expression.type.isPrimitiveArray()) irBuiltIns.anyNType else - expression.varargElementType + if (!expression.type.isPrimitiveArray()) irBuiltIns.anyNType else expression.varargElementType ) } @@ -136,6 +136,14 @@ class AutoboxingTransformer(context: JsCommonBackendContext) : AbstractValueUsag return res } + override fun visitFunction(declaration: IrFunction): IrStatement { + return super.visitFunction(declaration) + } + + override fun visitVararg(expression: IrVararg): IrExpression { + return super.visitVararg(expression) + } + override fun IrExpression.useAsResult(enclosing: IrExpression): IrExpression { if (processingReturnStack.lastOrNull()?.value == enclosing && enclosing is IrReturnableBlock) { return useReturnableExpressionAsType(enclosing.type) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt index c89585d1cde..b6f874d036e 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt @@ -36,7 +36,9 @@ class WasmBaseTypeOperatorTransformer(val context: WasmBackendContext) : IrEleme private val builtIns = context.irBuiltIns private lateinit var builder: DeclarationIrBuilder - + override fun visitSimpleFunction(declaration: IrSimpleFunction): IrStatement { + return super.visitSimpleFunction(declaration) + } override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression { super.visitTypeOperator(expression) builder = context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol).at(expression) diff --git a/compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt b/compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt new file mode 100644 index 00000000000..2b06ce0dd1e --- /dev/null +++ b/compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt @@ -0,0 +1,40 @@ +fun anyVararg(vararg x: T): Boolean = x[0] == 123f + +fun boxingNullablePrimitiveToAny(x: Float?): Boolean { + if (x !== null) { + return anyVararg(x) + } + return false +} + +fun boxingPrimitiveToAny(x: Float): Boolean = + anyVararg(x) + +fun primitiveVararg(vararg x: Float): Boolean = x[0] == 123f + +fun unboxingNullablePrimitiveToPrimitive(x: Float?): Boolean { + if (x !== null) { + return primitiveVararg(x) + } + return false +} + +fun noBoxingPrimitiveToPrimitive(x: Float): Boolean = + primitiveVararg(x) + +inline class InlineClass(val x: Float) + +fun valueClassAnyVararg(vararg x: T): Boolean = x[0].x == 123f + +fun boxingInlineClassToAny(x: InlineClass): Boolean = + valueClassAnyVararg(x) + + +fun box(): String { + if (!boxingNullablePrimitiveToAny(123f)) return "Fail1" + if (!boxingPrimitiveToAny(123f)) return "Fail2" + if (!unboxingNullablePrimitiveToPrimitive(123f)) return "Fail3" + if (!noBoxingPrimitiveToPrimitive(123f)) return "Fail4" + if (!boxingInlineClassToAny(InlineClass(123f))) return "Fail5" + return "OK" +} \ No newline at end of file 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 322d6396f65..3f986c2c7fd 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 @@ -48741,6 +48741,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/vararg/assigningArrayToVarargInAnnotation.kt"); } + @Test + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @Test @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() 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 65042f4244a..3bc6a038e8d 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 @@ -50733,6 +50733,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/vararg/assigningArrayToVarargInAnnotation.kt"); } + @Test + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @Test @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() 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 3314aabcbb3..f93bebf3602 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -39600,6 +39600,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/vararg/assigningArrayToVarargInAnnotation.kt"); } + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() throws Exception { runTest("compiler/testData/codegen/box/vararg/doNotCopyImmediatelyCreatedArrays.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java index 9c63201685b..70d443f10bd 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java @@ -36227,6 +36227,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/vararg"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); } + @Test + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @Test @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java index eaca993ae35..1e8df2925db 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java @@ -36407,6 +36407,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/vararg"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } + @Test + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @Test @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index 617a00a7f26..8f684cec56d 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -36407,6 +36407,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/vararg"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } + @Test + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @Test @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index d6f2832d850..93d2177a66c 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -32610,6 +32610,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/vararg"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true); } + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() throws Exception { runTest("compiler/testData/codegen/box/vararg/doNotCopyImmediatelyCreatedArrays.kt"); diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index 03b15e77787..34e32b3974f 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -39552,6 +39552,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/vararg"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true); } + @Test + @TestMetadata("boxingArgumentsForVararg.kt") + public void testBoxingArgumentsForVararg() throws Exception { + runTest("compiler/testData/codegen/box/vararg/boxingArgumentsForVararg.kt"); + } + @Test @TestMetadata("doNotCopyImmediatelyCreatedArrays.kt") public void testDoNotCopyImmediatelyCreatedArrays() throws Exception {