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 058ffd30047..8fc68a71fe8 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 @@ -30669,6 +30669,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedByNested.kt"); } + @Test + @TestMetadata("capturedLoopVariable.kt") + public void testCapturedLoopVariable() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.kt"); + } + @Test @TestMetadata("capturedWithControlJumps.kt") public void testCapturedWithControlJumps() 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 5528581ee09..aa653e18f7e 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 @@ -30765,6 +30765,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedByNested.kt"); } + @Test + @TestMetadata("capturedLoopVariable.kt") + public void testCapturedLoopVariable() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.kt"); + } + @Test @TestMetadata("capturedWithControlJumps.kt") public void testCapturedWithControlJumps() 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 dd3499d386f..7c7797ea604 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 @@ -30669,6 +30669,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedByNested.kt"); } + @Test + @TestMetadata("capturedLoopVariable.kt") + public void testCapturedLoopVariable() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.kt"); + } + @Test @TestMetadata("capturedWithControlJumps.kt") public void testCapturedWithControlJumps() throws Exception { diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.fir.kt new file mode 100644 index 00000000000..5f50f78d3ad --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.fir.kt @@ -0,0 +1,99 @@ +// ISSUE: KT-55338 + +fun test_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // smartcast in K1, unsafe call in K2 <------------ + } +} + +fun test_2_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + s = "" + } +} + +fun test_2_2() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + s = null + } +} + +fun test_3_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } else { + s = null + } + s.length // unsafe call in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + } +} + +fun test_3_2() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } else { + s = "world" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // smartcast in K1, unsafe call in K2 <------------ + } +} + +fun test_4_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = getString() + } else { + s = getNullableString() + } + s.length // unsafe call in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + } +} + +fun test_4_2() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = getString() + } else { + s = getString() + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // smartcast in K1, unsafe call in K2 <------------ + } +} + +fun getNullableString(): String? = null +fun getString(): String = "hello" +fun noInlineRun(block: () -> Unit) {} diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.kt new file mode 100644 index 00000000000..533d08ffc73 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.kt @@ -0,0 +1,99 @@ +// ISSUE: KT-55338 + +fun test_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // smartcast in K1, unsafe call in K2 <------------ + } +} + +fun test_2_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + s = "" + } +} + +fun test_2_2() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + s = null + } +} + +fun test_3_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } else { + s = null + } + s.length // unsafe call in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + } +} + +fun test_3_2() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = "hello" + } else { + s = "world" + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // smartcast in K1, unsafe call in K2 <------------ + } +} + +fun test_4_1() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = getString() + } else { + s = getNullableString() + } + s.length // unsafe call in K1 and K2 + noInlineRun { s.length } // unsafe call in K1 and K2 + } +} + +fun test_4_2() { + var s: String? = null + + for (i in 1..10) { + if (s == null) { + s = getString() + } else { + s = getString() + } + s.length // smartcast in K1 and K2 + noInlineRun { s.length } // smartcast in K1, unsafe call in K2 <------------ + } +} + +fun getNullableString(): String? = null +fun getString(): String = "hello" +fun noInlineRun(block: () -> Unit) {} diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.txt b/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.txt new file mode 100644 index 00000000000..91f22a217ef --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.txt @@ -0,0 +1,12 @@ +package + +public fun getNullableString(): kotlin.String? +public fun getString(): kotlin.String +public fun noInlineRun(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public fun test_1(): kotlin.Unit +public fun test_2_1(): kotlin.Unit +public fun test_2_2(): kotlin.Unit +public fun test_3_1(): kotlin.Unit +public fun test_3_2(): kotlin.Unit +public fun test_4_1(): kotlin.Unit +public fun test_4_2(): kotlin.Unit 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 233d6378629..e6b69b252ab 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 @@ -30765,6 +30765,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedByNested.kt"); } + @Test + @TestMetadata("capturedLoopVariable.kt") + public void testCapturedLoopVariable() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/variables/capturedLoopVariable.kt"); + } + @Test @TestMetadata("capturedWithControlJumps.kt") public void testCapturedWithControlJumps() throws Exception {