diff --git a/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.fir.txt b/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.fir.txt index a7894e6c53f..7d55312bf1a 100644 --- a/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.fir.txt @@ -38,12 +38,12 @@ FILE: kotlinSam.kt } )) R|/foo1|(SAM(R|/f|)) - R|/foo2#|( = foo2@fun (x: R|kotlin/Nothing|): R|kotlin/Boolean| { + R|/foo2#|( = foo2@fun (x: ): { ^ CMP(>, R|/x|.#(Int(1))) } ) R|/foo2#|(R|/f|) - R|/foo3#|( = foo3@fun (x: R|kotlin/Nothing|): R|kotlin/Boolean| { + R|/foo3#|( = foo3@fun (x: ): { ^ CMP(>, R|/x|.#(Int(1))) } ) diff --git a/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.kt b/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.kt index 9b3f3f10ed7..083bc743415 100644 --- a/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.kt +++ b/compiler/fir/analysis-tests/testData/resolve/samConversions/kotlinSam.kt @@ -29,10 +29,10 @@ fun main() { foo1 { x -> x > 1 } foo1(f) - foo2 { x -> x > 1 } + foo2 { x -> x > 1 } foo2(f) - foo3 { x -> x > 1 } + foo3 { x -> x > 1 } foo3(f) foo4 { x -> x > 1 } diff --git a/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.fir.txt b/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.fir.txt index 34197c0b171..a8452efbadd 100644 --- a/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.fir.txt @@ -2,7 +2,7 @@ FILE: main.kt public final fun foo(m: R|MyRunnable|): R|kotlin/Unit| { } public final fun main(): R|kotlin/Unit| { - Q|JavaUsage|.R|/JavaUsage.foo*s#|( = foo@fun (x: R|kotlin/Nothing|): R|kotlin/Boolean| { + Q|JavaUsage|.R|/JavaUsage.foo*s#|( = foo@fun (x: ): { ^ CMP(>, R|/x|.#(Int(1))) } ) diff --git a/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.kt b/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.kt index 2e8f15e87fa..d98e39988da 100644 --- a/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.kt +++ b/compiler/fir/analysis-tests/testData/resolve/samConversions/notSamBecauseOfSupertype.kt @@ -19,7 +19,7 @@ fun foo(m: MyRunnable) {} fun main() { JavaUsage.foo { - x -> x > 1 + x -> x > 1 } JavaUsage.foo({ it > 1 }) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConeConstraintSystemUtilContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConeConstraintSystemUtilContext.kt index 77acc18f4e4..0120ffe90e4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConeConstraintSystemUtilContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConeConstraintSystemUtilContext.kt @@ -108,8 +108,7 @@ object ConeConstraintSystemUtilContext : ConstraintSystemUtilContext { index: Int ): TypeVariableMarker { return ConeTypeVariableForLambdaParameterType( - PostponedArgumentInputTypesResolver.TYPE_VARIABLE_NAME_PREFIX_FOR_LAMBDA_PARAMETER_TYPE + index, - index + PostponedArgumentInputTypesResolver.TYPE_VARIABLE_NAME_PREFIX_FOR_LAMBDA_PARAMETER_TYPE + index ) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt index d924b1a3052..aa54ec8f9d7 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.addSubtypeConstraintIfCompatible import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind import org.jetbrains.kotlin.types.model.typeConstructor +import org.jetbrains.kotlin.utils.addToStdlib.runIf fun Candidate.preprocessLambdaArgument( csBuilder: ConstraintSystemBuilder, @@ -120,17 +121,18 @@ private fun extractLambdaInfo( argument.returnType ?: typeVariable.defaultType - val defaultType = when (candidate?.symbol?.origin) { - FirDeclarationOrigin.DynamicScope -> ConeDynamicType.create(session) - else -> session.builtinTypes.nothingType.type + val defaultType = runIf(candidate?.symbol?.origin == FirDeclarationOrigin.DynamicScope) { ConeDynamicType.create(session) } + + val parameters = argument.valueParameters.mapIndexed { i, it -> + it.returnTypeRef.coneTypeSafe() + ?: defaultType + ?: ConeTypeVariableForLambdaParameterType("_P$i").apply { csBuilder.registerVariable(this) }.defaultType } - val parameters = argument.valueParameters.map { - it.returnTypeRef.coneTypeSafe() ?: defaultType - } - - val contextReceivers = argument.contextReceivers.map { - it.typeRef.coneTypeSafe() ?: defaultType + val contextReceivers = argument.contextReceivers.mapIndexed { i, it -> + it.typeRef.coneTypeSafe() + ?: defaultType + ?: ConeTypeVariableForLambdaParameterType("_C$i").apply { csBuilder.registerVariable(this) }.defaultType } val newTypeVariableUsed = returnType == typeVariable.defaultType diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/ConeTypeVariables.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/ConeTypeVariables.kt index 34fcf130317..5c770488347 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/ConeTypeVariables.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/ConeTypeVariables.kt @@ -11,7 +11,7 @@ import org.jetbrains.kotlin.fir.types.ConeTypeVariable import org.jetbrains.kotlin.name.SpecialNames class ConeTypeVariableForPostponedAtom(name: String) : ConeTypeVariable(name) -class ConeTypeVariableForLambdaParameterType(name: String, val index: Int) : ConeTypeVariable(name) +class ConeTypeVariableForLambdaParameterType(name: String) : ConeTypeVariable(name) class ConeTypeVariableForLambdaReturnType(val argument: FirAnonymousFunction, name: String) : ConeTypeVariable(name) class ConeTypeParameterBasedTypeVariable( diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.fir.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.fir.kt deleted file mode 100644 index c90d9f55e36..00000000000 --- a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.fir.kt +++ /dev/null @@ -1,6 +0,0 @@ -package f - -fun h(i: Int, a: Any, r: R, f: (Boolean) -> Int) = 1 -fun h(a: Any, i: Int, r: R, f: (Boolean) -> Int) = 1 - -fun test() = h(1, 1, 1, { b -> 42 }) diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt index 8978c920a84..72eee805f8c 100644 --- a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt +++ b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL package f fun h(i: Int, a: Any, r: R, f: (Boolean) -> Int) = 1 diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.fir.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.fir.kt deleted file mode 100644 index 4f7d4791c23..00000000000 --- a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.fir.kt +++ /dev/null @@ -1,9 +0,0 @@ -// !DIAGNOSTICS: -CONFLICTING_JVM_DECLARATIONS -package f - -fun h(f: (Boolean) -> R) = 1 -fun h(f: (String) -> R) = 2 - -fun test() = h{ i -> getAnswer() } - -fun getAnswer() = 42 diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt index c630f37b25b..4e88bd4e9b7 100644 --- a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt +++ b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -CONFLICTING_JVM_DECLARATIONS package f diff --git a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt index c292016c533..3046989943a 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt @@ -184,7 +184,7 @@ fun main() { ")!>select(A3(), { it }, { a -> a }) ")!>select(A3(), ")!>A3::foo1) // Should be error as `A3::foo1` is `KFunction2`, but the remaining arguments are `KFuncion1` or `Function1` - ")!>select(A3(), ")!>A3::foo1, { a -> a }, { it -> it }) + ")!>select(A3(), ")!>A3::foo1, { a -> a }, { it -> it }) // It's OK because `A3::foo2` is from companion of `A3` ")!>select(A3(), ")!>A3::foo2, { a -> a }, { it -> it }) & java.io.Serializable>")!>select(A4(), { x: Number -> "" }) @@ -192,10 +192,10 @@ fun main() { ")!>select(A2(), id { a, b, c -> a; b; c }) ")!>select(id(A3()), { it }, { a -> a }) ")!>select(A3(), id(")!>A3::foo1)) - ")!>select(A3(), ")!>A3::foo1, id { a -> a }, { it -> it }) - ")!>select(A3(), ")!>A3::foo1, { a -> a }, id { it -> it }) - ")!>select(id(A3()), id(")!>A3::foo1), { a -> a }, { it -> it }) - ")!>select(id(A3()), id(")!>A3::foo1), { a -> a }, id { it -> it }) + ")!>select(A3(), ")!>A3::foo1, id { a -> a }, { it -> it }) + ")!>select(A3(), ")!>A3::foo1, { a -> a }, id { it -> it }) + ")!>select(id(A3()), id(")!>A3::foo1), { a -> a }, { it -> it }) + ")!>select(id(A3()), id(")!>A3::foo1), { a -> a }, id { it -> it }) // If lambdas' parameters are specified explicitly, we don't report an error, because there is proper CST – Function ")!>select(id(A3()), id(")!>A3::foo1), { a: Number -> a }) ")!>select(id(A3()), id(")!>A3::foo1), id { a: Number -> a }) diff --git a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt index 34db412d752..a05294b978d 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt @@ -5,14 +5,14 @@ fun select(vararg x: T) = x[0] fun id(x: K) = x fun main() { - val x1 = select(id { x, y -> }, { x: Int, y -> }) + val x1 = select(id { x, y -> }, { x: Int, y -> }) val x2 = select(id { x, y -> }, { x: Int, y -> }) val x3 = select(id(fun (x, y) {}), fun (x: Int, y) {}) - val x4 = select((fun (x, y) {}), fun (x: Int, y) {}) - val x5 = select(id(fun (x, y) {}), fun (x: Int, y) {}) - val x6 = id(fun (x) {}) + val x4 = select((fun (x, y) {}), fun (x: Int, y) {}) + val x5 = select(id(fun (x, y) {}), fun (x: Int, y) {}) + val x6 = id(fun (x) {}) - select(fun (x) {}, fun (x) {}) + select(fun (x) {}, fun (x) {}) } diff --git a/compiler/testData/diagnostics/tests/inference/nonFunctionalExpectedTypeForLambdaArgument.fir.kt b/compiler/testData/diagnostics/tests/inference/nonFunctionalExpectedTypeForLambdaArgument.fir.kt index 4a0a1e59823..ec045700126 100644 --- a/compiler/testData/diagnostics/tests/inference/nonFunctionalExpectedTypeForLambdaArgument.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/nonFunctionalExpectedTypeForLambdaArgument.fir.kt @@ -4,17 +4,17 @@ fun callAny(arg: Any?) {} fun callParam(arg: T) {} fun testAny() { - callAny { error -> error } - callAny l@{ error -> error } - callAny({error -> error}) - callAny(({error -> error})) - callAny(l@{error -> error}) - callAny((l@{error -> error})) + callAny { error -> error } + callAny l@{ error -> error } + callAny({error -> error}) + callAny(({error -> error})) + callAny(l@{error -> error}) + callAny((l@{error -> error})) } fun testAnyCall() { callAny { - error -> error() + error -> error() } } diff --git a/compiler/testData/diagnostics/tests/inference/nothingType/kt34335.fir.kt b/compiler/testData/diagnostics/tests/inference/nothingType/kt34335.fir.kt deleted file mode 100644 index 0feb52c1e13..00000000000 --- a/compiler/testData/diagnostics/tests/inference/nothingType/kt34335.fir.kt +++ /dev/null @@ -1,17 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER - -fun call(vararg x: Any?) {} -fun Any.call(vararg args: Any?): R = TODO() -fun println(message: Any?) {} - -fun foo(action: (Int) -> Unit) { - action(10) -} - -fun test1() { - call({ x -> println(x::class) }) // x inside the lambda is inferred to `Nothing`, the lambda is `(Nothing) -> Unit`. -} - -fun test2() { - ::foo.call({ x -> println(x::class) }) -} diff --git a/compiler/testData/diagnostics/tests/inference/nothingType/kt34335.kt b/compiler/testData/diagnostics/tests/inference/nothingType/kt34335.kt index 6b215a320fc..fa041e833c6 100644 --- a/compiler/testData/diagnostics/tests/inference/nothingType/kt34335.kt +++ b/compiler/testData/diagnostics/tests/inference/nothingType/kt34335.kt @@ -1,4 +1,5 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER +// !DIAGNOSTICS: -UNUSED_PARAMETER, -DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE +// FIR_IDENTICAL fun call(vararg x: Any?) {} fun Any.call(vararg args: Any?): R = TODO() @@ -9,9 +10,9 @@ fun foo(action: (Int) -> Unit) { } fun test1() { - call({ x -> println(x::class) }) // x inside the lambda is inferred to `Nothing`, the lambda is `(Nothing) -> Unit`. + call({ x -> println(x::class) }) // x inside the lambda is inferred to `Nothing`, the lambda is `(Nothing) -> Unit`. } fun test2() { - ::foo.call({ x -> println(x::class) }) + ::foo.call({ x -> println(x::class) }) } diff --git a/compiler/testData/diagnostics/tests/inference/reportingImprovements/FunctionPlaceholder.fir.kt b/compiler/testData/diagnostics/tests/inference/reportingImprovements/FunctionPlaceholder.fir.kt index 82e60c60f65..a914effeb9d 100644 --- a/compiler/testData/diagnostics/tests/inference/reportingImprovements/FunctionPlaceholder.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/reportingImprovements/FunctionPlaceholder.fir.kt @@ -7,7 +7,7 @@ fun bar(f: (T) -> R) = f fun test() { foo { it } - foo { x -> x} + foo { x -> x} foo { x: Int -> x} bar { it + 1 } diff --git a/compiler/testData/diagnostics/tests/inference/resolveWithUnknownLambdaParameterType.fir.kt b/compiler/testData/diagnostics/tests/inference/resolveWithUnknownLambdaParameterType.fir.kt deleted file mode 100644 index 0ca80a96baf..00000000000 --- a/compiler/testData/diagnostics/tests/inference/resolveWithUnknownLambdaParameterType.fir.kt +++ /dev/null @@ -1,11 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_ANONYMOUS_PARAMETER - -fun baz(f: (Int) -> String) {} - -object Foo { - fun baz(vararg anys: Any?) {} - - fun testResolvedToMember() { - baz({ x -> "" }) // should be an error - } -} diff --git a/compiler/testData/diagnostics/tests/inference/resolveWithUnknownLambdaParameterType.kt b/compiler/testData/diagnostics/tests/inference/resolveWithUnknownLambdaParameterType.kt index aa0d8da79fc..3392520ade3 100644 --- a/compiler/testData/diagnostics/tests/inference/resolveWithUnknownLambdaParameterType.kt +++ b/compiler/testData/diagnostics/tests/inference/resolveWithUnknownLambdaParameterType.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_ANONYMOUS_PARAMETER fun baz(f: (Int) -> String) {} diff --git a/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt b/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt index 86b296be1dd..516a18f144d 100644 --- a/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt +++ b/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt @@ -112,6 +112,6 @@ fun testTwoLambdas() { fun f1(): (() -> Unit) -> (() -> Unit) -> Unit { return { l1 -> l1() - { l2 -> l2() } + { l2 -> l2() } } } diff --git a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.fir.kt b/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.fir.kt deleted file mode 100644 index 326b937ef3c..00000000000 --- a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.fir.kt +++ /dev/null @@ -1,9 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER - -object X1 -object X2 - -fun foo(x: T1, f: (T1) -> T1) = X1 -fun foo(xf: () -> T2, f: (T2) -> T2) = X2 - -val test: X2 = foo({ 0 }, { it -> it + 1 }) diff --git a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt b/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt index fc64260ac88..0b1a1e57f82 100644 --- a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt +++ b/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt @@ -1,4 +1,5 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER +// !DIAGNOSTICS: -UNUSED_PARAMETER, -DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE, -DEBUG_INFO_MISSING_UNRESOLVED +// FIR_IDENTICAL object X1 object X2 @@ -6,4 +7,4 @@ object X2 fun foo(x: T1, f: (T1) -> T1) = X1 fun foo(xf: () -> T2, f: (T2) -> T2) = X2 -val test: X2 = foo({ 0 }, { it -> it + 1 }) +val test: X2 = foo({ 0 }, { it -> it + 1 })