JVM IR: do not try to optimize casts in TypeOperatorLowering

In case the cast value is used as a receiver to a private method call,
the cast is actually necessary, see KT-48927. Also, this optimization
has backfired once already (see kt48659_identityEqualsWithCastToAny.kt).
It seems that the best way to optimize these casts is not to generate
them in the first place, and/or use bytecode postprocessing.

Apparently the only kind of casts which need to be eliminated are those
which occur on an inline class to its supertype. Otherwise the
unsafe-coerce intrinsic is inserted at the incorrect place, and several
tests fail (uncastInlineClassToAnyAndBack.kt, genericOverride.kt,
classGenericOverride.kt).

 #KT-48927 Fixed
This commit is contained in:
Alexander Udalov
2021-09-30 03:01:42 +02:00
parent aaa0b41416
commit b821b26cfe
11 changed files with 110 additions and 2 deletions
@@ -4340,6 +4340,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@Test
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
@@ -19905,6 +19911,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@Test
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@Test
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
@@ -93,8 +93,7 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
)
}
}
// Do not optimize casts for values of primitive types because it can affect their identity and thus change behavior.
!argument.type.isPrimitiveType() && argument.type.isSubtypeOfClass(type.erasedUpperBound.symbol) ->
argument.type.isInlineClassType() && argument.type.isSubtypeOfClass(type.erasedUpperBound.symbol) ->
argument
else ->
builder.irAs(argument, type)
@@ -0,0 +1,9 @@
abstract class Base {
private fun test(): String = "OK"
fun test(d: Derived): String = (d as Base).test()
}
class Derived : Base()
fun box(): String = Derived().test(Derived())
@@ -0,0 +1,14 @@
inline class X(val x: Any)
interface IFoo<T> {
fun foo(): T
}
class TestX : IFoo<X> {
override fun foo(): X = X("OK")
}
fun box(): String {
val t: IFoo<X> = TestX()
return ((t.foo() as Any) as X).x.toString()
}
@@ -4268,6 +4268,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@Test
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
@@ -19773,6 +19779,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@Test
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@Test
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
@@ -4340,6 +4340,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@Test
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
@@ -19905,6 +19911,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@Test
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@Test
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
@@ -3729,6 +3729,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
runTest("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt");
@@ -16415,6 +16420,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/unrelatedGenerics.kt");
@@ -2844,6 +2844,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
runTest("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt");
@@ -14399,6 +14404,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/unrelatedGenerics.kt");
@@ -2844,6 +2844,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
runTest("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt");
@@ -13805,6 +13810,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/unrelatedGenerics.kt");
@@ -2809,6 +2809,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
runTest("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt");
@@ -13835,6 +13840,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/unrelatedGenerics.kt");
@@ -2349,6 +2349,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/casts/kt22714.kt");
}
@TestMetadata("kt48927_privateMethodOnDerivedCastToBase.kt")
public void testKt48927_privateMethodOnDerivedCastToBase() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt48927_privateMethodOnDerivedCastToBase.kt");
}
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
runTest("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt");
@@ -8684,6 +8689,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/relatedReturnTypes2b.kt");
}
@TestMetadata("uncastInlineClassToAnyAndBack.kt")
public void testUncastInlineClassToAnyAndBack() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/uncastInlineClassToAnyAndBack.kt");
}
@TestMetadata("unrelatedGenerics.kt")
public void testUnrelatedGenerics() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxReturnValueOnOverride/unrelatedGenerics.kt");