diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index ebddac97c58..5bd57a22985 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -3069,6 +3069,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/equalsAndIdentity.kt"); } + @TestMetadata("incorrectSmartcastToNothing.kt") + public void testIncorrectSmartcastToNothing() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.kt"); + } + @TestMetadata("kt10240.kt") public void testKt10240() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/kt10240.kt"); diff --git a/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.dot b/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.dot new file mode 100644 index 00000000000..a2da65ad285 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.dot @@ -0,0 +1,144 @@ +digraph incorrectSmartcastToNothing_kt { + graph [nodesep=3] + node [shape=box penwidth=2] + edge [penwidth=2] + + subgraph cluster_0 { + color=red + 0 [label="Enter property" style="filled" fillcolor=red]; + 1 [label="Const: String(foo)"]; + 2 [label="Function call: R|java/io/File.File|(...)"]; + 3 [label="Exit property" style="filled" fillcolor=red]; + } + 0 -> {1}; + 1 -> {2}; + 2 -> {3}; + + subgraph cluster_1 { + color=red + 4 [label="Enter function test" style="filled" fillcolor=red]; + subgraph cluster_2 { + color=blue + 5 [label="Enter block"]; + subgraph cluster_3 { + color=blue + 6 [label="Enter when"]; + subgraph cluster_4 { + color=blue + 7 [label="Enter when branch condition "]; + 8 [label="Access variable R|/cacheExtSetting|"]; + 9 [label="Const: Null(null)"]; + 10 [label="Equality operator =="]; + 11 [label="Exit when branch condition"]; + } + subgraph cluster_5 { + color=blue + 12 [label="Enter when branch condition "]; + 13 [label="Access variable R|/cacheExtSetting|"]; + 14 [label="Function call: R|/cacheExtSetting|.R|kotlin/text/isBlank|()"]; + 15 [label="Exit when branch condition"]; + } + subgraph cluster_6 { + color=blue + 16 [label="Enter when branch condition else"]; + 17 [label="Exit when branch condition"]; + } + 18 [label="Enter when branch result"]; + subgraph cluster_7 { + color=blue + 19 [label="Enter block"]; + 20 [label="Access variable R|/cacheExtSetting|"]; + 21 [label="Function call: R|java/io/File.File|(...)"]; + 22 [label="Exit block"]; + } + 23 [label="Exit when branch result"]; + 24 [label="Enter when branch result"]; + subgraph cluster_8 { + color=blue + 25 [label="Enter block"]; + 26 [label="Const: Null(null)"]; + 27 [label="Exit block"]; + } + 28 [label="Exit when branch result"]; + 29 [label="Enter when branch result"]; + subgraph cluster_9 { + color=blue + 30 [label="Enter block"]; + 31 [label="Access variable R|/cache|"]; + 32 [label="Enter safe call"]; + 33 [label="Postponed enter to lambda"]; + subgraph cluster_10 { + color=blue + 43 [label="Enter function anonymousFunction" style="filled" fillcolor=red]; + subgraph cluster_11 { + color=blue + 44 [label="Enter block"]; + 45 [label="Access variable R|/it|"]; + 46 [label="Const: String(main.kts.compiled.cache)"]; + 47 [label="Function call: R|java/io/File.File|(...)"]; + 48 [label="Exit block"]; + } + 49 [label="Exit function anonymousFunction" style="filled" fillcolor=red]; + } + 34 [label="Postponed exit from lambda"]; + 35 [label="Function call: $subj$.R|kotlin/let|(...)"]; + 36 [label="Exit safe call"]; + 37 [label="Exit block"]; + } + 38 [label="Exit when branch result"]; + 39 [label="Exit when"]; + } + 40 [label="Variable declaration: lval cacheBaseDir: R|java/io/File?|"]; + 41 [label="Exit block"]; + } + 42 [label="Exit function test" style="filled" fillcolor=red]; + } + 4 -> {5}; + 5 -> {6}; + 6 -> {7}; + 7 -> {8}; + 8 -> {9}; + 9 -> {10}; + 10 -> {11}; + 11 -> {29 12}; + 12 -> {13}; + 13 -> {14}; + 14 -> {15}; + 15 -> {24 16}; + 16 -> {17}; + 17 -> {18}; + 18 -> {19}; + 19 -> {20}; + 20 -> {21}; + 21 -> {22}; + 22 -> {23}; + 23 -> {39}; + 24 -> {25}; + 25 -> {26}; + 26 -> {27}; + 27 -> {28}; + 28 -> {39}; + 29 -> {30}; + 30 -> {31}; + 31 -> {32 36}; + 32 -> {33}; + 33 -> {43}; + 33 -> {34} [color=red]; + 33 -> {43} [style=dashed]; + 34 -> {35}; + 35 -> {36}; + 36 -> {37}; + 37 -> {38}; + 38 -> {39}; + 39 -> {40}; + 40 -> {41}; + 41 -> {42}; + 43 -> {44}; + 44 -> {45}; + 45 -> {46}; + 46 -> {47}; + 47 -> {48}; + 48 -> {49}; + 49 -> {34} [color=green]; + +} diff --git a/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.fir.txt b/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.fir.txt new file mode 100644 index 00000000000..8ddcedfeede --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.fir.txt @@ -0,0 +1,20 @@ +FILE: incorrectSmartcastToNothing.kt + public final val cache: R|java/io/File?| = R|java/io/File.File|(String(foo)) + public get(): R|java/io/File?| + public final fun test(cacheExtSetting: R|kotlin/String?|): R|kotlin/Unit| { + lval cacheBaseDir: R|java/io/File?| = when () { + ==(R|/cacheExtSetting|, Null(null)) -> { + R|/cache|?.{ $subj$.R|kotlin/let|( = let@fun (it: R|java/io/File|): R|java/io/File| { + ^ R|java/io/File.File|(R|/it|, String(main.kts.compiled.cache)) + } + ) } + } + R|/cacheExtSetting|.R|kotlin/text/isBlank|() -> { + Null(null) + } + else -> { + R|java/io/File.File|(R|/cacheExtSetting|) + } + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.kt b/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.kt new file mode 100644 index 00000000000..ae58063ea5e --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.kt @@ -0,0 +1,15 @@ +// WITH_STDLIB +// FULL_JDK +// DUMP_CFG + +import java.io.File + +val cache: File? = File("foo") + +fun test(cacheExtSetting: String?) { + val cacheBaseDir = when { + cacheExtSetting == null -> cache?.let { File(it, "main.kts.compiled.cache") } + cacheExtSetting.isBlank() -> null + else -> File(cacheExtSetting) + } +} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index a1e3074faa2..f865dfbe335 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -3467,6 +3467,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/equalsAndIdentity.kt"); } + @Test + @TestMetadata("incorrectSmartcastToNothing.kt") + public void testIncorrectSmartcastToNothing() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.kt"); + } + @Test @TestMetadata("kt10240.kt") public void testKt10240() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 4947280bebc..3941849cfaf 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -3467,6 +3467,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/equalsAndIdentity.kt"); } + @Test + @TestMetadata("incorrectSmartcastToNothing.kt") + public void testIncorrectSmartcastToNothing() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.kt"); + } + @Test @TestMetadata("kt10240.kt") public void testKt10240() 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 67f2fc9e69a..b0736220a63 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 @@ -627,6 +627,7 @@ class ControlFlowGraphBuilder { } fun enterWhenBranchCondition(whenBranch: FirWhenBranch): WhenBranchConditionEnterNode { + levelCounter += whenBranchIndices.top().getValue(whenBranch) return createWhenBranchConditionEnterNode(whenBranch).also { addNewSimpleNode(it) }.also { levelCounter++ } } @@ -639,7 +640,6 @@ class ControlFlowGraphBuilder { lastNodes.push(it) addEdge(conditionExitNode, it) } - levelCounter += whenBranchIndices.top().getValue(whenBranch) return conditionExitNode to branchEnterNode } diff --git a/compiler/testData/codegen/box/smartCasts/kt44814.dot b/compiler/testData/codegen/box/smartCasts/kt44814.dot index b9b39ab61ab..3425f9620c6 100644 --- a/compiler/testData/codegen/box/smartCasts/kt44814.dot +++ b/compiler/testData/codegen/box/smartCasts/kt44814.dot @@ -572,17 +572,17 @@ digraph kt44814_kt { } subgraph cluster_59 { color=blue - 177 [label="Enter when branch condition [4]"]; - 178 [label="Type operator: ($subj$ is R|FirPsiSourceElement|) [5]"]; - 179 [label="Exit when branch condition [4]"]; + 177 [label="Enter when branch condition [5]"]; + 178 [label="Type operator: ($subj$ is R|FirPsiSourceElement|) [6]"]; + 179 [label="Exit when branch condition [5]"]; } subgraph cluster_60 { color=blue - 180 [label="Enter when branch condition [4]"]; - 181 [label="Type operator: ($subj$ is R|FirLightSourceElement|) [5]"]; - 182 [label="Exit when branch condition [4]"]; + 180 [label="Enter when branch condition [6]"]; + 181 [label="Type operator: ($subj$ is R|FirLightSourceElement|) [7]"]; + 182 [label="Exit when branch condition [6]"]; } - 183 [label="Enter when branch result [5]"]; + 183 [label="Enter when branch result [7]"]; subgraph cluster_61 { color=blue 184 [label="Enter block [7]"]; @@ -623,7 +623,7 @@ digraph kt44814_kt { 202 [label="Exit block [7]"]; } 203 [label="Exit when branch result [6]"]; - 204 [label="Enter when branch result [5]"]; + 204 [label="Enter when branch result [6]"]; subgraph cluster_64 { color=blue 205 [label="Enter block [6]"]; @@ -796,10 +796,10 @@ digraph kt44814_kt { } subgraph cluster_72 { color=blue - 261 [label="Enter when branch condition else [4]"]; - 262 [label="Exit when branch condition [4]"]; + 261 [label="Enter when branch condition else [5]"]; + 262 [label="Exit when branch condition [5]"]; } - 263 [label="Enter when branch result [5]"]; + 263 [label="Enter when branch result [6]"]; subgraph cluster_73 { color=blue 264 [label="Enter block [6]"]; diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index 1785475e2b5..6a824e05ba3 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -3467,6 +3467,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/equalsAndIdentity.kt"); } + @Test + @TestMetadata("incorrectSmartcastToNothing.kt") + public void testIncorrectSmartcastToNothing() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/incorrectSmartcastToNothing.kt"); + } + @Test @TestMetadata("kt10240.kt") public void testKt10240() throws Exception {