Avoid skipping lambda argument processing in case of explicit type param

#KT-38691 fixed
This commit is contained in:
Ilya Chernikov
2020-05-06 22:30:09 +02:00
parent ec98d61520
commit 7e3dfb245b
7 changed files with 72 additions and 5 deletions
@@ -11561,6 +11561,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt37650.kt");
}
@TestMetadata("kt38691.kt")
public void testKt38691() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt38691.kt");
}
@TestMetadata("kt4420.kt")
public void testKt4420() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt4420.kt");
@@ -20,11 +20,10 @@ import org.jetbrains.kotlin.builtins.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor
import org.jetbrains.kotlin.resolve.calls.inference.model.ArgumentConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.LHSArgumentConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableForLambdaReturnType
import org.jetbrains.kotlin.resolve.calls.inference.model.*
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.typeUtil.builtIns
@@ -63,8 +62,20 @@ private fun preprocessLambdaArgument(
forceResolution: Boolean = false,
returnTypeVariable: TypeVariableForLambdaReturnType? = null
): ResolvedAtom {
if (expectedType != null && !forceResolution && csBuilder.isTypeVariable(expectedType)) {
return LambdaWithTypeVariableAsExpectedTypeAtom(argument, expectedType)
if (expectedType != null && !forceResolution) {
// postpone lambda processing if expected type is a type variable that could be fixed into something non-trivial
val expectedTypeVariableWithConstraints = csBuilder.currentStorage().notFixedTypeVariables[expectedType.constructor]
if (expectedTypeVariableWithConstraints != null) {
val explicitTypeArgument = expectedTypeVariableWithConstraints.constraints.find {
it.kind == ConstraintKind.EQUALITY && it.position.from is ExplicitTypeParameterConstraintPosition
}?.type as? KotlinType
if (explicitTypeArgument == null || explicitTypeArgument.arguments.isNotEmpty()) {
return LambdaWithTypeVariableAsExpectedTypeAtom(argument, expectedType)
}
}
}
val resolvedArgument = extractLambdaInfoFromFunctionalType(expectedType, argument, returnTypeVariable)
@@ -0,0 +1,14 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
class Inv<T>
fun <T> materializeInv() = Inv<T>()
fun <X> foo(x: Inv<X>, y: X) = materializeInv<X>()
fun <X> foo(x: Inv<X>, y: () -> X) = materializeInv<X>()
fun <R> main(fn: () -> R) {
fun bar(): R = null as R
val x1 = foo<R>(materializeInv()) { fn() } // OVERLOAD_RESOLUTION_AMBIGUITY only in NI
val x2 = foo<R>(materializeInv(), fn) // OK
val x3 = foo<R>(materializeInv(), ::bar) // OK
}
@@ -0,0 +1,14 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
class Inv<T>
fun <T> materializeInv() = Inv<T>()
fun <X> foo(x: Inv<X>, y: X) = materializeInv<X>()
fun <X> foo(x: Inv<X>, y: () -> X) = materializeInv<X>()
fun <R> main(fn: () -> R) {
fun bar(): R = null <!UNCHECKED_CAST!>as R<!>
val x1 = foo<R>(materializeInv()) { fn() } // OVERLOAD_RESOLUTION_AMBIGUITY only in NI
val x2 = foo<R>(materializeInv(), fn) // OK
val x3 = foo<R>(materializeInv(), ::bar) // OK
}
@@ -0,0 +1,13 @@
package
public fun </*0*/ X> foo(/*0*/ x: Inv<X>, /*1*/ y: () -> X): Inv<X>
public fun </*0*/ X> foo(/*0*/ x: Inv<X>, /*1*/ y: X): Inv<X>
public fun </*0*/ R> main(/*0*/ fn: () -> R): kotlin.Unit
public fun </*0*/ T> materializeInv(): Inv<T>
public final class Inv</*0*/ T> {
public constructor Inv</*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
}
@@ -11568,6 +11568,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt37650.kt");
}
@TestMetadata("kt38691.kt")
public void testKt38691() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt38691.kt");
}
@TestMetadata("kt4420.kt")
public void testKt4420() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt4420.kt");
@@ -11563,6 +11563,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt37650.kt");
}
@TestMetadata("kt38691.kt")
public void testKt38691() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt38691.kt");
}
@TestMetadata("kt4420.kt")
public void testKt4420() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/regressions/kt4420.kt");