[FIR2IR] Keep redundant cast on 'this' for local anonymous function

#KT-42517 Fixed
This commit is contained in:
Juan Chen
2020-10-21 05:02:11 +00:00
committed by Mikhail Glukhikh
parent f4531b0f34
commit 620a5d404d
9 changed files with 62 additions and 3 deletions
@@ -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 {
@@ -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");
+19
View File
@@ -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<Any?>
(this@copyValueIfNeeded as Array<Any?>).forEachIndexed { i, value -> this[i] = value?.copyValueIfNeeded() }
}
else -> this
}
}
fun box(): String {
val res = arrayOf("FAIL", "OK").copyValueIfNeeded() as Array<String>
return res[1]
}
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");