diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirBodyResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirBodyResolveTransformer.kt index 063555e61c0..b8cf503079b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirBodyResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirBodyResolveTransformer.kt @@ -484,6 +484,22 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn return functionCall } + override fun transformTryExpression(tryExpression: FirTryExpression, data: Any?): CompositeTransformResult { + val tryExpression = tryExpression.transformChildren(this, data) as FirTryExpression + if (tryExpression.resultType !is FirResolvedTypeRef) { + val type = commonSuperType((listOf(tryExpression.tryBlock) + tryExpression.catches.map { it.block }).mapNotNull { + val expression = it.statements.lastOrNull() as? FirExpression + if (expression != null) { + (expression.resultType as? FirResolvedTypeRef) ?: FirErrorTypeRefImpl(session, null, "No type for when branch result") + } else { + FirImplicitUnitTypeRef(session, null) + } + }) + if (type != null) tryExpression.resultType = type + } + return tryExpression.compose() + } + override fun transformFunctionCall(functionCall: FirFunctionCall, data: Any?): CompositeTransformResult { if (functionCall.calleeReference !is FirSimpleNamedReference) return functionCall.compose() val expectedTypeRef = data as FirTypeRef? diff --git a/compiler/testData/ir/irText/declarations/catchParameterInTopLevelProperty.fir.txt b/compiler/testData/ir/irText/declarations/catchParameterInTopLevelProperty.fir.txt index b8375ce27a9..ed4ab527095 100644 --- a/compiler/testData/ir/irText/declarations/catchParameterInTopLevelProperty.fir.txt +++ b/compiler/testData/ir/irText/declarations/catchParameterInTopLevelProperty.fir.txt @@ -1,15 +1,14 @@ FILE fqName: fileName:/catchParameterInTopLevelProperty.kt PROPERTY name:test visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:test type:IrErrorType visibility:public [final,static] + FIELD PROPERTY_BACKING_FIELD name:test type:kotlin.Unit visibility:public [final,static] EXPRESSION_BODY - TRY type=IrErrorType + TRY type=kotlin.Unit try: BLOCK type=kotlin.Unit origin=null CATCH parameter=val e: kotlin.Throwable [val] declared in .test VAR name:e type:kotlin.Throwable [val] BLOCK type=kotlin.Unit origin=null - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:IrErrorType + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.Unit correspondingProperty: PROPERTY name:test visibility:public modality:FINAL [val] BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): IrErrorType declared in ' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test type:IrErrorType visibility:public [final,static] ' type=IrErrorType origin=null - + RETURN type=kotlin.Nothing from='public final fun (): kotlin.Unit declared in ' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test type:kotlin.Unit visibility:public [final,static] ' type=kotlin.Unit origin=null diff --git a/compiler/testData/ir/irText/expressions/catchParameterAccess.fir.txt b/compiler/testData/ir/irText/expressions/catchParameterAccess.fir.txt index a8198d14008..eda974e97c6 100644 --- a/compiler/testData/ir/irText/expressions/catchParameterAccess.fir.txt +++ b/compiler/testData/ir/irText/expressions/catchParameterAccess.fir.txt @@ -1,9 +1,9 @@ FILE fqName: fileName:/catchParameterAccess.kt - FUN name:test visibility:public modality:FINAL <> (f:kotlin.Function0) returnType:IrErrorType + FUN name:test visibility:public modality:FINAL <> (f:kotlin.Function0) returnType:kotlin.Nothing? VALUE_PARAMETER name:f index:0 type:kotlin.Function0 BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun test (f: kotlin.Function0): IrErrorType declared in ' - TRY type=IrErrorType + RETURN type=kotlin.Nothing from='public final fun test (f: kotlin.Function0): kotlin.Nothing? declared in ' + TRY type=kotlin.Nothing? try: ERROR_CALL 'Unresolved reference: #' type=IrErrorType CATCH parameter=val e: java.lang.Exception [val] declared in .test VAR name:e type:java.lang.Exception [val] diff --git a/compiler/testData/ir/irText/expressions/tryCatch.fir.txt b/compiler/testData/ir/irText/expressions/tryCatch.fir.txt index ffbe1959602..ab2d3d1f1e1 100644 --- a/compiler/testData/ir/irText/expressions/tryCatch.fir.txt +++ b/compiler/testData/ir/irText/expressions/tryCatch.fir.txt @@ -1,7 +1,7 @@ FILE fqName: fileName:/tryCatch.kt FUN name:test1 visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY - TRY type=IrErrorType + TRY type=kotlin.Unit try: CALL 'public final fun println (): kotlin.Unit [inline] declared in kotlin.io' type=kotlin.Unit origin=null CATCH parameter=val e: kotlin.Throwable [val] declared in .test1 VAR name:e type:kotlin.Throwable [val] @@ -10,7 +10,7 @@ FILE fqName: fileName:/tryCatch.kt FUN name:test2 visibility:public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun test2 (): kotlin.Int declared in ' - TRY type=IrErrorType + TRY type=kotlin.Int try: BLOCK type=kotlin.Int origin=null CALL 'public final fun println (): kotlin.Unit [inline] declared in kotlin.io' type=kotlin.Unit origin=null CONST Int type=kotlin.Int value=42 diff --git a/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.fir.txt b/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.fir.txt index 4a12f28dab9..7e6fae1f72b 100644 --- a/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.fir.txt +++ b/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.fir.txt @@ -9,7 +9,7 @@ FILE fqName: fileName:/tryCatchWithImplicitCast.kt then: RETURN type=kotlin.Nothing from='public final fun testImplicitCast (a: kotlin.Any): kotlin.Unit declared in ' 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=IrErrorType + TRY type=kotlin.String try: GET_VAR 'a: kotlin.Any declared in .testImplicitCast' type=kotlin.Any origin=null CATCH parameter=val e: kotlin.Throwable [val] declared in .testImplicitCast VAR name:e type:kotlin.Throwable [val]