FIR: enforce the return type of function literals without body
Its return type should be Unit, so do not use the expected type from, e.g., parameter type.
This commit is contained in:
committed by
Mikhail Glukhikh
parent
9aaa952b39
commit
27c942a0ff
+11
-4
@@ -6,10 +6,10 @@
|
||||
package org.jetbrains.kotlin.fir.resolve.inference
|
||||
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionClassKind
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.*
|
||||
import org.jetbrains.kotlin.fir.declarations.FirAnonymousFunction
|
||||
import org.jetbrains.kotlin.fir.declarations.FirClass
|
||||
import org.jetbrains.kotlin.fir.originalForSubstitutionOverride
|
||||
import org.jetbrains.kotlin.fir.expressions.FirReturnExpression
|
||||
import org.jetbrains.kotlin.fir.resolve.*
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.Candidate
|
||||
import org.jetbrains.kotlin.fir.scopes.FakeOverrideTypeCalculator
|
||||
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
@@ -248,7 +247,15 @@ fun extractLambdaInfoFromFunctionalType(
|
||||
if (!expectedType.isBuiltinFunctionalType(session)) return null
|
||||
|
||||
val receiverType = argument.receiverType ?: expectedType.receiverType(session)
|
||||
val returnType = argument.returnType ?: expectedType.returnType(session) ?: return null
|
||||
val lastStatement = argument.body?.statements?.singleOrNull()
|
||||
val returnType =
|
||||
// Simply { }, i.e., function literals without body. Raw FIR added an implicit return with an implicit unit type ref.
|
||||
if (lastStatement?.source?.kind is FirFakeSourceElementKind.ImplicitReturn &&
|
||||
(lastStatement as? FirReturnExpression)?.result?.source?.kind is FirFakeSourceElementKind.ImplicitUnit
|
||||
) {
|
||||
session.builtinTypes.unitType.type
|
||||
} else
|
||||
argument.returnType ?: expectedType.returnType(session) ?: return null
|
||||
val parameters = extractLambdaParameters(expectedType, argument, expectedType.isExtensionFunctionType(session), session)
|
||||
|
||||
return ResolvedLambdaAtom(
|
||||
|
||||
@@ -10,7 +10,7 @@ fun text() {
|
||||
bar1 {1}
|
||||
bar1 {it + 1}
|
||||
|
||||
bar2 {}
|
||||
<!INAPPLICABLE_CANDIDATE!>bar2<!> {}
|
||||
bar2 {1}
|
||||
bar2 {<!UNRESOLVED_REFERENCE!>it<!>}
|
||||
<!INAPPLICABLE_CANDIDATE!>bar2<!> {it -> it}
|
||||
|
||||
@@ -14,6 +14,6 @@ fun <T : ILength> bar(a: (Int) -> T) {
|
||||
}
|
||||
|
||||
fun test() {
|
||||
foo<String> { }
|
||||
bar<Impl> { }
|
||||
<!INAPPLICABLE_CANDIDATE!>foo<!><String> { }
|
||||
<!INAPPLICABLE_CANDIDATE!>bar<!><Impl> { }
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -14,5 +14,5 @@ fun useJ(j: J) {
|
||||
}
|
||||
|
||||
fun jj() {
|
||||
useJ({})
|
||||
<!INAPPLICABLE_CANDIDATE!>useJ<!>({})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user