From d4d633ea2652859dbea824a4e11e948b447150b9 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 11 Sep 2015 17:16:53 +0300 Subject: [PATCH] Combined identifier info for things like '(x + y).z' is no longer treated as 'z' identifier info + a pair of tests + code fix #KT-9126 Fixed --- .../calls/smartcasts/DataFlowValueFactory.java | 11 +++++------ .../diagnostics/tests/smartCasts/fieldExclExcl.kt | 12 ++++++++++++ .../diagnostics/tests/smartCasts/fieldExclExcl.txt | 11 +++++++++++ .../diagnostics/tests/smartCasts/fieldPlus.kt | 14 ++++++++++++++ .../diagnostics/tests/smartCasts/fieldPlus.txt | 12 ++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 12 ++++++++++++ .../kotlin/idea/actions/JavaToKotlinAction.kt | 2 +- 7 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.txt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/fieldPlus.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/fieldPlus.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowValueFactory.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowValueFactory.java index f682e78c02c..0a071387506 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowValueFactory.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DataFlowValueFactory.java @@ -26,7 +26,6 @@ import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor; import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.BindingContext; -import org.jetbrains.kotlin.resolve.BindingContextUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage; import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext; @@ -188,16 +187,16 @@ public class DataFlowValueFactory { } @NotNull - private static IdentifierInfo createPackageInfo(Object id) { + private static IdentifierInfo createPackageOrClassInfo(Object id) { return new IdentifierInfo(id, true, false, true); } @NotNull private static IdentifierInfo combineInfo(@Nullable IdentifierInfo receiverInfo, @NotNull IdentifierInfo selectorInfo) { - if (selectorInfo.id == null) { + if (selectorInfo.id == null || receiverInfo == NO_IDENTIFIER_INFO) { return NO_IDENTIFIER_INFO; } - if (receiverInfo == null || receiverInfo == NO_IDENTIFIER_INFO || receiverInfo.isPackage) { + if (receiverInfo == null || receiverInfo.isPackage) { return selectorInfo; } return createInfo(Pair.create(receiverInfo.id, selectorInfo.id), @@ -281,8 +280,8 @@ public class DataFlowValueFactory { isStableVariable(variableDescriptor, usageModuleDescriptor), isUncapturedLocalVariable(variableDescriptor, bindingContext))); } - if (declarationDescriptor instanceof PackageViewDescriptor) { - return createPackageInfo(declarationDescriptor); + if (declarationDescriptor instanceof PackageViewDescriptor || declarationDescriptor instanceof ClassDescriptor) { + return createPackageOrClassInfo(declarationDescriptor); } return NO_IDENTIFIER_INFO; } diff --git a/compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.kt b/compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.kt new file mode 100644 index 00000000000..2d406e0dc06 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.kt @@ -0,0 +1,12 @@ +// !DIAGNOSTICS: -UNNECESSARY_NOT_NULL_ASSERTION +// See KT-9126: Variable change does not affect data flow info for its fields + +class My(val x: Int?) + +fun foo() { + var y: My? = My(42) + if (y!!.x != null) { + y = My(null) + y!!.x.hashCode() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.txt b/compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.txt new file mode 100644 index 00000000000..363c0c77c19 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.txt @@ -0,0 +1,11 @@ +package + +public fun foo(): kotlin.Unit + +public final class My { + public constructor My(/*0*/ x: kotlin.Int?) + public final val x: kotlin.Int? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/fieldPlus.kt b/compiler/testData/diagnostics/tests/smartCasts/fieldPlus.kt new file mode 100644 index 00000000000..258c1db6e12 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/fieldPlus.kt @@ -0,0 +1,14 @@ +// !DIAGNOSTICS: -UNNECESSARY_NOT_NULL_ASSERTION +// Advancement of KT-9126 + +class My(val x: Int?) { + fun plus(y: My) = if (this.x != null) this else y +} + +fun foo() { + var y: My? = My(42) + if (y!!.x != null) { + y = My(null) + (y + My(0)).x.hashCode() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/fieldPlus.txt b/compiler/testData/diagnostics/tests/smartCasts/fieldPlus.txt new file mode 100644 index 00000000000..5671067439d --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/fieldPlus.txt @@ -0,0 +1,12 @@ +package + +public fun foo(): kotlin.Unit + +public final class My { + public constructor My(/*0*/ x: kotlin.Int?) + public final val x: kotlin.Int? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public final fun plus(/*0*/ y: My): My + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 712b1d24ad5..67322cb46e0 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -13197,6 +13197,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("fieldExclExcl.kt") + public void testFieldExclExcl() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/fieldExclExcl.kt"); + doTest(fileName); + } + + @TestMetadata("fieldPlus.kt") + public void testFieldPlus() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/fieldPlus.kt"); + doTest(fileName); + } + @TestMetadata("kt1461.kt") public void testKt1461() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/kt1461.kt"); diff --git a/idea/src/org/jetbrains/kotlin/idea/actions/JavaToKotlinAction.kt b/idea/src/org/jetbrains/kotlin/idea/actions/JavaToKotlinAction.kt index 7ac2b72f897..9781d1dbde8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/actions/JavaToKotlinAction.kt +++ b/idea/src/org/jetbrains/kotlin/idea/actions/JavaToKotlinAction.kt @@ -73,7 +73,7 @@ public class JavaToKotlinAction : AnAction() { ProgressManager.getInstance().runProcessWithProgressSynchronously( { runReadAction { - externalCodeUpdate = converterResult!!.externalCodeProcessing.prepareWriteOperation(ProgressManager.getInstance().getProgressIndicator()) + externalCodeUpdate = converterResult!!.externalCodeProcessing!!.prepareWriteOperation(ProgressManager.getInstance().getProgressIndicator()) } }, title,