From 146036010c9e53e6b681db6e8fd34aee5fbc3284 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Mon, 12 Oct 2020 22:45:31 -0700 Subject: [PATCH] FIR DFA: add tests about smartcasts in/after try-catch-finally --- ...irOldFrontendDiagnosticsTestGenerated.java | 10 ++++++ .../castchecks/castInTryWithCatch.fir.kt | 32 +++++++++++++++++++ .../castchecks/castInTryWithCatch.kt | 32 +++++++++++++++++++ .../castchecks/castInTryWithCatch.txt | 5 +++ .../castchecks/castInTryWithoutCatch.fir.kt | 17 ++++++++++ .../castchecks/castInTryWithoutCatch.kt | 17 ++++++++++ .../castchecks/castInTryWithoutCatch.txt | 4 +++ .../checkers/DiagnosticsTestGenerated.java | 10 ++++++ .../DiagnosticsUsingJavacTestGenerated.java | 10 ++++++ 9 files changed, 137 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.fir.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.txt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.fir.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.txt diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index f25811e0fa1..07ae23e4ca8 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -21915,6 +21915,16 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/basicOn.kt"); } + @TestMetadata("castInTryWithCatch.kt") + public void testCastInTryWithCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.kt"); + } + + @TestMetadata("castInTryWithoutCatch.kt") + public void testCastInTryWithoutCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.kt"); + } + @TestMetadata("impossible.kt") public void testImpossible() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/impossible.kt"); diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.fir.kt new file mode 100644 index 00000000000..76c04d0251a --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.fir.kt @@ -0,0 +1,32 @@ +fun castInTry(s: Any) { + try { + s as String // Potential cast exception + } catch (e: Exception) { + s.length // shouldn't be resolved + } finally { + s.length // shouldn't be resolved + } + s.length // shouldn't be resolved +} + +fun castInTryAndCatch(s: Any) { + try { + s as String // Potential cast exception + } catch (e: Exception) { + s as String // Potential cast exception + } finally { + s.length // shouldn't be resolved + } + s.length // should be smartcast +} + +fun castAtAll(s: Any) { + try { + s as String // Potential cast exception + } catch (e: Exception) { + s as String // Potential cast exception + } finally { + s as String // Potential cast exception + } + s.length +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.kt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.kt new file mode 100644 index 00000000000..b9741a228cb --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.kt @@ -0,0 +1,32 @@ +fun castInTry(s: Any) { + try { + s as String // Potential cast exception + } catch (e: Exception) { + s.length // shouldn't be resolved + } finally { + s.length // shouldn't be resolved + } + s.length // shouldn't be resolved +} + +fun castInTryAndCatch(s: Any) { + try { + s as String // Potential cast exception + } catch (e: Exception) { + s as String // Potential cast exception + } finally { + s.length // shouldn't be resolved + } + s.length // should be smartcast +} + +fun castAtAll(s: Any) { + try { + s as String // Potential cast exception + } catch (e: Exception) { + s as String // Potential cast exception + } finally { + s as String // Potential cast exception + } + s.length +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.txt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.txt new file mode 100644 index 00000000000..5d6f5c5e949 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.txt @@ -0,0 +1,5 @@ +package + +public fun castAtAll(/*0*/ s: kotlin.Any): kotlin.Unit +public fun castInTry(/*0*/ s: kotlin.Any): kotlin.Unit +public fun castInTryAndCatch(/*0*/ s: kotlin.Any): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.fir.kt new file mode 100644 index 00000000000..44c1618c7ac --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.fir.kt @@ -0,0 +1,17 @@ +fun castInTry(s: Any) { + try { + s as String // Potential cast exception + } finally { + s.length // Shouldn't be resolved + } + s.length // Shouldn't be resolved +} + +fun castInTryAndFinally(s: Any) { + try { + s as String // Potential cast exception + } finally { + s as String // Potential cast exception + } + s.length // Should be smartcast +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.kt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.kt new file mode 100644 index 00000000000..754f1bc6ab2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.kt @@ -0,0 +1,17 @@ +fun castInTry(s: Any) { + try { + s as String // Potential cast exception + } finally { + s.length // Shouldn't be resolved + } + s.length // Shouldn't be resolved +} + +fun castInTryAndFinally(s: Any) { + try { + s as String // Potential cast exception + } finally { + s as String // Potential cast exception + } + s.length // Should be smartcast +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.txt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.txt new file mode 100644 index 00000000000..019ab2ae2be --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.txt @@ -0,0 +1,4 @@ +package + +public fun castInTry(/*0*/ s: kotlin.Any): kotlin.Unit +public fun castInTryAndFinally(/*0*/ s: kotlin.Any): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 290b35dd7da..eb9b16060f9 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -21992,6 +21992,16 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/basicOn.kt"); } + @TestMetadata("castInTryWithCatch.kt") + public void testCastInTryWithCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.kt"); + } + + @TestMetadata("castInTryWithoutCatch.kt") + public void testCastInTryWithoutCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.kt"); + } + @TestMetadata("impossible.kt") public void testImpossible() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/impossible.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 573adcf8980..6be9c99bccb 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -21917,6 +21917,16 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/basicOn.kt"); } + @TestMetadata("castInTryWithCatch.kt") + public void testCastInTryWithCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithCatch.kt"); + } + + @TestMetadata("castInTryWithoutCatch.kt") + public void testCastInTryWithoutCatch() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/castInTryWithoutCatch.kt"); + } + @TestMetadata("impossible.kt") public void testImpossible() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/castchecks/impossible.kt");