[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
This commit is contained in:
Brian Norman
2023-11-01 13:53:07 -05:00
committed by Space Team
parent 3fb415d966
commit 6cf76e1d07
4 changed files with 6 additions and 1 deletions
@@ -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
@@ -158,6 +158,8 @@ fun illegalReturnIf(): Char {
fun returnNothing(): Nothing {
throw <!TYPE_MISMATCH!>1<!>
}
fun returnNothingEmpty(): Nothing {
<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
fun f(): Int {
if (1 < 2) { return 1 } else returnNothing()
}
@@ -158,6 +158,8 @@ fun illegalReturnIf(): Char {
fun returnNothing(): Nothing {
throw <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>
}
fun returnNothingEmpty(): Nothing {
<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
fun f(): Int {
if (1 < 2) { return 1 } else returnNothing()
}
@@ -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