JVM IR: fix smart cast on argument of 'throw'

#KT-48163 Fixed
This commit is contained in:
Alexander Udalov
2021-08-09 16:21:33 +02:00
parent cb37a05b79
commit afacff326d
9 changed files with 67 additions and 5 deletions
@@ -40424,6 +40424,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/smartCasts/kt44942.kt");
}
@Test
@TestMetadata("kt48163_smartCastToThrowable.kt")
public void testKt48163_smartCastToThrowable() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/kt48163_smartCastToThrowable.kt");
}
@Test
@TestMetadata("lambdaArgumentWithoutType.kt")
public void testLambdaArgumentWithoutType() throws Exception {
@@ -91,6 +91,12 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/charConstant.kt");
}
@Test
@TestMetadata("checkcastOnThrow.kt")
public void testCheckcastOnThrow() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/checkcastOnThrow.kt");
}
@Test
@TestMetadata("collectionStubs.kt")
public void testCollectionStubs() throws Exception {
@@ -1327,12 +1327,11 @@ class ExpressionCodegen(
override fun visitThrow(expression: IrThrow, data: BlockInfo): PromisedValue {
expression.markLineNumber(startOffset = true)
val exception = expression.value.accept(this, data)
// Avoid unecessary CHECKCASTs to java/lang/Throwable. If the exception is not of type Object
// then it must be some subtype of throwable and we don't need to coerce it.
if (exception.type == OBJECT_TYPE)
exception.materializeAt(context.irBuiltIns.throwableType)
else
// Avoid unnecessary CHECKCASTs to java/lang/Throwable.
if (exception.irType.isSubtypeOfClass(context.irBuiltIns.throwableClass))
exception.materialize()
else
exception.materializeAt(context.irBuiltIns.throwableType)
mv.athrow()
return unitValue
}
@@ -0,0 +1,19 @@
// TARGET_BACKEND: JVM
import java.io.Serializable
fun bar(s: Serializable) {
when (s) {
is Exception -> throw s
else -> Unit
}
}
fun box(): String {
try {
bar(Exception("OK"))
return "Fail"
} catch (e: Exception) {
return e.message!!
}
}
@@ -0,0 +1,9 @@
// TARGET_BACKEND: JVM_IR
fun f(x: Throwable) { throw x }
fun f(x: Exception) { throw x }
fun f(x: RuntimeException) { throw x }
fun f(x: Error) { throw x }
fun f(x: AssertionError) { throw x }
// 0 CHECKCAST
@@ -40262,6 +40262,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/smartCasts/kt44942.kt");
}
@Test
@TestMetadata("kt48163_smartCastToThrowable.kt")
public void testKt48163_smartCastToThrowable() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/kt48163_smartCastToThrowable.kt");
}
@Test
@TestMetadata("lambdaArgumentWithoutType.kt")
public void testLambdaArgumentWithoutType() throws Exception {
@@ -40424,6 +40424,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/smartCasts/kt44942.kt");
}
@Test
@TestMetadata("kt48163_smartCastToThrowable.kt")
public void testKt48163_smartCastToThrowable() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/kt48163_smartCastToThrowable.kt");
}
@Test
@TestMetadata("lambdaArgumentWithoutType.kt")
public void testLambdaArgumentWithoutType() throws Exception {
@@ -91,6 +91,12 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/charConstant.kt");
}
@Test
@TestMetadata("checkcastOnThrow.kt")
public void testCheckcastOnThrow() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/checkcastOnThrow.kt");
}
@Test
@TestMetadata("collectionStubs.kt")
public void testCollectionStubs() throws Exception {
@@ -32290,6 +32290,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/smartCasts/kt44942.kt");
}
@TestMetadata("kt48163_smartCastToThrowable.kt")
public void testKt48163_smartCastToThrowable() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/kt48163_smartCastToThrowable.kt");
}
@TestMetadata("lambdaArgumentWithoutType.kt")
public void testLambdaArgumentWithoutType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/lambdaArgumentWithoutType.kt");