From 03aaddd379fc203c192939a4d034e7b311d075c7 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 2 Nov 2015 17:25:44 +0300 Subject: [PATCH] More correct handling of possible type set during assignment #KT-8229 Fixed --- .../smartcasts/DelegatingDataFlowInfo.java | 2 +- .../variables/assignmentConversion.kt | 35 +++++++++++++++++++ .../variables/assignmentConversion.txt | 6 ++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.java index 5d2cdd594db..2ededc9318b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.java @@ -175,7 +175,7 @@ import static org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability.NOT_NULL putNullability(nullability, a, nullabilityOfB); SetMultimap newTypeInfo = newTypeInfo(); - Set typesForB = collectTypesFromMeAndParents(b); + Set typesForB = getPossibleTypes(b); // Own type of B must be recorded separately, e.g. for a constant // But if its type is the same as A or it's null, there is no reason to do it // because usually null type or own type are not saved in this set diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.kt new file mode 100644 index 00000000000..1a2ddc2fed4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.kt @@ -0,0 +1,35 @@ +fun foo(x: String) = x + +fun test1() { + var c: Any? = "XXX" + if (c !is String) return + + val newC: String? = "YYY" + if (newC != null) { + c = newC + } + foo(c) +} + +fun test2() { + var c: Any? = "XXX" + if (c !is String) return + + val newC: String? = "YYY" + if (newC is String) { + c = newC + } + foo(c) +} + +fun test3() { + var c: Any? = "XXX" + if (c !is String) return + + val newC: String? = "YYY" + if (newC == null) return + c = newC + + foo(c) +} + diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.txt b/compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.txt new file mode 100644 index 00000000000..55f94cc64e5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.txt @@ -0,0 +1,6 @@ +package + +public fun foo(/*0*/ x: kotlin.String): kotlin.String +public fun test1(): kotlin.Unit +public fun test2(): kotlin.Unit +public fun test3(): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 0657557f15b..7a898f93581 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -15344,6 +15344,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("assignmentConversion.kt") + public void testAssignmentConversion() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/variables/assignmentConversion.kt"); + doTest(fileName); + } + @TestMetadata("doWhileWithMiddleBreak.kt") public void testDoWhileWithMiddleBreak() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/variables/doWhileWithMiddleBreak.kt");