diff --git a/compiler/fir/analysis-tests/testData/resolve/cfg/propertiesAndInitBlocks.dot b/compiler/fir/analysis-tests/testData/resolve/cfg/propertiesAndInitBlocks.dot index 0a055a69561..d0a556432aa 100644 --- a/compiler/fir/analysis-tests/testData/resolve/cfg/propertiesAndInitBlocks.dot +++ b/compiler/fir/analysis-tests/testData/resolve/cfg/propertiesAndInitBlocks.dot @@ -283,25 +283,25 @@ digraph propertiesAndInitBlocks_kt { } subgraph cluster_29 { color=blue - 91 [label="Enter finally"]; + 91 [label="Catch enter"]; subgraph cluster_30 { color=blue 92 [label="Enter block"]; - 93 [label="Const: Int(0)"]; + 93 [label="Const: Int(2)"]; 94 [label="Exit block"]; } - 95 [label="Exit finally"]; + 95 [label="Catch exit"]; } subgraph cluster_31 { color=blue - 96 [label="Catch enter"]; + 96 [label="Enter finally"]; subgraph cluster_32 { color=blue 97 [label="Enter block"]; - 98 [label="Const: Int(2)"]; + 98 [label="Const: Int(0)"]; 99 [label="Exit block"]; } - 100 [label="Catch exit"]; + 100 [label="Exit finally"]; } 101 [label="Try expression exit"]; } @@ -309,17 +309,17 @@ digraph propertiesAndInitBlocks_kt { } 84 -> {85}; 85 -> {86}; - 86 -> {102 96 91 87}; + 86 -> {102 91 96 87}; 87 -> {88}; 88 -> {89}; 89 -> {90}; - 90 -> {101}; - 91 -> {92}; + 90 -> {96}; + 91 -> {102 92}; 92 -> {93}; 93 -> {94}; 94 -> {95}; - 95 -> {101}; - 96 -> {102 97}; + 95 -> {96}; + 96 -> {97}; 97 -> {98}; 98 -> {99}; 99 -> {100}; diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/smartcasts/tryWithLambdaInside.dot b/compiler/fir/analysis-tests/testData/resolveWithStdlib/smartcasts/tryWithLambdaInside.dot index 3af3e0d50fb..f342deb184a 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/smartcasts/tryWithLambdaInside.dot +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/smartcasts/tryWithLambdaInside.dot @@ -110,7 +110,7 @@ finally { 18 -> {19}; 19 -> {20}; 20 -> {21}; - 21 -> {26}; + 21 -> {22}; 22 -> {23}; 23 -> {24}; 24 -> {25}; @@ -198,7 +198,7 @@ finally { 43 -> {44}; 44 -> {45}; 45 -> {46}; - 46 -> {51}; + 46 -> {47}; 47 -> {48}; 48 -> {49}; 49 -> {50}; 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 6d1f6f6647e..ae47650e47f 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 @@ -804,7 +804,13 @@ class ControlFlowGraphBuilder { levelCounter-- val node = createTryMainBlockExitNode(tryExpression) popAndAddEdge(node) - addEdge(node, tryExitNodes.top()) + val finallyEnterNode = finallyEnterNodes.topOrNull() + // NB: Check the level to avoid adding an edge to the finally block at an upper level. + if (finallyEnterNode != null && finallyEnterNode.level == levelCounter + 1) { + addEdge(node, finallyEnterNode) + } else { + addEdge(node, tryExitNodes.top()) + } return node } @@ -816,7 +822,13 @@ class ControlFlowGraphBuilder { levelCounter-- return createCatchClauseExitNode(catch).also { popAndAddEdge(it) - addEdge(it, tryExitNodes.top(), propagateDeadness = false) + val finallyEnterNode = finallyEnterNodes.topOrNull() + // NB: Check the level to avoid adding an edge to the finally block at an upper level. + if (finallyEnterNode != null && finallyEnterNode.level == levelCounter + 1) { + addEdge(it, finallyEnterNode, propagateDeadness = false) + } else { + addEdge(it, tryExitNodes.top(), propagateDeadness = false) + } } } diff --git a/compiler/testData/diagnostics/testsWithStdLib/contracts/controlflow/flowInlining/nestedTryCatchFinally.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/contracts/controlflow/flowInlining/nestedTryCatchFinally.fir.kt index d09ec05a883..d7d436c9b87 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/contracts/controlflow/flowInlining/nestedTryCatchFinally.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/contracts/controlflow/flowInlining/nestedTryCatchFinally.fir.kt @@ -45,7 +45,7 @@ fun outerFinallyInitializes() { } // Properly initialized - x.inc() + x.inc() } fun innerFinallyInitializes() {