From 1667f18d7c8b19688fdfdbaeeeaf84f4c8828119 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 9 Sep 2015 17:43:42 +0300 Subject: [PATCH] Receiver of a safe call is not null inside the call also for extension functions + a pair of tests #KT-9033 Fixed --- .../context/CallCandidateResolutionContext.java | 1 - .../calls/model/DataFlowInfoForArgumentsImpl.java | 2 +- .../tests/smartCasts/safecalls/extensionCall.kt | 6 ++++++ .../tests/smartCasts/safecalls/extensionCall.txt | 4 ++++ .../smartcasts/letUsesOwnReceiver.kt | 7 +++++++ .../smartcasts/letUsesOwnReceiver.txt | 4 ++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++++ .../JetDiagnosticsTestWithStdLibGenerated.java | 15 +++++++++++++++ 8 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.txt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.kt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/context/CallCandidateResolutionContext.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/context/CallCandidateResolutionContext.java index 71e7a4eeb90..8da0467c2dd 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/context/CallCandidateResolutionContext.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/context/CallCandidateResolutionContext.java @@ -75,7 +75,6 @@ public final class CallCandidateResolutionContext @NotNull TracingStrategy tracing, @NotNull Call call, @NotNull ReceiverValue explicitExtensionReceiverForInvoke, @NotNull CandidateResolveMode candidateResolveMode ) { - candidateCall.getDataFlowInfoForArguments().setInitialDataFlowInfo(context.dataFlowInfo); return new CallCandidateResolutionContext( candidateCall, tracing, trace, context.scope, call, context.expectedType, context.dataFlowInfo, context.contextDependency, context.checkArguments, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/model/DataFlowInfoForArgumentsImpl.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/model/DataFlowInfoForArgumentsImpl.java index 5e039c22e0c..414e76ec8e3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/model/DataFlowInfoForArgumentsImpl.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/model/DataFlowInfoForArgumentsImpl.java @@ -86,8 +86,8 @@ public class DataFlowInfoForArgumentsImpl implements MutableDataFlowInfoForArgum @NotNull @Override public DataFlowInfo getResultInfo() { - if (resultInfo == null) return initialInfo; assert initialInfo != null : "Initial data flow info was not set for call: " + call; + if (resultInfo == null) return initialInfo; return initialInfo.and(resultInfo); } } diff --git a/compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.kt b/compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.kt new file mode 100644 index 00000000000..e1ba3e3a11e --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.kt @@ -0,0 +1,6 @@ +fun String.bar(s: String) = s + +fun foo(s: String?) { + s?.bar(s) + s?.charAt(s.length()) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.txt b/compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.txt new file mode 100644 index 00000000000..7e67b087abb --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.txt @@ -0,0 +1,4 @@ +package + +public fun foo(/*0*/ s: kotlin.String?): kotlin.Unit +public fun kotlin.String.bar(/*0*/ s: kotlin.String): kotlin.String diff --git a/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.kt b/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.kt new file mode 100644 index 00000000000..647362874a9 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.kt @@ -0,0 +1,7 @@ +// Based on KT-9033 +fun f(s: String) = s + +fun foo(s: String?) { + s?.let { f(s) } + s?.let { f(it) } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.txt b/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.txt new file mode 100644 index 00000000000..5c0c35d8c00 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.txt @@ -0,0 +1,4 @@ +package + +public fun f(/*0*/ s: kotlin.String): kotlin.String +public fun foo(/*0*/ s: kotlin.String?): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 91df5d10d77..5e654fb4ceb 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -13895,6 +13895,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("extensionCall.kt") + public void testExtensionCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/safecalls/extensionCall.kt"); + doTest(fileName); + } + @TestMetadata("falseArgument.kt") public void testFalseArgument() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/safecalls/falseArgument.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithStdLibGenerated.java index 5f86bea930a..6d75df2c554 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithStdLibGenerated.java @@ -830,6 +830,21 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic } } + @TestMetadata("compiler/testData/diagnostics/testsWithStdLib/smartcasts") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Smartcasts extends AbstractJetDiagnosticsTestWithStdLib { + public void testAllFilesPresentInSmartcasts() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/smartcasts"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("letUsesOwnReceiver.kt") + public void testLetUsesOwnReceiver() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/smartcasts/letUsesOwnReceiver.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/diagnostics/testsWithStdLib/varargs") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)