K2: Support callable references adaptation on top-level

^KT-45989 In progress
^KT-54709 Related
^KT-55217 Fixed
This commit is contained in:
Denis.Zharkov
2022-11-25 12:28:18 +01:00
committed by Space Team
parent 8f8ea8c57f
commit dcdc48a233
13 changed files with 118 additions and 31 deletions
@@ -154,34 +154,36 @@ class FirSyntheticCallGenerator(
): FirCallableReferenceAccess? {
val argumentList = buildUnaryArgumentList(callableReferenceAccess)
val typeArguments =
val parameterTypeRef =
when {
expectedTypeRef is FirResolvedTypeRef && !expectedTypeRef.coneType.isUnitOrFlexibleUnit -> listOf(
buildTypeProjectionWithVariance {
variance = Variance.INVARIANT
typeRef = expectedTypeRef
}
)
else -> emptyList()
expectedTypeRef is FirResolvedTypeRef && !expectedTypeRef.coneType.isUnitOrFlexibleUnit -> expectedTypeRef
else -> context.session.builtinTypes.anyType
}
val callableId = SyntheticCallableId.ACCEPT_SPECIFIC_TYPE
val functionSymbol = FirSyntheticFunctionSymbol(callableId)
// fun accept(p: <parameterTypeRef>): Unit
val function =
generateMemberFunction(functionSymbol, callableId.callableName, returnType = context.session.builtinTypes.unitType).apply {
valueParameters += parameterTypeRef.toValueParameter("reference", functionSymbol, isVararg = false)
}.build()
val reference =
generateCalleeReferenceWithCandidate(
callableReferenceAccess,
idFunction,
function,
argumentList,
SyntheticCallableId.ID.callableName,
callableId.callableName,
CallKind.SyntheticIdForCallableReferencesResolution,
context,
typeArguments,
)
val fakeCallElement = buildFunctionCall {
calleeReference = reference
this.argumentList = argumentList
}
val argument = components.callCompleter.completeCall(fakeCallElement, expectedTypeRef).result.argument
return ((argument as? FirVarargArgumentsExpression)?.arguments?.get(0) ?: argument) as FirCallableReferenceAccess?
val argument = components.callCompleter.completeCall(fakeCallElement, expectedTypeRef = null).result.argument
return argument as FirCallableReferenceAccess?
}
private fun generateCalleeReferenceWithCandidate(
@@ -191,9 +193,8 @@ class FirSyntheticCallGenerator(
name: Name,
callKind: CallKind = CallKind.SyntheticSelect,
context: ResolutionContext,
typeArguments: List<FirTypeProjection> = emptyList()
): FirNamedReferenceWithCandidate {
val callInfo = generateCallInfo(callSite, name, argumentList, callKind, typeArguments)
val callInfo = generateCallInfo(callSite, name, argumentList, callKind)
val candidate = generateCandidate(callInfo, function, context)
val applicability = components.resolutionStageRunner.processCandidate(candidate, context)
if (applicability <= CandidateApplicability.INAPPLICABLE) {
@@ -224,7 +225,6 @@ class FirSyntheticCallGenerator(
name: Name,
argumentList: FirArgumentList,
callKind: CallKind,
typeArguments: List<FirTypeProjection> = emptyList()
) = CallInfo(
callSite = callSite,
callKind = callKind,
@@ -232,7 +232,7 @@ class FirSyntheticCallGenerator(
explicitReceiver = null,
argumentList = argumentList,
isImplicitInvoke = false,
typeArguments = typeArguments,
typeArguments = emptyList(),
session = session,
containingFile = components.file,
containingDeclarations = components.containingDeclarations