From 8a93421ecbe35887c7c001e1dd1759c0e1d014b5 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 29 Nov 2011 21:03:42 +0300 Subject: [PATCH] KT-597 Type inference failed Resolution rules changed: now autocast and non-autocast candidates are judged together --- .../lang/resolve/calls/TaskPrioritizer.java | 16 ++------------- .../calls/autocasts/AutoCastServiceImpl.java | 5 ++++- .../calls/autocasts/AutoCastUtils.java | 3 +++ .../resolve/calls/autocasts/DataFlowInfo.java | 2 +- .../quick/regressions/kt597.jet | 20 +++++++++++++++++++ idea/testData/checker/infos/Autocasts.jet | 2 +- 6 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 compiler/testData/checkerWithErrorTypes/quick/regressions/kt597.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java index 823666d4ed2..89989f037c9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java @@ -64,20 +64,8 @@ import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor scope = explicitReceiver.getType().getMemberScope(); explicitReceiver = NO_RECEIVER; } - doComputeTasks(scope, explicitReceiver, call, name, result, AutoCastService.NO_AUTO_CASTS); + doComputeTasks(scope, explicitReceiver, call, name, result, new AutoCastServiceImpl(dataFlowInfo, bindingContext)); - List receivers; - if (explicitReceiver.exists()) { - receivers = Collections.singletonList(explicitReceiver); - } - else { - receivers = Lists.newArrayList(); - scope.getImplicitReceiversHierarchy(receivers); - } - for (ReceiverDescriptor receiverToCast : receivers) { - assert receiverToCast.exists(); - doComputeTasks(scope, receiverToCast, call, name, result, new AutoCastServiceImpl(dataFlowInfo, bindingContext)); - } return result; } @@ -87,7 +75,7 @@ import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor scope.getImplicitReceiversHierarchy(implicitReceivers); if (receiver.exists()) { List variantsForExplicitReceiver = autoCastService.getVariantsForReceiver(receiver); - + Collection> extensionFunctions = convertWithImpliedThis(scope, variantsForExplicitReceiver, getExtensionsByName(scope, name)); List> nonlocals = Lists.newArrayList(); List> locals = Lists.newArrayList(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastServiceImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastServiceImpl.java index 5789419b22e..dbc350798d1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastServiceImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastServiceImpl.java @@ -1,5 +1,6 @@ package org.jetbrains.jet.lang.resolve.calls.autocasts; +import com.google.common.collect.Lists; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; @@ -21,7 +22,9 @@ public class AutoCastServiceImpl implements AutoCastService { @NotNull @Override public List getVariantsForReceiver(@NotNull ReceiverDescriptor receiverDescriptor) { - return AutoCastUtils.getAutoCastVariants(bindingContext, dataFlowInfo, receiverDescriptor); + List variants = Lists.newArrayList(AutoCastUtils.getAutoCastVariants(bindingContext, dataFlowInfo, receiverDescriptor)); + variants.add(receiverDescriptor); + return variants; } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastUtils.java index e1b62313964..44bf37380a2 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/AutoCastUtils.java @@ -17,6 +17,9 @@ public class AutoCastUtils { private AutoCastUtils() {} + /** + * @return variants @param receiverToCast may be cast to according to @param dataFlowInfo, @param receiverToCast itself is NOT included + */ public static List getAutoCastVariants(@NotNull final BindingContext bindingContext, @NotNull final DataFlowInfo dataFlowInfo, @NotNull ReceiverDescriptor receiverToCast) { return receiverToCast.accept(new ReceiverDescriptorVisitor, Object>() { @Override diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/DataFlowInfo.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/DataFlowInfo.java index f1148ec21d2..fb3b2db7e4a 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/DataFlowInfo.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/autocasts/DataFlowInfo.java @@ -102,7 +102,7 @@ public class DataFlowInfo { boolean changed = false; changed |= putNullability(builder, a, nullabilityOfA.refine(nullabilityOfB.invert())); - changed |= putNullability(builder, b, nullabilityOfA.refine(nullabilityOfA.invert())); + changed |= putNullability(builder, b, nullabilityOfB.refine(nullabilityOfA.invert())); return changed ? new DataFlowInfo(ImmutableMap.copyOf(builder), typeInfo) : this; } diff --git a/compiler/testData/checkerWithErrorTypes/quick/regressions/kt597.jet b/compiler/testData/checkerWithErrorTypes/quick/regressions/kt597.jet new file mode 100644 index 00000000000..bb32c636edb --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/regressions/kt597.jet @@ -0,0 +1,20 @@ +//KT-597 Type inference failed +// +JDK + +fun Array?.get(i: Int) : T { + if (this != null) + return this.get(i) // <- inferred type is Any? but &T was excepted + else throw NullPointerException() +} + +fun Int?.inc() : Int { + if (this != null) + return this.inc() + else + throw NullPointerException() +} + +fun test() { + var i : Int? = 10 + var i_inc = i++ // <- expected Int?, but returns Any? +} \ No newline at end of file diff --git a/idea/testData/checker/infos/Autocasts.jet b/idea/testData/checker/infos/Autocasts.jet index d2acba03849..a5b716d9c05 100644 --- a/idea/testData/checker/infos/Autocasts.jet +++ b/idea/testData/checker/infos/Autocasts.jet @@ -216,7 +216,7 @@ fun declarationInsidePattern(x: (Any, Any)): String = when(x) { is (val a is Str fun mergeAutocasts(a: Any?) { if (a is String || a is Int) { a.compareTo("") - a.toString() + a.toString() } if (a is Int || a is String) { a.compareTo("")