From dd682bd37d6efe8af3cb84ec132532c7bc2648f8 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Mon, 1 Oct 2018 14:16:44 +0300 Subject: [PATCH] Add test on data-flow Idea is to intersect similar smartcasts on similar properties coming from *different* instances (something like 'this.x == null && other.x == null' ). It checks that we distinguish subjects of such smartcasts properly. Currently behavior is undesired, see KT-27260 --- .../smartcastOnSameFieldOfDifferentInstances.kt | 11 +++++++++++ .../smartcastOnSameFieldOfDifferentInstances.txt | 10 ++++++++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 5 +++++ .../javac/DiagnosticsUsingJavacTestGenerated.java | 5 +++++ 4 files changed, 31 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.txt diff --git a/compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.kt b/compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.kt new file mode 100644 index 00000000000..7433c144bd5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.kt @@ -0,0 +1,11 @@ +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// See KT-27260 + +class A(val x: String?) { + fun foo(other: A) { + when { + x == null && other.x == null -> "1" + x.length > 0 -> "2" + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.txt b/compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.txt new file mode 100644 index 00000000000..72171de2bd1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.txt @@ -0,0 +1,10 @@ +package + +public final class A { + public constructor A(/*0*/ x: kotlin.String?) + public final val x: kotlin.String? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(/*0*/ other: A): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 5ceed36006f..7847d7d6a32 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -19377,6 +19377,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/smartCasts/smartCastOnWhen.kt"); } + @TestMetadata("smartcastOnSameFieldOfDifferentInstances.kt") + public void testSmartcastOnSameFieldOfDifferentInstances() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.kt"); + } + @TestMetadata("thisWithLabel.kt") public void testThisWithLabel() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/thisWithLabel.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 7ac4d1a4e53..486ad61745c 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -19377,6 +19377,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/smartCasts/smartCastOnWhen.kt"); } + @TestMetadata("smartcastOnSameFieldOfDifferentInstances.kt") + public void testSmartcastOnSameFieldOfDifferentInstances() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/smartcastOnSameFieldOfDifferentInstances.kt"); + } + @TestMetadata("thisWithLabel.kt") public void testThisWithLabel() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/thisWithLabel.kt");