Fix error type for implicit invoke with function literal argument

#KT-11401 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-01-09 22:02:54 +03:00
parent 9ff8192aff
commit cff0865c87
22 changed files with 347 additions and 7 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.Constrain
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.RECEIVER_POSITION
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.VALUE_PARAMETER_POSITION
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ValidityConstraintForConstituentType
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.makeNullableTypeIfSafeReceiver
import org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus
import org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus.INCOMPLETE_TYPE_INFERENCE
@@ -254,17 +255,26 @@ class GenericCandidateResolver(
val resolvedCall = context.candidateCall
val constraintSystem = resolvedCall.constraintSystem?.toBuilder() ?: return
// `resolvedCall` can contain wrapped call (e.g. CallForImplicitInvoke). Meanwhile, `context` contains simple call which leads
// to inconsistency and errors in inference. See definition of `effectiveExpectedTypeInSystem` in `addConstraintForFunctionLiteralArgument`
val newContext = if (resolvedCall is VariableAsFunctionResolvedCall) {
CallCandidateResolutionContext.create(
resolvedCall, context, context.trace, context.tracing, resolvedCall.functionCall.call, context.candidateResolveMode)
} else {
context
}
// constraints for function literals
// Value parameters
for ((valueParameterDescriptor, resolvedValueArgument) in resolvedCall.valueArguments) {
for (valueArgument in resolvedValueArgument.arguments) {
valueArgument.getArgumentExpression()?.let { argumentExpression ->
ArgumentTypeResolver.getFunctionLiteralArgumentIfAny(argumentExpression, context)?.let { functionLiteral ->
addConstraintForFunctionLiteralArgument(functionLiteral, valueArgument, valueParameterDescriptor, constraintSystem, context,
ArgumentTypeResolver.getFunctionLiteralArgumentIfAny(argumentExpression, newContext)?.let { functionLiteral ->
addConstraintForFunctionLiteralArgument(functionLiteral, valueArgument, valueParameterDescriptor, constraintSystem, newContext,
resolvedCall.candidateDescriptor.returnType)
}
ArgumentTypeResolver.getCallableReferenceExpressionIfAny(argumentExpression, context)?.let { callableReference ->
addConstraintForCallableReference(callableReference, valueArgument, valueParameterDescriptor, constraintSystem, context)
ArgumentTypeResolver.getCallableReferenceExpressionIfAny(argumentExpression, newContext)?.let { callableReference ->
addConstraintForCallableReference(callableReference, valueArgument, valueParameterDescriptor, constraintSystem, newContext)
}
}
}
@@ -0,0 +1,14 @@
class TestClass {
companion object {
inline operator fun <T> invoke(task: () -> T) = task()
}
}
fun box(): String {
val test1 = TestClass { "K" }
if (test1 != "K") return "fail1, 'test1' == $test1"
val ok = "OK"
val x = TestClass { return ok }
}
@@ -0,0 +1,10 @@
class TestClass {
inline operator fun <T> invoke(task: () -> T) = task()
}
fun box(): String {
val test = TestClass()
val ok = "OK"
val x = test { return ok }
}
@@ -0,0 +1,18 @@
@kotlin.Metadata
public final class ImplicitInvokeInCompanionObjectWithFunctionalArgumentKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@kotlin.Metadata
public final class TestClass {
public final static field Companion: TestClass.Companion
inner class TestClass/Companion
public method <init>(): void
}
@kotlin.Metadata
public final static class TestClass/Companion {
inner class TestClass/Companion
private method <init>(): void
public final method invoke(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Object
}
@@ -0,0 +1,10 @@
@kotlin.Metadata
public final class ImplicitInvokeWithFunctionLiteralArgumentKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@kotlin.Metadata
public final class TestClass {
public method <init>(): void
public final method invoke(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Object
}
@@ -0,0 +1,18 @@
@kotlin.Metadata
public final class ImplicitInvokeInCompanionObjectWithFunctionalArgumentKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@kotlin.Metadata
public final class TestClass {
public final static field Companion: TestClass.Companion
inner class TestClass/Companion
public method <init>(): void
}
@kotlin.Metadata
public final static class TestClass/Companion {
inner class TestClass/Companion
private method <init>(): void
public final method invoke(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Object
}
@@ -0,0 +1,10 @@
@kotlin.Metadata
public final class ImplicitInvokeWithFunctionLiteralArgumentKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@kotlin.Metadata
public final class TestClass {
public method <init>(): void
public final method invoke(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Object
}
@@ -0,0 +1,28 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_VARIABLE
class AbstractSelector<S, I>
class SelectorFor<S>
inline operator fun <S, I> SelectorFor<S>.invoke(f: S.() -> I): AbstractSelector<S, I> = TODO()
class State(val p1: Double, val p2: () -> Int, val p3: String?)
fun test(s: SelectorFor<State>): Double {
val a = s { p1 }
a checkType { _<AbstractSelector<State, Double>>() }
val b = s { p2 }
b checkType { _<AbstractSelector<State, () -> Int>>()}
val c = s { p3 }
c checkType { _<AbstractSelector<State, String?>>() }
val d = s { }
d checkType { _<AbstractSelector<State, Unit>>() }
val e = s { return p1 }
e checkType { _<AbstractSelector<State, Nothing>>() }
<!UNREACHABLE_CODE!>return<!> null!!
}
@@ -0,0 +1,28 @@
package
public fun test(/*0*/ s: SelectorFor<State>): kotlin.Double
public operator inline fun </*0*/ S, /*1*/ I> SelectorFor<S>.invoke(/*0*/ f: S.() -> I): AbstractSelector<S, I>
public final class AbstractSelector</*0*/ S, /*1*/ I> {
public constructor AbstractSelector</*0*/ S, /*1*/ I>()
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
}
public final class SelectorFor</*0*/ S> {
public constructor SelectorFor</*0*/ S>()
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
}
public final class State {
public constructor State(/*0*/ p1: kotlin.Double, /*1*/ p2: () -> kotlin.Int, /*2*/ p3: kotlin.String?)
public final val p1: kotlin.Double
public final val p2: () -> kotlin.Int
public final val p3: kotlin.String?
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
}
@@ -0,0 +1,16 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_VARIABLE
class TestClass {
companion object {
inline operator fun <T> invoke(task: () -> T) = task()
}
}
fun test(s: String): String {
val a = TestClass { "K" }
a checkType { _<String>() }
<!UNREACHABLE_CODE!>val b =<!> TestClass { return s }
<!UNREACHABLE_CODE!>b checkType { _<Nothing>() }<!>
}
@@ -0,0 +1,18 @@
package
public fun test(/*0*/ s: kotlin.String): kotlin.String
public final class TestClass {
public constructor TestClass()
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
public companion object Companion {
private constructor Companion()
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 operator inline fun </*0*/ T> invoke(/*0*/ task: () -> T): T
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,14 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_VARIABLE
object TestClass {
inline operator fun <T> invoke(task: () -> T) = task()
}
fun test(s: String): String {
val a = TestClass { TestClass { TestClass } }
a checkType { _<TestClass>() }
<!UNREACHABLE_CODE!>val b =<!> TestClass { return s }
<!UNREACHABLE_CODE!>b checkType { _<Nothing>() }<!>
}
@@ -0,0 +1,11 @@
package
public fun test(/*0*/ s: kotlin.String): kotlin.String
public object TestClass {
private constructor TestClass()
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 operator inline fun </*0*/ T> invoke(/*0*/ task: () -> T): T
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,37 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_VARIABLE
class TestClass {
inline operator fun <T> invoke(task: () -> T) = task()
}
fun <T> test(value: T, test: TestClass): T {
<!UNREACHABLE_CODE!>val x =<!> test { return value }
<!UNREACHABLE_CODE!>x checkType { _<Nothing>() }<!>
<!UNREACHABLE_CODE!>return value<!>
}
// ---
class Future<T>
interface FutureCallback<E> {
operator fun <T> invoke(f: (E) -> T): Future<T>
}
fun test(cb: FutureCallback<String>) {
val a = cb { it[0] }
a checkType { _<Future<Char>>() }
val b = cb { it }
b checkType { _<Future<String>>() }
val c = cb {}
c checkType { _<Future<Unit>>() }
cb.let { callback ->
val d = callback { it.length }
d checkType { _<Future<Int>>() }
}
}
@@ -0,0 +1,26 @@
package
public fun test(/*0*/ cb: FutureCallback<kotlin.String>): kotlin.Unit
public fun </*0*/ T> test(/*0*/ value: T, /*1*/ test: TestClass): T
public final class Future</*0*/ T> {
public constructor Future</*0*/ T>()
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
}
public interface FutureCallback</*0*/ E> {
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 abstract operator fun </*0*/ T> invoke(/*0*/ f: (E) -> T): Future<T>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class TestClass {
public constructor TestClass()
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 operator inline fun </*0*/ T> invoke(/*0*/ task: () -> T): T
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -7744,6 +7744,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("implicitInvokeInCompanionObjectWithFunctionalArgument.kt")
public void testImplicitInvokeInCompanionObjectWithFunctionalArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeInCompanionObjectWithFunctionalArgument.kt");
doTest(fileName);
}
@TestMetadata("implicitInvokeWithFunctionLiteralArgument.kt")
public void testImplicitInvokeWithFunctionLiteralArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeWithFunctionLiteralArgument.kt");
doTest(fileName);
}
@TestMetadata("invoke.kt")
public void testInvoke() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/invoke.kt");
@@ -9922,6 +9922,30 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("implicitInvokeExtensionWithFunctionalArgument.kt")
public void testImplicitInvokeExtensionWithFunctionalArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/implicitInvokeExtensionWithFunctionalArgument.kt");
doTest(fileName);
}
@TestMetadata("implicitInvokeInCompanionObjectWithFunctionalArgument.kt")
public void testImplicitInvokeInCompanionObjectWithFunctionalArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/implicitInvokeInCompanionObjectWithFunctionalArgument.kt");
doTest(fileName);
}
@TestMetadata("implicitInvokeInObjectWithFunctionalArgument.kt")
public void testImplicitInvokeInObjectWithFunctionalArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/implicitInvokeInObjectWithFunctionalArgument.kt");
doTest(fileName);
}
@TestMetadata("implicitInvokeWithFunctionLiteralArgument.kt")
public void testImplicitInvokeWithFunctionLiteralArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/implicitInvokeWithFunctionLiteralArgument.kt");
doTest(fileName);
}
@TestMetadata("inferInFunctionLiterals.kt")
public void testInferInFunctionLiterals() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/inferInFunctionLiterals.kt");
@@ -7744,6 +7744,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("implicitInvokeInCompanionObjectWithFunctionalArgument.kt")
public void testImplicitInvokeInCompanionObjectWithFunctionalArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeInCompanionObjectWithFunctionalArgument.kt");
doTest(fileName);
}
@TestMetadata("implicitInvokeWithFunctionLiteralArgument.kt")
public void testImplicitInvokeWithFunctionLiteralArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeWithFunctionLiteralArgument.kt");
doTest(fileName);
}
@TestMetadata("invoke.kt")
public void testInvoke() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/invoke.kt");
@@ -7744,6 +7744,18 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis
doTest(fileName);
}
@TestMetadata("implicitInvokeInCompanionObjectWithFunctionalArgument.kt")
public void testImplicitInvokeInCompanionObjectWithFunctionalArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeInCompanionObjectWithFunctionalArgument.kt");
doTest(fileName);
}
@TestMetadata("implicitInvokeWithFunctionLiteralArgument.kt")
public void testImplicitInvokeWithFunctionLiteralArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeWithFunctionLiteralArgument.kt");
doTest(fileName);
}
@TestMetadata("invoke.kt")
public void testInvoke() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/invoke.kt");
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -8825,6 +8825,18 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("implicitInvokeInCompanionObjectWithFunctionalArgument.kt")
public void testImplicitInvokeInCompanionObjectWithFunctionalArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeInCompanionObjectWithFunctionalArgument.kt");
doTest(fileName);
}
@TestMetadata("implicitInvokeWithFunctionLiteralArgument.kt")
public void testImplicitInvokeWithFunctionLiteralArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/implicitInvokeWithFunctionLiteralArgument.kt");
doTest(fileName);
}
@TestMetadata("invoke.kt")
public void testInvoke() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/invoke/invoke.kt");