From ac0a1d984fa4f8345fa592b56324f82a2e9a7735 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 25 Apr 2019 12:47:15 +0300 Subject: [PATCH] [NI] Don't return resulting call if return type has uninferred types --- .../OverloadResolutionResultsUtil.java | 21 ++++++++++++------- .../tower/KotlinToResolvedCallTransformer.kt | 7 +++++++ .../diagnostics/tests/ResolveToJava.kt | 2 +- .../resolve/withGenericFun.ni.txt | 2 +- .../differentDelegatedExpressions.kt | 4 ++-- ...oExpectedTypeForSupertypeConstraint.ni.txt | 2 +- .../propertyDefferedType.ni.txt | 2 +- .../diagnostics/tests/generics/kt30590.ni.txt | 2 +- .../typeInferenceExpectedTypeMismatch.kt | 2 +- .../genericNestedClass.ni.txt | 4 ++-- .../cantBeInferred.ni.txt | 4 ++-- .../recursiveGetter.ni.txt | 4 ++-- .../unsupportedInferenceFromGetters.ni.txt | 4 ++-- .../diagnostics/tests/regressions/kt10843.kt | 2 +- .../constructorCallType.ni.txt | 2 +- ...asConstructorTypeArgumentsInference.ni.txt | 2 +- 16 files changed, 40 insertions(+), 26 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadResolutionResultsUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadResolutionResultsUtil.java index f871275d376..28ccaa8282f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadResolutionResultsUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadResolutionResultsUtil.java @@ -19,13 +19,14 @@ package org.jetbrains.kotlin.resolve.calls.results; import com.google.common.collect.Lists; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.config.LanguageFeature; import org.jetbrains.kotlin.descriptors.CallableDescriptor; import org.jetbrains.kotlin.resolve.calls.context.ContextDependency; import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext; import org.jetbrains.kotlin.resolve.calls.model.MutableResolvedCall; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.calls.tower.KotlinToResolvedCallTransformerKt; +import org.jetbrains.kotlin.resolve.calls.tower.NewResolvedCallImpl; +import org.jetbrains.kotlin.resolve.calls.tower.NewVariableAsFunctionResolvedCallImpl; import org.jetbrains.kotlin.types.KotlinType; import java.util.Collection; @@ -56,15 +57,21 @@ public class OverloadResolutionResultsUtil { ) { if (results.isSingleResult() && context.contextDependency == ContextDependency.INDEPENDENT) { ResolvedCall resultingCall = results.getResultingCall(); - if (!context.languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) { - if (!((MutableResolvedCall) resultingCall).hasInferredReturnType()) { - return null; - } + NewResolvedCallImpl newResolvedCall; + if (resultingCall instanceof NewVariableAsFunctionResolvedCallImpl) { + newResolvedCall = ((NewVariableAsFunctionResolvedCallImpl) resultingCall).getFunctionCall(); } - else { - if (KotlinToResolvedCallTransformerKt.isNewNotCompleted(resultingCall)) { + else if (resultingCall instanceof NewResolvedCallImpl) { + newResolvedCall = (NewResolvedCallImpl) resultingCall; + } else { + newResolvedCall = null; + } + if (newResolvedCall != null) { + if (!KotlinToResolvedCallTransformerKt.hasInferredReturnType(newResolvedCall)) { return null; } + } else if (!((MutableResolvedCall) resultingCall).hasInferredReturnType()) { + return null; } } return results.isSingleResult() ? results.getResultingCall() : null; diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index e217d312efe..bc004251dd5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -764,3 +764,10 @@ fun ResolvedCall<*>.isNewNotCompleted(): Boolean { } return false } + +fun NewResolvedCallImpl<*>.hasInferredReturnType(): Boolean { + if (isNewNotCompleted()) return false + + val returnType = this.resultingDescriptor.returnType ?: return false + return !returnType.contains { ErrorUtils.isUninferredParameter(it) } +} diff --git a/compiler/testData/diagnostics/tests/ResolveToJava.kt b/compiler/testData/diagnostics/tests/ResolveToJava.kt index fe0b5775408..75828e1f1f0 100644 --- a/compiler/testData/diagnostics/tests/ResolveToJava.kt +++ b/compiler/testData/diagnostics/tests/ResolveToJava.kt @@ -21,7 +21,7 @@ fun test(l : java.util val f : java.io.File? = null - Collections.emptyList + Collections.emptyList Collections.emptyList Collections.emptyList() Collections.emptyList() diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/withGenericFun.ni.txt b/compiler/testData/diagnostics/tests/callableReference/resolve/withGenericFun.ni.txt index 556cd82d2a9..b4aee01f697 100644 --- a/compiler/testData/diagnostics/tests/callableReference/resolve/withGenericFun.ni.txt +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/withGenericFun.ni.txt @@ -2,7 +2,7 @@ package public val x1: kotlin.Unit public val x2: kotlin.Unit -public val x3: ??? +public val x3: [ERROR : Type for apply(true, ::foo)] public fun apply(/*0*/ x: T, /*1*/ f: (T) -> R): R public fun foo(/*0*/ i: kotlin.Int): kotlin.Unit public fun foo(/*0*/ s: kotlin.String): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/differentDelegatedExpressions.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/differentDelegatedExpressions.kt index fb7a9e02f5d..f41be83d563 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/differentDelegatedExpressions.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/differentDelegatedExpressions.kt @@ -12,8 +12,8 @@ class A(outer: Outer) { var b: String by foo(getMyProperty()) var r: String by foo(outer.getContainer().getMyProperty()) - var e: String by + foo(getMyProperty()) - var f: String by foo(getMyProperty()) - 1 + var e: String by + foo(getMyProperty()) + var f: String by foo(getMyProperty()) - 1 } fun foo(a: Any?) = MyProperty() diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.ni.txt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.ni.txt index 316fad2cbc7..fe4e1259365 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.ni.txt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.ni.txt @@ -2,7 +2,7 @@ package public final class A { public constructor A() - public final var a: ??? + public final var a: [ERROR : ] 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/delegatedProperty/propertyDefferedType.ni.txt b/compiler/testData/diagnostics/tests/delegatedProperty/propertyDefferedType.ni.txt index 8954c72da71..02fcb832d25 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/propertyDefferedType.ni.txt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/propertyDefferedType.ni.txt @@ -2,7 +2,7 @@ package public final class B { public constructor B() - public final val c: kotlin.Int + public final val c: [ERROR : ] 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/generics/kt30590.ni.txt b/compiler/testData/diagnostics/tests/generics/kt30590.ni.txt index a77bc0861ab..7da0b4336a1 100644 --- a/compiler/testData/diagnostics/tests/generics/kt30590.ni.txt +++ b/compiler/testData/diagnostics/tests/generics/kt30590.ni.txt @@ -1,7 +1,7 @@ package public fun emptyStrangeMap(): kotlin.collections.Map -public fun test(): kotlin.collections.Map +public fun test(): [ERROR : Error function type] public fun test7(): kotlin.collections.Map public interface A { diff --git a/compiler/testData/diagnostics/tests/inference/typeInferenceExpectedTypeMismatch.kt b/compiler/testData/diagnostics/tests/inference/typeInferenceExpectedTypeMismatch.kt index bbcb9218d75..8eb209a6b29 100644 --- a/compiler/testData/diagnostics/tests/inference/typeInferenceExpectedTypeMismatch.kt +++ b/compiler/testData/diagnostics/tests/inference/typeInferenceExpectedTypeMismatch.kt @@ -4,7 +4,7 @@ package typeInferenceExpectedTypeMismatch import java.util.* fun test() { - val s : Set = newList() + val s : Set = newList() use(s) } diff --git a/compiler/testData/diagnostics/tests/inner/qualifiedExpression/genericNestedClass.ni.txt b/compiler/testData/diagnostics/tests/inner/qualifiedExpression/genericNestedClass.ni.txt index 3af7e9b58f1..817b72a1d17 100644 --- a/compiler/testData/diagnostics/tests/inner/qualifiedExpression/genericNestedClass.ni.txt +++ b/compiler/testData/diagnostics/tests/inner/qualifiedExpression/genericNestedClass.ni.txt @@ -1,8 +1,8 @@ package -public fun manyArguments(): Outer.Nested +public fun manyArguments(): [ERROR : Error function type] public fun nested(): Outer.Nested -public fun noArguments(): Outer.Nested +public fun noArguments(): [ERROR : Error function type] public fun noArgumentsExpectedType(): Outer.Nested public final class Outer { diff --git a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/cantBeInferred.ni.txt b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/cantBeInferred.ni.txt index 53d40b1cdf9..74f05e17888 100644 --- a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/cantBeInferred.ni.txt +++ b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/cantBeInferred.ni.txt @@ -1,6 +1,6 @@ package -public val x: ??? -public val y: kotlin.collections.List +public val x: [ERROR : Error function type] +public val y: [ERROR : Error function type] public fun bar(): kotlin.collections.List public fun foo(): E diff --git a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.ni.txt b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.ni.txt index da9b6ba8875..83c24b9000a 100644 --- a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.ni.txt +++ b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.ni.txt @@ -10,8 +10,8 @@ public final class A { public final val b: [ERROR : Error function type] public final val u: [ERROR : Error function type] public final val y: [ERROR : Error function type] - public final val z1: ??? - public final val z2: kotlin.collections.List + public final val z1: [ERROR : Error function type] + public final val z2: [ERROR : Error function type] 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/properties/inferenceFromGetters/unsupportedInferenceFromGetters.ni.txt b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/unsupportedInferenceFromGetters.ni.txt index 9dc8e7e025a..0be08fa00ce 100644 --- a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/unsupportedInferenceFromGetters.ni.txt +++ b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/unsupportedInferenceFromGetters.ni.txt @@ -2,12 +2,12 @@ package public val u: kotlin.String public val x: [ERROR : No type, no body] -public val x1: ??? +public val x1: [ERROR : Error function type] public val x2: kotlin.String public var x3: kotlin.Int public val x4: [ERROR : Error function type] public val x5: kotlin.Nothing? -public val y1: kotlin.collections.List +public val y1: [ERROR : Error function type] public val y2: kotlin.collections.List public val y5: kotlin.Nothing public val z2: kotlin.collections.List diff --git a/compiler/testData/diagnostics/tests/regressions/kt10843.kt b/compiler/testData/diagnostics/tests/regressions/kt10843.kt index 3992bd25473..0f7a68de53c 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt10843.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt10843.kt @@ -1,7 +1,7 @@ // !WITH_NEW_INFERENCE // NI_EXPECTED_FILE // See EA-76890 / KT-10843: NPE during analysis -fun lambda(x : Int?) = x?.let l { +fun lambda(x : Int?) = x?.let l { y -> if (y > 0) return@l x y diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/constructorCallType.ni.txt b/compiler/testData/diagnostics/tests/secondaryConstructors/constructorCallType.ni.txt index 18da66c17a4..78dcbf37e8b 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/constructorCallType.ni.txt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/constructorCallType.ni.txt @@ -4,7 +4,7 @@ public val x1: A public val x2: A public val x3: A public val y1: B -public val y10: B +public val y10: [ERROR : Type for B("")] public val y2: B public val y3: B public val y4: B diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.ni.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.ni.txt index 8214d295a05..a9b012a6f4d 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.ni.txt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.ni.txt @@ -3,7 +3,7 @@ package public val test0: N /* = Num */ public val test1: N /* = Num */ public val test2: C /* = Cons */ -public val test3: CC /* = Cons> */ +public val test3: [ERROR : Type for CC(1, 2)] public val test4: CC /* = Cons> */ public val test5: PL /* = Pair> */ public fun testProjections1(/*0*/ x: Pair): Foo