Minor. Extract obtainValueParametersFromResolvedLambdaAtom
This commit is contained in:
committed by
teamcityserver
parent
e3122d0c90
commit
8ff25825dd
+41
-35
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.fir.resolve.constructFunctionalTypeRef
|
|||||||
import org.jetbrains.kotlin.fir.resolve.dfa.FirControlFlowGraphReferenceImpl
|
import org.jetbrains.kotlin.fir.resolve.dfa.FirControlFlowGraphReferenceImpl
|
||||||
import org.jetbrains.kotlin.fir.resolve.dfa.unwrapSmartcastExpression
|
import org.jetbrains.kotlin.fir.resolve.dfa.unwrapSmartcastExpression
|
||||||
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeLocalVariableNoTypeOrInitializer
|
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeLocalVariableNoTypeOrInitializer
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.inference.ResolvedLambdaAtom
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.extractLambdaInfoFromFunctionalType
|
import org.jetbrains.kotlin.fir.resolve.inference.extractLambdaInfoFromFunctionalType
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.isSuspendFunctionType
|
import org.jetbrains.kotlin.fir.resolve.inference.isSuspendFunctionType
|
||||||
import org.jetbrains.kotlin.fir.resolve.mode
|
import org.jetbrains.kotlin.fir.resolve.mode
|
||||||
@@ -724,41 +725,9 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
var lambda = anonymousFunction
|
var lambda = anonymousFunction
|
||||||
val valueParameters = when (resolvedLambdaAtom) {
|
val valueParameters = when {
|
||||||
null -> lambda.valueParameters
|
resolvedLambdaAtom != null -> obtainValueParametersFromResolvedLambdaAtom(resolvedLambdaAtom, lambda)
|
||||||
else -> {
|
else -> lambda.valueParameters
|
||||||
val singleParameterType = resolvedLambdaAtom.parameters.singleOrNull()
|
|
||||||
when {
|
|
||||||
lambda.valueParameters.isEmpty() && singleParameterType != null -> {
|
|
||||||
val name = Name.identifier("it")
|
|
||||||
val itParam = buildValueParameter {
|
|
||||||
source = lambda.source?.fakeElement(FirFakeSourceElementKind.ItLambdaParameter)
|
|
||||||
moduleData = session.moduleData
|
|
||||||
origin = FirDeclarationOrigin.Source
|
|
||||||
returnTypeRef = singleParameterType.toFirResolvedTypeRef()
|
|
||||||
this.name = name
|
|
||||||
symbol = FirValueParameterSymbol(name)
|
|
||||||
isCrossinline = false
|
|
||||||
isNoinline = false
|
|
||||||
isVararg = false
|
|
||||||
}
|
|
||||||
listOf(itParam)
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
lambda.valueParameters.mapIndexed { index, param ->
|
|
||||||
if (param.returnTypeRef is FirResolvedTypeRef) {
|
|
||||||
param
|
|
||||||
} else {
|
|
||||||
val resolvedType =
|
|
||||||
param.returnTypeRef.resolvedTypeFromPrototype(resolvedLambdaAtom.parameters[index])
|
|
||||||
param.replaceReturnTypeRef(resolvedType)
|
|
||||||
param
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val returnTypeRefFromResolvedAtom =
|
val returnTypeRefFromResolvedAtom =
|
||||||
resolvedLambdaAtom?.returnType?.let { lambda.returnTypeRef.resolvedTypeFromPrototype(it) }
|
resolvedLambdaAtom?.returnType?.let { lambda.returnTypeRef.resolvedTypeFromPrototype(it) }
|
||||||
@@ -822,6 +791,43 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
return lambda.addReturn()
|
return lambda.addReturn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun obtainValueParametersFromResolvedLambdaAtom(
|
||||||
|
resolvedLambdaAtom: ResolvedLambdaAtom,
|
||||||
|
lambda: FirAnonymousFunction,
|
||||||
|
): List<FirValueParameter> {
|
||||||
|
val singleParameterType = resolvedLambdaAtom.parameters.singleOrNull()
|
||||||
|
return when {
|
||||||
|
lambda.valueParameters.isEmpty() && singleParameterType != null -> {
|
||||||
|
val name = Name.identifier("it")
|
||||||
|
val itParam = buildValueParameter {
|
||||||
|
source = lambda.source?.fakeElement(FirFakeSourceElementKind.ItLambdaParameter)
|
||||||
|
moduleData = session.moduleData
|
||||||
|
origin = FirDeclarationOrigin.Source
|
||||||
|
returnTypeRef = singleParameterType.toFirResolvedTypeRef()
|
||||||
|
this.name = name
|
||||||
|
symbol = FirValueParameterSymbol(name)
|
||||||
|
isCrossinline = false
|
||||||
|
isNoinline = false
|
||||||
|
isVararg = false
|
||||||
|
}
|
||||||
|
listOf(itParam)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
lambda.valueParameters.mapIndexed { index, param ->
|
||||||
|
if (param.returnTypeRef is FirResolvedTypeRef) {
|
||||||
|
param
|
||||||
|
} else {
|
||||||
|
val resolvedType =
|
||||||
|
param.returnTypeRef.resolvedTypeFromPrototype(resolvedLambdaAtom.parameters[index])
|
||||||
|
param.replaceReturnTypeRef(resolvedType)
|
||||||
|
param
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun FirAnonymousFunction.addReturn(): FirAnonymousFunction {
|
private fun FirAnonymousFunction.addReturn(): FirAnonymousFunction {
|
||||||
// If this lambda's resolved, expected return type is Unit, we don't need an explicit return statement.
|
// If this lambda's resolved, expected return type is Unit, we don't need an explicit return statement.
|
||||||
// During conversion (to backend IR), the last expression will be coerced to Unit if needed.
|
// During conversion (to backend IR), the last expression will be coerced to Unit if needed.
|
||||||
|
|||||||
Reference in New Issue
Block a user