FIR2IR: don't generate unnecessary 'return throw'

#KT-60245 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-08-18 17:16:46 +02:00
committed by Space Team
parent 90904e4f8a
commit 7d5e2f85cc
8 changed files with 15 additions and 16 deletions
@@ -190,7 +190,7 @@ class Fir2IrImplicitCastInserter(
(data as? IrContainerExpression)?.insertImplicitCasts() ?: data
override fun visitReturnExpression(returnExpression: FirReturnExpression, data: IrElement): IrElement {
val irReturn = data as IrReturn
val irReturn = data as? IrReturn ?: return data
val expectedType = returnExpression.target.labeledElement.returnTypeRef
irReturn.value = irReturn.value.cast(returnExpression.result, returnExpression.result.typeRef, expectedType)
return data
@@ -498,9 +498,13 @@ class Fir2IrVisitor(
// ==================================================================================
override fun visitReturnExpression(returnExpression: FirReturnExpression, data: Any?): IrElement {
val result = returnExpression.result
if (result is FirThrowExpression) {
// Note: in FIR we must have 'return' as the last statement
return convertToIrExpression(result)
}
val irTarget = conversionScope.returnTarget(returnExpression, declarationStorage)
return returnExpression.convertWithOffsets { startOffset, endOffset ->
val result = returnExpression.result
// For implicit returns, use the expression endOffset to generate the expected line number for debugging.
val returnStartOffset = if (returnExpression.source?.kind is KtFakeSourceElementKind.ImplicitReturn) endOffset else startOffset
IrReturnImpl(
+2 -3
View File
@@ -2,9 +2,8 @@ FILE fqName:<root> fileName:/kt30796.kt
FUN name:magic visibility:public modality:FINAL <T> () returnType:T of <root>.magic
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun magic <T> (): T of <root>.magic declared in <root>'
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.Exception' type=java.lang.Exception origin=null
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.Exception' type=java.lang.Exception origin=null
FUN name:test visibility:public modality:FINAL <T> (value:T of <root>.test, value2:T of <root>.test) returnType:kotlin.Unit
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
VALUE_PARAMETER name:value index:0 type:T of <root>.test
+1 -1
View File
@@ -1,5 +1,5 @@
fun <T : Any?> magic(): T {
return throw Exception()
throw Exception()
}
fun <T : Any?> test(value: T, value2: T) {
@@ -124,9 +124,8 @@ FILE fqName:<root> fileName:/ArrayMap.kt
public abstract fun next (): T of kotlin.collections.Iterator declared in kotlin.collections.Iterator
$this: VALUE_PARAMETER name:<this> type:<root>.EmptyArrayMap.iterator.<no name provided>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public open fun next (): kotlin.Nothing declared in <root>.EmptyArrayMap.iterator.<no name provided>'
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.util.NoSuchElementException' type=java.util.NoSuchElementException origin=null
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.util.NoSuchElementException' type=java.util.NoSuchElementException origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.collections.Iterator
@@ -62,7 +62,7 @@ internal object EmptyArrayMap : ArrayMap<Nothing> {
}
override operator fun next(): Nothing {
return throw NoSuchElementException()
throw NoSuchElementException()
}
}
@@ -328,4 +328,3 @@ internal class ArrayMapImpl<T : Any> : ArrayMap<T> {
}
}
@@ -94,9 +94,8 @@ FILE fqName:<root> fileName:/typeAliasWithUnsafeVariance.kt
CONST Boolean type=kotlin.Boolean value=true
FUN name:getTag visibility:public modality:FINAL <> () returnType:<root>.Tag<*>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun getTag (): <root>.Tag<*> declared in <root>'
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.Exception' type=java.lang.Exception origin=null
THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in java.lang.Exception' type=java.lang.Exception origin=null
FUN name:doAction visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
@@ -43,10 +43,9 @@ data class Tag<out RenderingT : Any?> {
}
fun getTag(): Tag<*> {
return throw Exception()
throw Exception()
}
fun doAction() {
getTag().<get-action>() /*~> Unit */
}