From 6cf76e1d07a9ecdc33b57bc0804bc872db7924a1 Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Wed, 1 Nov 2023 13:53:07 -0500 Subject: [PATCH] [FIR] Report no return diagnostic on functions returning Nothing Previously, the NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY diagnostic was not reported at all on functions which return Nothing. However, in K1, if the function returns Nothing but the body is empty or doesn't contain a Nothing expression, this diagnostic would be reported. The checker in K2 has been updated to report this diagnostic on functions which return Nothing as well. ^KT-55965 Fixed --- .../analysis/checkers/declaration/FirFunctionReturnChecker.kt | 2 +- compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt | 2 ++ compiler/testData/diagnostics/tests/FunctionReturnTypes.kt | 2 ++ compiler/testData/diagnostics/tests/FunctionReturnTypes.txt | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirFunctionReturnChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirFunctionReturnChecker.kt index 374471da3d3..dbd34b66f7b 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirFunctionReturnChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirFunctionReturnChecker.kt @@ -35,7 +35,7 @@ object FirFunctionReturnChecker : FirFunctionChecker() { if (declaration is FirConstructor) return if (declaration is FirAnonymousFunction && declaration.isLambda) return val returnType = declaration.returnTypeRef.coneType.fullyExpandedType(context.session) - if (returnType.isUnit || returnType.isNothing) return + if (returnType.isUnit) return val graph = declaration.controlFlowGraphReference?.controlFlowGraph ?: return val blockExitNode = graph.exitNode.previousNodes.lastOrNull { it is BlockExitNode } ?: return diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt index bc08f5224ee..783b1ace38d 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt @@ -158,6 +158,8 @@ fun illegalReturnIf(): Char { fun returnNothing(): Nothing { throw 1 } +fun returnNothingEmpty(): Nothing { +} fun f(): Int { if (1 < 2) { return 1 } else returnNothing() } diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt index 3b6a8a3cf07..f548c71a02c 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt @@ -158,6 +158,8 @@ fun illegalReturnIf(): Char { fun returnNothing(): Nothing { throw 1 } +fun returnNothingEmpty(): Nothing { +} fun f(): Int { if (1 < 2) { return 1 } else returnNothing() } diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.txt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.txt index 5af51a4886b..85499c952d7 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.txt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.txt @@ -47,6 +47,7 @@ public fun nonBlockNoReturnIfUnitInOneBranch(): kotlin.Int public fun nonBlockReturnIfEmptyIf(): kotlin.Int public fun none(): kotlin.Unit public fun returnNothing(): kotlin.Nothing +public fun returnNothingEmpty(): kotlin.Nothing public fun test1(): kotlin.Any public fun test2(): kotlin.Any public fun test3(): kotlin.Any