From 1f66c64ee002fc4accc51801475ec1ac02584b25 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 20 Nov 2014 17:12:48 +0300 Subject: [PATCH] `dynamic` receiver admits nullable values --- .../lang/resolve/calls/CandidateResolver.java | 3 +-- .../resolve/calls/smartcasts/SmartCastUtils.java | 6 ++++-- .../diagnostics/tests/dynamicTypes/block.kt | 16 ++++++++++++++++ .../diagnostics/tests/dynamicTypes/block.txt | 4 ++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++++ 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/dynamicTypes/block.kt create mode 100644 compiler/testData/diagnostics/tests/dynamicTypes/block.txt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java index a600c3f686f..6cdb350abd1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java @@ -597,8 +597,7 @@ public class CandidateResolver { BindingContext bindingContext = trace.getBindingContext(); if (!safeAccess && !receiverParameter.getType().isNullable() && receiverArgumentType.isNullable()) { - if (!SmartCastUtils.isNotNull(receiverArgument, bindingContext, context.dataFlowInfo)) { - + if (!SmartCastUtils.canBeSmartCast(receiverParameter, receiverArgument, bindingContext, context.dataFlowInfo)) { context.tracing.unsafeCall(trace, receiverArgumentType, implicitInvokeCheck); return UNSAFE_CALL_ERROR; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/smartcasts/SmartCastUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/smartcasts/SmartCastUtils.java index 1077f003549..33d35880be1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/smartcasts/SmartCastUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/smartcasts/SmartCastUtils.java @@ -22,6 +22,7 @@ import kotlin.Function1; import kotlin.KotlinPackage; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor; import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingTrace; @@ -183,7 +184,8 @@ public class SmartCastUtils { } } - public static boolean isNotNull( + public static boolean canBeSmartCast( + @NotNull ReceiverParameterDescriptor receiverParameter, @NotNull ReceiverValue receiver, @NotNull BindingContext bindingContext, @NotNull DataFlowInfo dataFlowInfo @@ -192,7 +194,7 @@ public class SmartCastUtils { List smartCastVariants = getSmartCastVariants(receiver, bindingContext, dataFlowInfo); for (JetType smartCastVariant : smartCastVariants) { - if (!smartCastVariant.isNullable()) return true; + if (JetTypeChecker.DEFAULT.isSubtypeOf(smartCastVariant, receiverParameter.getType())) return true; } return false; } diff --git a/compiler/testData/diagnostics/tests/dynamicTypes/block.kt b/compiler/testData/diagnostics/tests/dynamicTypes/block.kt new file mode 100644 index 00000000000..ca53df7e80d --- /dev/null +++ b/compiler/testData/diagnostics/tests/dynamicTypes/block.kt @@ -0,0 +1,16 @@ +// !MARK_DYNAMIC_CALLS + +// MODULE[js]: m1 +// FILE: k.kt + +fun foo() { + dynamic { + foo() + bar.baz(0) + } +} + +fun dynamic(body: dynamic.() -> T): T { + val topLevel = null + return topLevel.body() +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/dynamicTypes/block.txt b/compiler/testData/diagnostics/tests/dynamicTypes/block.txt new file mode 100644 index 00000000000..69284db5cd5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/dynamicTypes/block.txt @@ -0,0 +1,4 @@ +package + +internal fun dynamic(/*0*/ body: dynamic.() -> T): T +internal fun foo(): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index b6afd4c6fd5..52d8b07b51e 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -3722,6 +3722,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("block.kt") + public void testBlock() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dynamicTypes/block.kt"); + doTest(fileName); + } + @TestMetadata("dynamicCalls.kt") public void testDynamicCalls() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dynamicTypes/dynamicCalls.kt");