From 5f0c9490bbaa44fa45c485d14e46cd1316bede38 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 23 Oct 2017 08:58:51 +0300 Subject: [PATCH] Add test for smart cast on a value bound to when subject variable E.g.: when (val y = x?.foo()) { null -> ... else -> ... // 'x' is also non-null here } --- .../smartCastOnValueBoundToSubjectVariable.kt | 9 +++++++++ .../smartCastOnValueBoundToSubjectVariable.txt | 4 ++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ .../javac/DiagnosticsUsingJavacTestGenerated.java | 6 ++++++ 4 files changed, 25 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt create mode 100644 compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt diff --git a/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt new file mode 100644 index 00000000000..8845e45cbff --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt @@ -0,0 +1,9 @@ +// !LANGUAGE: +VariableDeclarationInWhenSubject + +fun foo(s1: String, s2: String) = s1 + s2 + +fun test(x: Any?) = + when (val y = x?.toString()) { + null -> "null" + else -> foo(x.toString(), y) + } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt new file mode 100644 index 00000000000..8be3c40533b --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt @@ -0,0 +1,4 @@ +package + +public fun foo(/*0*/ s1: kotlin.String, /*1*/ s2: kotlin.String): kotlin.String +public fun test(/*0*/ x: kotlin.Any?): kotlin.String diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 9e7645d2486..190880be37a 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -22484,6 +22484,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("smartCastOnValueBoundToSubjectVariable.kt") + public void testSmartCastOnValueBoundToSubjectVariable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt"); + doTest(fileName); + } + @TestMetadata("smartCastsOnSubjectVariable.kt") public void testSmartCastsOnSubjectVariable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastsOnSubjectVariable.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index ddcb85d9f8d..058538d3bd9 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -22484,6 +22484,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing doTest(fileName); } + @TestMetadata("smartCastOnValueBoundToSubjectVariable.kt") + public void testSmartCastOnValueBoundToSubjectVariable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt"); + doTest(fileName); + } + @TestMetadata("smartCastsOnSubjectVariable.kt") public void testSmartCastsOnSubjectVariable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastsOnSubjectVariable.kt");