diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt index 8ce55e134ee..c96687ccea6 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt @@ -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 diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index 702259e0348..daa01245dea 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -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( diff --git a/compiler/testData/ir/irText/expressions/kt30796.fir.ir.txt b/compiler/testData/ir/irText/expressions/kt30796.fir.ir.txt index 90ad1df9efc..11086c5321f 100644 --- a/compiler/testData/ir/irText/expressions/kt30796.fir.ir.txt +++ b/compiler/testData/ir/irText/expressions/kt30796.fir.ir.txt @@ -2,9 +2,8 @@ FILE fqName: fileName:/kt30796.kt FUN name:magic visibility:public modality:FINAL () returnType:T of .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 of .magic declared in ' - THROW type=kotlin.Nothing - CONSTRUCTOR_CALL 'public constructor () declared in java.lang.Exception' type=java.lang.Exception origin=null + THROW type=kotlin.Nothing + CONSTRUCTOR_CALL 'public constructor () declared in java.lang.Exception' type=java.lang.Exception origin=null FUN name:test visibility:public modality:FINAL (value:T of .test, value2:T of .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 .test diff --git a/compiler/testData/ir/irText/expressions/kt30796.fir.kt.txt b/compiler/testData/ir/irText/expressions/kt30796.fir.kt.txt index 182cb9b15a4..78a860220e9 100644 --- a/compiler/testData/ir/irText/expressions/kt30796.fir.kt.txt +++ b/compiler/testData/ir/irText/expressions/kt30796.fir.kt.txt @@ -1,5 +1,5 @@ fun magic(): T { - return throw Exception() + throw Exception() } fun test(value: T, value2: T) { diff --git a/compiler/testData/ir/irText/firProblems/ArrayMap.fir.ir.txt b/compiler/testData/ir/irText/firProblems/ArrayMap.fir.ir.txt index 6df82b2389f..b45f0d0f679 100644 --- a/compiler/testData/ir/irText/firProblems/ArrayMap.fir.ir.txt +++ b/compiler/testData/ir/irText/firProblems/ArrayMap.fir.ir.txt @@ -124,9 +124,8 @@ FILE fqName: fileName:/ArrayMap.kt public abstract fun next (): T of kotlin.collections.Iterator declared in kotlin.collections.Iterator $this: VALUE_PARAMETER name: type:.EmptyArrayMap.iterator. BLOCK_BODY - RETURN type=kotlin.Nothing from='public open fun next (): kotlin.Nothing declared in .EmptyArrayMap.iterator.' - THROW type=kotlin.Nothing - CONSTRUCTOR_CALL 'public constructor () declared in java.util.NoSuchElementException' type=java.util.NoSuchElementException origin=null + THROW type=kotlin.Nothing + CONSTRUCTOR_CALL 'public constructor () 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 diff --git a/compiler/testData/ir/irText/firProblems/ArrayMap.fir.kt.txt b/compiler/testData/ir/irText/firProblems/ArrayMap.fir.kt.txt index 429c509dba5..0fbfec89089 100644 --- a/compiler/testData/ir/irText/firProblems/ArrayMap.fir.kt.txt +++ b/compiler/testData/ir/irText/firProblems/ArrayMap.fir.kt.txt @@ -62,7 +62,7 @@ internal object EmptyArrayMap : ArrayMap { } override operator fun next(): Nothing { - return throw NoSuchElementException() + throw NoSuchElementException() } } @@ -328,4 +328,3 @@ internal class ArrayMapImpl : ArrayMap { } } - diff --git a/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.ir.txt b/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.ir.txt index 423b68d0039..61bd4d5a6d6 100644 --- a/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.ir.txt +++ b/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.ir.txt @@ -94,9 +94,8 @@ FILE fqName: fileName:/typeAliasWithUnsafeVariance.kt CONST Boolean type=kotlin.Boolean value=true FUN name:getTag visibility:public modality:FINAL <> () returnType:.Tag<*> BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun getTag (): .Tag<*> declared in ' - THROW type=kotlin.Nothing - CONSTRUCTOR_CALL 'public constructor () declared in java.lang.Exception' type=java.lang.Exception origin=null + THROW type=kotlin.Nothing + CONSTRUCTOR_CALL 'public constructor () 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 diff --git a/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.kt.txt b/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.kt.txt index 4c5ff34e459..0f4d65c2bd7 100644 --- a/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.kt.txt +++ b/compiler/testData/ir/irText/types/typeAliasWithUnsafeVariance.fir.kt.txt @@ -43,10 +43,9 @@ data class Tag { } fun getTag(): Tag<*> { - return throw Exception() + throw Exception() } fun doAction() { getTag().() /*~> Unit */ } -