FIR2IR: Fix line number differences in try block

This commit is contained in:
Mads Ager
2021-10-28 10:28:54 +03:00
committed by Mikhail Glukhikh
parent 5aa8e2d0d1
commit e5bff514b6
11 changed files with 69 additions and 31 deletions
@@ -948,13 +948,14 @@ class Fir2IrVisitor(
}
override fun visitTryExpression(tryExpression: FirTryExpression, data: Any?): IrElement {
// Always generate a block for try, catch and finally blocks. When leaving the finally block in the debugger
// for both Java and Kotlin there is a step on the end brace. For that to happen we need the block with
// that line number for the finally block.
return tryExpression.convertWithOffsets { startOffset, endOffset ->
IrTryImpl(
startOffset, endOffset, tryExpression.typeRef.toIrType(),
tryExpression.tryBlock.convertToIrExpressionOrBlock(),
tryExpression.tryBlock.convertToIrBlock(),
tryExpression.catches.map { it.accept(this, data) as IrCatch },
// Always generate a block for the finally block. When leaving the finally block in the debugger for both
// Java and Kotlin there is a step on the end brace. For that to happen we need the block with that line number.
tryExpression.finallyBlock?.convertToIrBlock()
)
}
@@ -963,7 +964,7 @@ class Fir2IrVisitor(
override fun visitCatch(catch: FirCatch, data: Any?): IrElement {
return catch.convertWithOffsets { startOffset, endOffset ->
val catchParameter = declarationStorage.createIrVariable(catch.parameter, conversionScope.parentFromStack())
IrCatchImpl(startOffset, endOffset, catchParameter, catch.block.convertToIrExpressionOrBlock())
IrCatchImpl(startOffset, endOffset, catchParameter, catch.block.convertToIrBlock())
}
}
@@ -25,7 +25,8 @@ FILE fqName:<root> fileName:/useNextParamInLambda.kt
CONST String type=kotlin.String value="fail"
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
TRY type=kotlin.Any
try: CALL 'public final fun f (f1: kotlin.Function0<kotlin.String>, f2: kotlin.Function0<kotlin.String>): kotlin.String declared in <root>' type=kotlin.String origin=null
try: BLOCK type=kotlin.String origin=null
CALL 'public final fun f (f1: kotlin.Function0<kotlin.String>, f2: kotlin.Function0<kotlin.String>): kotlin.String declared in <root>' type=kotlin.String origin=null
CATCH parameter=val e: java.lang.Exception [val] declared in <root>.box
VAR name:e type:java.lang.Exception [val]
BLOCK type=kotlin.Unit origin=null
@@ -10,7 +10,9 @@ fun f(f1: Function0<String> = local fun <anonymous>(): String {
fun box(): String {
var result: String = "fail"
try f()
try { // BLOCK
f()
}
catch (e: Exception){ // BLOCK
result = "OK"
}
@@ -23,3 +25,4 @@ fun box(): String {
}
))
}
@@ -4,9 +4,11 @@ FILE fqName:<root> fileName:/catchParameterAccess.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test (f: kotlin.Function0<kotlin.Unit>): kotlin.Unit declared in <root>'
TRY type=kotlin.Unit
try: CALL 'public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'f: kotlin.Function0<kotlin.Unit> declared in <root>.test' type=kotlin.Function0<kotlin.Unit> origin=VARIABLE_AS_FUNCTION
try: BLOCK type=kotlin.Unit origin=null
CALL 'public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'f: kotlin.Function0<kotlin.Unit> declared in <root>.test' type=kotlin.Function0<kotlin.Unit> origin=VARIABLE_AS_FUNCTION
CATCH parameter=val e: java.lang.Exception [val] declared in <root>.test
VAR name:e type:java.lang.Exception [val]
THROW type=kotlin.Nothing
GET_VAR 'val e: java.lang.Exception [val] declared in <root>.test' type=java.lang.Exception origin=null
BLOCK type=kotlin.Nothing origin=null
THROW type=kotlin.Nothing
GET_VAR 'val e: java.lang.Exception [val] declared in <root>.test' type=java.lang.Exception origin=null
@@ -1,5 +1,10 @@
fun test(f: Function0<Unit>) {
return try f.invoke()
catch (e: Exception)throw e
return try { // BLOCK
f.invoke()
}
catch (e: Exception){ // BLOCK
throw e
}
}
+10 -6
View File
@@ -9,11 +9,13 @@ FILE fqName:<root> fileName:/kt48806.kt
FIELD PROPERTY_BACKING_FIELD name:test_1 type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
TRY type=kotlin.Int
try: THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.RuntimeException' type=java.lang.RuntimeException origin=null
try: BLOCK type=kotlin.Nothing origin=null
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.RuntimeException' type=java.lang.RuntimeException origin=null
CATCH parameter=val e: java.lang.Exception [val] declared in <root>.A.test_1
VAR name:e type:java.lang.Exception [val]
CONST Int type=kotlin.Int value=1
BLOCK type=kotlin.Int origin=null
CONST Int type=kotlin.Int value=1
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test_1> visibility:public modality:FINAL <> ($this:<root>.A) returnType:kotlin.Int
correspondingProperty: PROPERTY name:test_1 visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.A
@@ -25,11 +27,13 @@ FILE fqName:<root> fileName:/kt48806.kt
FIELD PROPERTY_BACKING_FIELD name:test_2 type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
TRY type=kotlin.Int
try: CONST Int type=kotlin.Int value=1
try: BLOCK type=kotlin.Int origin=null
CONST Int type=kotlin.Int value=1
CATCH parameter=val e: java.lang.Exception [val] declared in <root>.A.test_2
VAR name:e type:java.lang.Exception [val]
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.RuntimeException' type=java.lang.RuntimeException origin=null
BLOCK type=kotlin.Nothing origin=null
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.RuntimeException' type=java.lang.RuntimeException origin=null
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test_2> visibility:public modality:FINAL <> ($this:<root>.A) returnType:kotlin.Int
correspondingProperty: PROPERTY name:test_2 visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.A
+13 -4
View File
@@ -6,15 +6,24 @@ class A {
}
val test_1: Int
field = try throw RuntimeException()
catch (e: Exception)1
field = try { // BLOCK
throw RuntimeException()
}
catch (e: Exception){ // BLOCK
1
}
get
val test_2: Int
field = try 1
catch (e: Exception)throw RuntimeException()
field = try { // BLOCK
1
}
catch (e: Exception){ // BLOCK
throw RuntimeException()
}
get
}
@@ -2,10 +2,12 @@ FILE fqName:<root> fileName:/tryCatch.kt
FUN name:test1 visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
TRY type=kotlin.Unit
try: CALL 'public final fun println (): kotlin.Unit [inline] declared in kotlin.io.ConsoleKt' type=kotlin.Unit origin=null
try: BLOCK type=kotlin.Unit origin=null
CALL 'public final fun println (): kotlin.Unit [inline] declared in kotlin.io.ConsoleKt' type=kotlin.Unit origin=null
CATCH parameter=val e: kotlin.Throwable [val] declared in <root>.test1
VAR name:e type:kotlin.Throwable [val]
CALL 'public final fun println (): kotlin.Unit [inline] declared in kotlin.io.ConsoleKt' type=kotlin.Unit origin=null
BLOCK type=kotlin.Unit origin=null
CALL 'public final fun println (): kotlin.Unit [inline] declared in kotlin.io.ConsoleKt' type=kotlin.Unit origin=null
finally: BLOCK type=kotlin.Unit origin=null
CALL 'public final fun println (): kotlin.Unit [inline] declared in kotlin.io.ConsoleKt' type=kotlin.Unit origin=null
FUN name:test2 visibility:public modality:FINAL <> () returnType:kotlin.Int
@@ -1,6 +1,10 @@
fun test1() {
try println()
catch (e: Throwable)println()
try { // BLOCK
println()
}
catch (e: Throwable){ // BLOCK
println()
}
finally { // BLOCK
println()
}
@@ -10,8 +10,10 @@ FILE fqName:<root> fileName:/tryCatchWithImplicitCast.kt
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
VAR name:t type:kotlin.String [val]
TRY type=kotlin.String
try: TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String
GET_VAR 'a: kotlin.Any declared in <root>.testImplicitCast' type=kotlin.Any origin=null
try: BLOCK type=kotlin.String origin=null
TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String
GET_VAR 'a: kotlin.Any declared in <root>.testImplicitCast' type=kotlin.Any origin=null
CATCH parameter=val e: kotlin.Throwable [val] declared in <root>.testImplicitCast
VAR name:e type:kotlin.Throwable [val]
CONST String type=kotlin.String value=""
BLOCK type=kotlin.String origin=null
CONST String type=kotlin.String value=""
@@ -2,7 +2,12 @@ fun testImplicitCast(a: Any) {
when {
a !is String -> return Unit
}
val t: String = try a /*as String */
catch (e: Throwable)""
val t: String = try { // BLOCK
a /*as String */
}
catch (e: Throwable){ // BLOCK
""
}
}