[NI] Convert extension lambda to the non-extension one, if needed

#KT-30245 fixed
This commit is contained in:
Ilya Chernikov
2019-12-04 12:25:28 +01:00
parent ef5fe0675a
commit caa677e6d2
8 changed files with 394 additions and 23 deletions
@@ -17578,6 +17578,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/regressions/kt302.kt");
}
@TestMetadata("kt30245.kt")
public void testKt30245() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt30245.kt");
}
@TestMetadata("kt306.kt")
public void testKt306() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt306.kt");
@@ -118,16 +118,51 @@ private fun extractLambdaInfoFromFunctionalType(
returnTypeVariable: TypeVariableForLambdaReturnType? = null
): ResolvedLambdaAtom? {
if (expectedType == null || !expectedType.isBuiltinFunctionalType) return null
val parameters = extractLambdaParameters(expectedType, argument)
val parametersTypes = argument.parametersTypes
val expectedParameters = expectedType.getValueParameterTypesFromFunctionType()
val expectedReceiver = expectedType.getReceiverTypeFromFunctionType()?.unwrap()
val argumentAsFunctionExpression = argument.safeAs<FunctionExpression>()
val receiverType = argumentAsFunctionExpression?.receiverType ?: expectedType.getReceiverTypeFromFunctionType()?.unwrap()
val receiverFromExpected = argumentAsFunctionExpression?.receiverType == null && expectedReceiver != null
fun UnwrappedType?.orExpected(index: Int) =
this ?: expectedParameters.getOrNull(index)?.type?.unwrap() ?: expectedType.builtIns.nullableAnyType
// Extracting parameters and receiver type, taking into account the actual lambda definition and expected lambda type
val (parameters, receiver) = when {
argumentAsFunctionExpression != null ->
// lambda has explicit functional type - use types from it if available
(parametersTypes?.mapIndexed { index, type ->
type.orExpected(index)
} ?: emptyList()) to argumentAsFunctionExpression.receiverType
(parametersTypes?.size ?: 0) == expectedParameters.size && receiverFromExpected ->
// expected type has receiver, but arguments sizes are the same in actual and expected, so assuming missing (maybe unused) receiver in lambda
// TODO: in case of implicit parameters in lambda ("this" and "it") this case assumes "this", probably we should generate two possible overloads and choose among them later
(parametersTypes?.mapIndexed { index, type ->
type.orExpected(index)
} ?: expectedParameters.map { it.type.unwrap() }) to expectedReceiver
(parametersTypes?.size ?: 0) - expectedParameters.size == 1 && receiverFromExpected -> {
// one "missing" parameter in the expected parameters - first lambda parameter should be mapped to expected receiver
// TODO: same "this" or "it" case from above could be applicable here as well
(parametersTypes?.mapIndexed { index, type ->
type ?: run {
if (index == 0) expectedReceiver?.unwrap()
else expectedParameters.getOrNull(index - 1)?.type?.unwrap()
} ?: expectedType.builtIns.nullableAnyType
} ?: expectedParameters.map { it.type.unwrap() }) to null
}
else ->
(parametersTypes?.mapIndexed { index, type ->
type.orExpected(index)
} ?: expectedParameters.map { it.type.unwrap() }) to (if (receiverFromExpected) expectedReceiver else null)
}
val returnType = argumentAsFunctionExpression?.returnType ?: expectedType.getReturnTypeFromFunctionType().unwrap()
return ResolvedLambdaAtom(
argument,
expectedType.isSuspendFunctionType,
receiverType,
receiver,
parameters,
returnType,
typeVariableForLambdaReturnType = returnTypeVariable,
@@ -135,18 +170,6 @@ private fun extractLambdaInfoFromFunctionalType(
)
}
private fun extractLambdaParameters(expectedType: UnwrappedType, argument: LambdaKotlinCallArgument): List<UnwrappedType> {
val parametersTypes = argument.parametersTypes
val expectedParameters = expectedType.getValueParameterTypesFromFunctionType()
if (parametersTypes == null) {
return expectedParameters.map { it.type.unwrap() }
}
return parametersTypes.mapIndexed { index, type ->
type ?: expectedParameters.getOrNull(index)?.type?.unwrap() ?: expectedType.builtIns.nullableAnyType
}
}
fun LambdaWithTypeVariableAsExpectedTypeAtom.transformToResolvedLambda(
csBuilder: ConstraintSystemBuilder,
expectedType: UnwrappedType? = null,
@@ -159,7 +182,7 @@ fun LambdaWithTypeVariableAsExpectedTypeAtom.transformToResolvedLambda(
atom,
fixedExpectedType,
forceResolution = true,
returnTypeVariable
returnTypeVariable = returnTypeVariable
) as ResolvedLambdaAtom
setAnalyzed(resolvedLambdaAtom)
@@ -5,10 +5,12 @@
package org.jetbrains.kotlin.resolve.calls.components
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType
import org.jetbrains.kotlin.builtins.getValueParameterTypesFromFunctionType
import org.jetbrains.kotlin.builtins.isBuiltinFunctionalType
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument
import org.jetbrains.kotlin.resolve.calls.inference.model.*
@@ -72,6 +74,8 @@ class PostponedArgumentsAnalyzer(
fun substitute(type: UnwrappedType) = currentSubstitutor.safeSubstitute(c, type) as UnwrappedType
// Expected type has a higher priority against which lambda should be analyzed
// Mostly, this is needed to report more specific diagnostics on lambda parameters
fun expectedOrActualType(expected: UnwrappedType?, actual: UnwrappedType?): UnwrappedType? {
val expectedSubstituted = expected?.let(::substitute)
return if (expectedSubstituted != null && c.canBeProper(expectedSubstituted)) expectedSubstituted else actual?.let(::substitute)
@@ -79,14 +83,22 @@ class PostponedArgumentsAnalyzer(
val builtIns = c.getBuilder().builtIns
// Expected type has a higher priority against which lambda should be analyzed
// Mostly, this is needed to report more specific diagnostics on lambda parameters
val receiver = expectedOrActualType(lambda.expectedType.receiver(), lambda.receiver)
val expectedParameters = lambda.expectedType.valueParameters()
val expectedReceiver = lambda.expectedType.receiver()
val receiver = lambda.receiver?.let {
expectedOrActualType(expectedReceiver ?: expectedParameters?.getOrNull(0), lambda.receiver)
}
val expectedParametersToMatchAgainst = when {
receiver == null && expectedReceiver != null && expectedParameters != null -> listOf(expectedReceiver) + expectedParameters
receiver == null && expectedReceiver != null -> listOf(expectedReceiver)
receiver != null && expectedReceiver == null -> expectedParameters?.drop(1)
else -> expectedParameters
}
val parameters =
expectedParameters?.mapIndexed { index, expected ->
expectedParametersToMatchAgainst?.mapIndexed { index, expected ->
expectedOrActualType(expected, lambda.parameters.getOrNull(index)) ?: builtIns.nothingType
} ?: lambda.parameters.map(::substitute)
@@ -101,13 +113,18 @@ class PostponedArgumentsAnalyzer(
else -> null
}
val convertedAnnotations = lambda.expectedType?.annotations?.let { annotations ->
if (receiver != null || expectedReceiver == null) annotations
else FilteredAnnotations(annotations, true) { it != KotlinBuiltIns.FQ_NAMES.extensionFunctionType }
}
val (returnArguments, inferenceSession) = resolutionCallbacks.analyzeAndGetLambdaReturnArguments(
lambda.atom,
lambda.isSuspend,
receiver,
parameters,
expectedTypeForReturnArguments,
lambda.expectedType?.annotations ?: Annotations.EMPTY,
convertedAnnotations ?: Annotations.EMPTY,
stubsForPostponedVariables.cast()
)
@@ -0,0 +1,125 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_PARAMETER -UNUSED_EXPRESSION
class Sample
fun <K> id(x: K): K = x
fun test() {
val f00: Sample.() -> Unit = id { val a = 1 }
val f01: Sample.() -> Unit = id { s: Sample -> }
val f02: Sample.() -> Unit = id<Sample.() -> Unit> { s: Sample -> }
}
typealias E0 = Int.() -> Int
class W1(val f: E0) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: () -> Int) : this(fun Int.(): Int = f() )
}
typealias E1 = Int.(String) -> Int
class W2(val f: E1) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: Int.() -> Int) : this(fun Int.(String): Int = f())
}
typealias L1 = (Int) -> Int
class W3(val f: L1) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: () -> Int) : this( { i: Int -> f() } )
}
typealias L2 = (Int, String) -> Int
class W4(val f: L2) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: L1) : this( { i: Int, s: String -> f(i) } )
}
fun test1() { // to extension lambda 0
val w10 = W1 { this } // oi+ ni+
val i10: E0 = id { this } // o1- ni+
val j10 = id<E0> { this } // oi+ ni+
val f10 = W1(fun Int.(): Int = this) // oi+ ni+
val g10: E0 = id(fun Int.(): Int = this) // oi+ ni+
val w11 = W1 { i: Int -> i } // oi- ni+
val i11: E0 = id { i: Int -> i } // o1+ ni+
val w12 = W1 { i -> i } // oi- ni+
val i12: E0 = id { i -> i } // oi- ni+
val j12 = id<E0> { i -> i } // oi- ni+
// yet unsupported cases - considering lambdas as extension ones unconditionally
// val w13 = W1 { it } // this or it: oi- ni-
// val i13: E0 = id { it } // this or it: oi- ni-
// val j13 = id<E0> { it } // this or it: oi- ni-
val o14 = W1 { -> 0 } // oi+ ni+
}
fun test2() { // to extension lambda 1
val w20 = W2 { this + it.length } // oi+ ni+
val i20: E1 = id { this + it.length } // oi- ni+
val w21 = W2 { this } // oi+ ni+
val i21: E1 = id { this } // oi- ni+
val f21 = W2(fun Int.(String): Int = this) // oi+ ni+
val g21: E1 = id(fun Int.(String): Int = this) // oi+ ni+
val w22 = W2 { s -> this + s.length } // oi+ ni+
val i22: E1 = id { s -> this + s.length } // oi+ ni+
val w23 = W2 { s -> s.length } // oi+ ni+
val i23: E1 = id { s -> s.length } // oi+ ni+
val w24 = W2 { s: String -> this + s.length } // oi+ ni+
// val i24: E1 = id { s: String -> this + s.length } //oi- ni-
val w25 = W2 { s: String -> s.length } // oi+ ni+
// val i25: E1 = id { s: String -> s.length } // oi- ni-
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val w26 = W2 { i, s -> i + s.length } // overload oi- ni-
// val i26: E1 = id { i, s -> i + s.length } // overload oi- ni-
// val w27 = W2 { i, s: String -> i + s.length } // overload oi- ni-
// val i27: E1 = id { i, s: String -> i + s.length } // overload oi- ni-
val w28 = W2 { i: Int, s -> i + s.length } // oi- ni+
val i28: E1 = id { i: Int, s -> i + s.length } // oi- ni+
val w29 = W2 { i: Int, s: String -> i + s.length } // oi- ni+
val i29: E1 = id { i: Int, s: String -> i + s.length } // oi+ ni+
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val o2a = W2 { i: Int -> i } // overload oi- ni+
// val o2b = W2 { i -> i } // overload oi- ni-
}
fun test3() { // to non-extension lambda 1
val w30 = W3 { i -> i } // oi+ ni+
val i30: L1 = id { i -> i } // oi+ ni+
val w31 = W3 { it } // oi+ ni+
val i31: L1 = id { it } // oi- ni+
val j31 = id<L1> { it } // oi+ ni+
// yet unsupported cases - considering lambdas as extension ones unconditionally
// val w32 = W3 { this } // this or it: oi- ni-
// val i32: L1 = id { this } // this or it: oi- ni-
// val j32 = id<L1> { this } // this or it: oi- ni-
val w33 = W3(fun Int.(): Int = this) // oi- ni+
val i33: L1 = id(fun Int.(): Int = this) // oi+ ni+
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val o34 = W3 { -> 1 } // overload oi- ni-
}
fun test4() { // to non-extension lambda 2
val w30 = W4 { i, s -> i } // oi+ ni+
val i30: L2 = id { i, s -> i } // oi+ ni+
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val w31 = W4 { this } // overload oi- ni-
// val i31: L2 = id { this } // overload oi- ni-
// val w32 = W4 { this + it.length } // overload oi- ni-
// val i32: L2 = id { this + it.length } // overload oi- ni-
}
open class A(a: () -> Unit) {
constructor(f: (String) -> Unit) : this({ -> f("") })
}
class B: A({ s -> "1" })
@@ -0,0 +1,125 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_PARAMETER -UNUSED_EXPRESSION
class Sample
fun <K> id(x: K): K = x
fun test() {
val f00: Sample.() -> Unit = id { val a = 1 }
val f01: Sample.() -> Unit = id { s: Sample -> }
val f02: Sample.() -> Unit = id<Sample.() -> Unit> { s: Sample -> }
}
typealias E0 = Int.() -> Int
class W1(val f: E0) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: () -> Int) : this(fun Int.(): Int = f() )
}
typealias E1 = Int.(String) -> Int
class W2(val f: E1) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: Int.() -> Int) : this(fun Int.(String): Int = f())
}
typealias L1 = (Int) -> Int
class W3(val f: L1) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: () -> Int) : this( { i: Int -> f() } )
}
typealias L2 = (Int, String) -> Int
class W4(val f: L2) {
// overload ambiguity is not supported yet - see commented examples with "overload" keyword below
// constructor(f: L1) : this( { i: Int, s: String -> f(i) } )
}
fun test1() { // to extension lambda 0
val w10 = W1 { this } // oi+ ni+
val i10: E0 = id { this } // o1- ni+
val j10 = id<E0> { this } // oi+ ni+
val f10 = W1(fun Int.(): Int = this) // oi+ ni+
val g10: E0 = id(fun Int.(): Int = this) // oi+ ni+
val w11 = W1 { i: Int -> i } // oi- ni+
val i11: E0 = id { i: Int -> i } // o1+ ni+
val w12 = W1 { i -> i } // oi- ni+
val i12: E0 = id { i -> i } // oi- ni+
val j12 = id<E0> { i -> i } // oi- ni+
// yet unsupported cases - considering lambdas as extension ones unconditionally
// val w13 = W1 { it } // this or it: oi- ni-
// val i13: E0 = id { it } // this or it: oi- ni-
// val j13 = id<E0> { it } // this or it: oi- ni-
val o14 = W1 { -> 0 } // oi+ ni+
}
fun test2() { // to extension lambda 1
val w20 = W2 { this + it.length } // oi+ ni+
val i20: E1 = id { this + it.length } // oi- ni+
val w21 = W2 { this } // oi+ ni+
val i21: E1 = id { this } // oi- ni+
val f21 = W2(fun Int.(String): Int = this) // oi+ ni+
val g21: E1 = id(fun Int.(String): Int = this) // oi+ ni+
val w22 = W2 { s -> this + s.length } // oi+ ni+
val i22: E1 = id { s -> this + s.length } // oi+ ni+
val w23 = W2 { s -> s.length } // oi+ ni+
val i23: E1 = id { s -> s.length } // oi+ ni+
val w24 = W2 { s: String -> this + s.length } // oi+ ni+
// val i24: E1 = id { s: String -> this + s.length } //oi- ni-
val w25 = W2 { s: String -> s.length } // oi+ ni+
// val i25: E1 = id { s: String -> s.length } // oi- ni-
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val w26 = W2 { i, s -> i + s.length } // overload oi- ni-
// val i26: E1 = id { i, s -> i + s.length } // overload oi- ni-
// val w27 = W2 { i, s: String -> i + s.length } // overload oi- ni-
// val i27: E1 = id { i, s: String -> i + s.length } // overload oi- ni-
val w28 = W2 { i: Int, s -> i + s.length } // oi- ni+
val i28: E1 = id { i: Int, s -> i + s.length } // oi- ni+
val w29 = W2 { i: Int, s: String -> i + s.length } // oi- ni+
val i29: E1 = id { i: Int, s: String -> i + s.length } // oi+ ni+
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val o2a = W2 { i: Int -> i } // overload oi- ni+
// val o2b = W2 { i -> i } // overload oi- ni-
}
fun test3() { // to non-extension lambda 1
val w30 = W3 { i -> i } // oi+ ni+
val i30: L1 = id { i -> i } // oi+ ni+
val w31 = W3 { it } // oi+ ni+
val i31: L1 = id { it } // oi- ni+
val j31 = id<L1> { it } // oi+ ni+
// yet unsupported cases - considering lambdas as extension ones unconditionally
// val w32 = W3 { this } // this or it: oi- ni-
// val i32: L1 = id { this } // this or it: oi- ni-
// val j32 = id<L1> { this } // this or it: oi- ni-
val w33 = W3(fun Int.(): Int = this) // oi- ni+
val i33: L1 = id(fun Int.(): Int = this) // oi+ ni+
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val o34 = W3 { -> 1 } // overload oi- ni-
}
fun test4() { // to non-extension lambda 2
val w30 = W4 { i, s -> i } // oi+ ni+
val i30: L2 = id { i, s -> i } // oi+ ni+
// yet unsupported cases with ambiguity for the lambda conversion (commented constructors in wrappers above)
// val w31 = W4 { this } // overload oi- ni-
// val i31: L2 = id { this } // overload oi- ni-
// val w32 = W4 { this + it.length } // overload oi- ni-
// val i32: L2 = id { this + it.length } // overload oi- ni-
}
open class A(a: () -> Unit) {
constructor(f: (String) -> Unit) : this({ -> f("") })
}
class B: A({ s -> "1" })
@@ -0,0 +1,66 @@
package
public fun </*0*/ K> id(/*0*/ x: K): K
public fun test(): kotlin.Unit
public fun test1(): kotlin.Unit
public fun test2(): kotlin.Unit
public fun test3(): kotlin.Unit
public fun test4(): kotlin.Unit
public open class A {
public constructor A(/*0*/ a: () -> kotlin.Unit)
public constructor A(/*0*/ f: (kotlin.String) -> kotlin.Unit)
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 B : A {
public constructor B()
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 Sample {
public constructor Sample()
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 W1 {
public constructor W1(/*0*/ f: E0 /* = kotlin.Int.() -> kotlin.Int */)
public final val f: E0 /* = kotlin.Int.() -> kotlin.Int */
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 W2 {
public constructor W2(/*0*/ f: E1 /* = kotlin.Int.(kotlin.String) -> kotlin.Int */)
public final val f: E1 /* = kotlin.Int.(kotlin.String) -> kotlin.Int */
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 W3 {
public constructor W3(/*0*/ f: L1 /* = (kotlin.Int) -> kotlin.Int */)
public final val f: L1 /* = (kotlin.Int) -> kotlin.Int */
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 W4 {
public constructor W4(/*0*/ f: L2 /* = (kotlin.Int, kotlin.String) -> kotlin.Int */)
public final val f: L2 /* = (kotlin.Int, kotlin.String) -> kotlin.Int */
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 typealias E0 = kotlin.Int.() -> kotlin.Int
public typealias E1 = kotlin.Int.(kotlin.String) -> kotlin.Int
public typealias L1 = (kotlin.Int) -> kotlin.Int
public typealias L2 = (kotlin.Int, kotlin.String) -> kotlin.Int
@@ -17590,6 +17590,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/regressions/kt302.kt");
}
@TestMetadata("kt30245.kt")
public void testKt30245() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt30245.kt");
}
@TestMetadata("kt306.kt")
public void testKt306() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt306.kt");
@@ -17580,6 +17580,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/regressions/kt302.kt");
}
@TestMetadata("kt30245.kt")
public void testKt30245() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt30245.kt");
}
@TestMetadata("kt306.kt")
public void testKt306() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt306.kt");