From 6fb871a088e66d3b61aeaa11ec8a1bc3e96adafc Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 11 Dec 2015 12:08:36 +0300 Subject: [PATCH] A set of additional tests for complex expressions DFA --- .../tests/smartCasts/ifCascadeExprNotNull.kt | 14 ++++++++ .../tests/smartCasts/ifCascadeExprNotNull.txt | 3 ++ .../smartCasts/ifExprInConditionNonNull.kt | 11 ++++++ .../smartCasts/ifExprInConditionNonNull.txt | 3 ++ .../smartCasts/ifExprInWhenSubjectNonNull.kt | 11 ++++++ .../smartCasts/ifExprInWhenSubjectNonNull.txt | 3 ++ .../tests/smartCasts/ifWhenExprNonNull.kt | 13 +++++++ .../tests/smartCasts/ifWhenExprNonNull.txt | 3 ++ .../tests/smartCasts/shortIfExprNotNull.kt | 9 +++++ .../tests/smartCasts/shortIfExprNotNull.txt | 3 ++ .../tests/smartCasts/whenIfExprNonNull.kt | 13 +++++++ .../tests/smartCasts/whenIfExprNonNull.txt | 3 ++ .../checkers/DiagnosticsTestGenerated.java | 36 +++++++++++++++++++ 13 files changed, 125 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.txt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.txt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.txt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.txt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.txt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.txt diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.kt b/compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.kt new file mode 100644 index 00000000000..c9659fabce5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.kt @@ -0,0 +1,14 @@ +fun baz(s: String?): String { + val t = if (s == null) { + "" + } + else if (s == "") { + val u: String? = null + if (u == null) return "" + u + } + else { + s + } + return t +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.txt b/compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.txt new file mode 100644 index 00000000000..9a5ac9f28cf --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.txt @@ -0,0 +1,3 @@ +package + +public fun baz(/*0*/ s: kotlin.String?): kotlin.String diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.kt b/compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.kt new file mode 100644 index 00000000000..f9dae64aa86 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.kt @@ -0,0 +1,11 @@ +fun baz(s: String?, b: Boolean?): String { + val t = if (if (b == null) return "" else b) { + if (s == null) return "" + s + } + else { + if (s != null) return s + "" + } + return t +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.txt b/compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.txt new file mode 100644 index 00000000000..0fc2a4fa64e --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.txt @@ -0,0 +1,3 @@ +package + +public fun baz(/*0*/ s: kotlin.String?, /*1*/ b: kotlin.Boolean?): kotlin.String diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.kt b/compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.kt new file mode 100644 index 00000000000..9f8903d76c0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.kt @@ -0,0 +1,11 @@ +fun baz(s: String?, u: String?): String { + val t = when(if (u == null) return "" else u) { + "abc" -> u + "" -> { + if (s == null) return "" + s + } + else -> u + } + return t +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.txt b/compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.txt new file mode 100644 index 00000000000..56ba9bd6cac --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.txt @@ -0,0 +1,3 @@ +package + +public fun baz(/*0*/ s: kotlin.String?, /*1*/ u: kotlin.String?): kotlin.String diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.kt b/compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.kt new file mode 100644 index 00000000000..f58e9729feb --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.kt @@ -0,0 +1,13 @@ +fun baz(s: String?): String { + val t = if (s == null) { + "" + } + else { + val u: String? = null + when (u) { + null -> "" + else -> u + } + } + return t +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.txt b/compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.txt new file mode 100644 index 00000000000..9a5ac9f28cf --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.txt @@ -0,0 +1,3 @@ +package + +public fun baz(/*0*/ s: kotlin.String?): kotlin.String diff --git a/compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.kt b/compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.kt new file mode 100644 index 00000000000..ac79af76ecd --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.kt @@ -0,0 +1,9 @@ +fun baz(s: String?): String { + val t = if (s != null) s + else { + val u: String? = null + if (u == null) return "" + u + } + return t +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.txt b/compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.txt new file mode 100644 index 00000000000..9a5ac9f28cf --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.txt @@ -0,0 +1,3 @@ +package + +public fun baz(/*0*/ s: kotlin.String?): kotlin.String diff --git a/compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.kt b/compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.kt new file mode 100644 index 00000000000..e17cf49265c --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.kt @@ -0,0 +1,13 @@ +fun baz(s: String?, u: String?): String { + val t = when(s) { + is String -> { + if (u == null) return s + u + } + else -> { + if (u == null) return "" + u + } + } + return t +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.txt b/compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.txt new file mode 100644 index 00000000000..56ba9bd6cac --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.txt @@ -0,0 +1,3 @@ +package + +public fun baz(/*0*/ s: kotlin.String?, /*1*/ u: kotlin.String?): kotlin.String diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 84de1fe4a57..de7564b5cfe 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -14967,12 +14967,36 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("ifCascadeExprNotNull.kt") + public void testIfCascadeExprNotNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/ifCascadeExprNotNull.kt"); + doTest(fileName); + } + + @TestMetadata("ifExprInConditionNonNull.kt") + public void testIfExprInConditionNonNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/ifExprInConditionNonNull.kt"); + doTest(fileName); + } + + @TestMetadata("ifExprInWhenSubjectNonNull.kt") + public void testIfExprInWhenSubjectNonNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/ifExprInWhenSubjectNonNull.kt"); + doTest(fileName); + } + @TestMetadata("ifExprNonNull.kt") public void testIfExprNonNull() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/ifExprNonNull.kt"); doTest(fileName); } + @TestMetadata("ifWhenExprNonNull.kt") + public void testIfWhenExprNonNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/ifWhenExprNonNull.kt"); + doTest(fileName); + } + @TestMetadata("implicitReceiver.kt") public void testImplicitReceiver() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/implicitReceiver.kt"); @@ -15231,6 +15255,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("shortIfExprNotNull.kt") + public void testShortIfExprNotNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/shortIfExprNotNull.kt"); + doTest(fileName); + } + @TestMetadata("smartCastOnElvis.kt") public void testSmartCastOnElvis() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/smartCastOnElvis.kt"); @@ -15327,6 +15357,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("whenIfExprNonNull.kt") + public void testWhenIfExprNonNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/whenIfExprNonNull.kt"); + doTest(fileName); + } + @TestMetadata("whenSubjectImpossible.kt") public void testWhenSubjectImpossible() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/whenSubjectImpossible.kt");