diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 5a6dc1c20b2..790bc7fdbe6 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -15843,6 +15843,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/inline/inlineReified.kt"); } + @Test + @TestMetadata("inlinedReturnInBranch.kt") + public void testInlinedReturnInBranch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.kt"); + } + @Test @TestMetadata("invoke.kt") public void testInvoke() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 3f0b644886a..611cd595157 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -15843,6 +15843,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inline/inlineReified.kt"); } + @Test + @TestMetadata("inlinedReturnInBranch.kt") + public void testInlinedReturnInBranch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.kt"); + } + @Test @TestMetadata("invoke.kt") public void testInvoke() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index f5e3d14dd92..3e0a4e96737 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -15843,6 +15843,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/inline/inlineReified.kt"); } + @Test + @TestMetadata("inlinedReturnInBranch.kt") + public void testInlinedReturnInBranch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.kt"); + } + @Test @TestMetadata("invoke.kt") public void testInvoke() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt index 77cd7aba698..e5859663db3 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt @@ -641,6 +641,7 @@ class ControlFlowGraphBuilder { addNewSimpleNode(node) whenExitNodes.push(createWhenExitNode(whenExpression)) whenBranchIndices.push(whenExpression.branches.mapIndexed { index, branch -> branch to index }.toMap()) + notCompletedFunctionCalls.push(mutableListOf()) levelCounter++ return node } @@ -676,6 +677,7 @@ class ControlFlowGraphBuilder { val whenExitNode = whenExitNodes.pop() // exit from last condition node still on stack // we should remove it + notCompletedFunctionCalls.pop().forEach(::completeFunctionCall) val lastWhenConditionExit = lastNodes.pop() val syntheticElseBranchNode = if (!whenExpression.isProperlyExhaustive) { createWhenSyntheticElseBranchNode(whenExpression).apply { @@ -985,7 +987,7 @@ class ControlFlowGraphBuilder { catchNodeStorages.pop() val catchExitNodes = catchExitNodeStorages.pop() val tryMainExitNode = tryMainExitNodes.pop() - + notCompletedFunctionCalls.pop().forEach(::completeFunctionCall) val node = tryExitNodes.pop() @@ -1396,14 +1398,15 @@ class ControlFlowGraphBuilder { tryExitNodes.top().fir.finallyBlock == null -> { // (3)... without finally ...(4) // Either in try-main or catch. - if (tryMainExitNodes.top().followingNodes.isNotEmpty()) { + val tryMainExitNode = tryMainExitNodes.top() + if (tryMainExitNode.followingNodes.isNotEmpty()) { // (4)... in catch, i.e., re-throw. exitTargetsForTry.top() } else { // (4)... in try-main. Route to exit of try main block. // We already have edges from the exit of try main block to each catch clause. // This edge makes the remaining part of try main block, e.g., following `when` branches, marked as dead. - tryMainExitNodes.top() + tryMainExitNode } } // (3)... within finally. diff --git a/compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.kt b/compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.kt new file mode 100644 index 00000000000..e1374ee10ae --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.kt @@ -0,0 +1,20 @@ +// FIR_IDENTICAL +// https://youtrack.jetbrains.com/issue/KT-49289 + +inline fun myRun(f: () -> T): T = f() + +fun foo(): Int { + myRun { + return 24 + } +} + +fun bar(arg: Boolean): Int { + if (arg) { + return 42 + } else { + myRun { + return 24 + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.txt b/compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.txt new file mode 100644 index 00000000000..dc085899173 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.txt @@ -0,0 +1,5 @@ +package + +public fun bar(/*0*/ arg: kotlin.Boolean): kotlin.Int +public fun foo(): kotlin.Int +public inline fun myRun(/*0*/ f: () -> T): T diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index d7eeb05224e..4b33f3ab1cf 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -15849,6 +15849,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inline/inlineReified.kt"); } + @Test + @TestMetadata("inlinedReturnInBranch.kt") + public void testInlinedReturnInBranch() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/inlinedReturnInBranch.kt"); + } + @Test @TestMetadata("invoke.kt") public void testInvoke() throws Exception {