From 620a5d404df2f7e3bbed2776682bb861b62d8c6b Mon Sep 17 00:00:00 2001 From: Juan Chen Date: Wed, 21 Oct 2020 05:02:11 +0000 Subject: [PATCH] [FIR2IR] Keep redundant cast on 'this' for local anonymous function #KT-42517 Fixed --- .../kotlin/fir/backend/Fir2IrVisitor.kt | 11 ++++++++--- .../ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/box/smartCasts/kt42517.kt | 19 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 9 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/smartCasts/kt42517.kt diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index d09e3c14604..45ccd01db2a 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.backend import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.fir.* @@ -423,8 +424,10 @@ class Fir2IrVisitor( return visitQualifiedAccessExpression(thisReceiverExpression, data) } - private fun implicitCastOrExpression(original: IrExpression, castType: IrType): IrExpression { - if (original.type == castType) return original + private fun implicitCastOrExpression(original: IrExpression, castType: IrType, isThisInLocalFun: Boolean = false): IrExpression { + // If the original is a "this" in a local function and original.type is the same as castType, + // we still want to keep the cast. See kt-42517 + if (original.type == castType && !isThisInLocalFun) return original return IrTypeOperatorCallImpl( original.startOffset, original.endOffset, @@ -485,7 +488,9 @@ class Fir2IrVisitor( } } } - return implicitCastOrExpression(value, castTypeRef.toIrType()) + val isLocalFun = ((value as? IrGetValue)?.symbol?.owner?.parent as? IrFunction)?.visibility == DescriptorVisibilities.LOCAL + return implicitCastOrExpression(value, castTypeRef.toIrType(), + isLocalFun && expressionWithSmartcast.originalExpression is FirThisReceiverExpression) } override fun visitExpressionWithSmartcast(expressionWithSmartcast: FirExpressionWithSmartcast, data: Any?): IrElement { diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 90a501eabad..bdf2344e07a 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -30049,6 +30049,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/smartCasts/kt19100.kt"); } + @TestMetadata("kt42517.kt") + public void testKt42517() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/kt42517.kt"); + } + @TestMetadata("lambdaArgumentWithoutType.kt") public void testLambdaArgumentWithoutType() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.kt"); diff --git a/compiler/testData/codegen/box/smartCasts/kt42517.kt b/compiler/testData/codegen/box/smartCasts/kt42517.kt new file mode 100644 index 00000000000..55746dd9a93 --- /dev/null +++ b/compiler/testData/codegen/box/smartCasts/kt42517.kt @@ -0,0 +1,19 @@ +// WITH_RUNTIME +// FULL_JDK +// IGNORE_BACKEND: JS_IR +// IGNORE_BACKEND: JS + +fun Any.copyValueIfNeeded(): Any { + return when (this) { + is Array<*> -> java.lang.reflect.Array.newInstance(this::class.java.componentType, size).apply { + this as Array + (this@copyValueIfNeeded as Array).forEachIndexed { i, value -> this[i] = value?.copyValueIfNeeded() } + } + else -> this + } +} + +fun box(): String { + val res = arrayOf("FAIL", "OK").copyValueIfNeeded() as Array + return res[1] +} \ 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 8c4df7b3c65..07ebbdf2936 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -31820,6 +31820,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/smartCasts/kt19100.kt"); } + @TestMetadata("kt42517.kt") + public void testKt42517() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/kt42517.kt"); + } + @TestMetadata("lambdaArgumentWithoutType.kt") public void testLambdaArgumentWithoutType() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a672f6193aa..0050ad92b14 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -29454,6 +29454,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/smartCasts/kt19100.kt"); } + @TestMetadata("kt42517.kt") + public void testKt42517() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/kt42517.kt"); + } + @TestMetadata("lambdaArgumentWithoutType.kt") public void testLambdaArgumentWithoutType() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 2515197cb31..6936966565e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -30049,6 +30049,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/smartCasts/kt19100.kt"); } + @TestMetadata("kt42517.kt") + public void testKt42517() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/kt42517.kt"); + } + @TestMetadata("lambdaArgumentWithoutType.kt") public void testLambdaArgumentWithoutType() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index da30dd69c13..6dda311d562 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -24340,6 +24340,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/smartCasts/kt19100.kt"); } + @TestMetadata("kt42517.kt") + public void testKt42517() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/kt42517.kt"); + } + @TestMetadata("lambdaArgumentWithoutType.kt") public void testLambdaArgumentWithoutType() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 84db96f38d4..d7dccec904a 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -24340,6 +24340,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/smartCasts/kt19100.kt"); } + @TestMetadata("kt42517.kt") + public void testKt42517() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/kt42517.kt"); + } + @TestMetadata("lambdaArgumentWithoutType.kt") public void testLambdaArgumentWithoutType() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.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 414a0eec4f7..2c9ca1667a0 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 @@ -24355,6 +24355,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/smartCasts/kt19100.kt"); } + @TestMetadata("kt42517.kt") + public void testKt42517() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/kt42517.kt"); + } + @TestMetadata("lambdaArgumentWithoutType.kt") public void testLambdaArgumentWithoutType() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.kt");