FIR: Support callable references for constructors

^KT-32725 In Progress
This commit is contained in:
Denis Zharkov
2019-10-24 13:03:10 +03:00
parent 1e0105d40e
commit f97f06a99a
5 changed files with 44 additions and 9 deletions
@@ -61,7 +61,7 @@ class ConeOverloadConflictResolver(
return FlatSignature(
call,
constructor.typeParameters.map { it.symbol },
call.argumentMapping!!.map { it.value.argumentType() },
computeParameterTypes(call, constructor),
//constructor.receiverTypeRef != null,
false,
constructor.valueParameters.any { it.isVararg },
@@ -78,15 +78,10 @@ class ConeOverloadConflictResolver(
}
private fun createFlatSignature(call: Candidate, function: FirSimpleFunction): FlatSignature<Candidate> {
val valueParametersTypes =
call.resultingTypeForCallableReference?.typeArguments?.map { it as ConeKotlinType }
?: (listOfNotNull<ConeKotlinType>(function.receiverTypeRef?.coneTypeUnsafe()) +
call.argumentMapping?.map { it.value.argumentType() }.orEmpty())
return FlatSignature(
call,
function.typeParameters.map { it.symbol },
valueParametersTypes,
computeParameterTypes(call, function),
function.receiverTypeRef != null,
function.valueParameters.any { it.isVararg },
function.valueParameters.count { it.defaultValue != null },
@@ -95,6 +90,15 @@ class ConeOverloadConflictResolver(
)
}
private fun computeParameterTypes(
call: Candidate,
function: FirFunction<*>
): List<ConeKotlinType> {
return (call.resultingTypeForCallableReference?.typeArguments?.map { it as ConeKotlinType }
?: (listOfNotNull<ConeKotlinType>(function.receiverTypeRef?.coneTypeUnsafe()) +
call.argumentMapping?.map { it.value.argumentType() }.orEmpty()))
}
private fun createFlatSignature(call: Candidate, variable: FirVariable<*>): FlatSignature<Candidate> {
return FlatSignature(
call,
@@ -190,7 +190,7 @@ internal object CheckCallableReferenceExpectedType : CheckerStage() {
val returnTypeRef = candidate.bodyResolveComponents.returnTypeCalculator.tryCalculateReturnType(fir)
val resultingType: ConeKotlinType = when (fir) {
is FirSimpleFunction -> createKFunctionType(fir, resultingReceiverType, returnTypeRef)
is FirFunction -> createKFunctionType(fir, resultingReceiverType, returnTypeRef)
is FirProperty -> createKPropertyType(fir, resultingReceiverType, returnTypeRef)
else -> ConeKotlinErrorType("Unknown callable kind: ${fir::class}")
}.let(candidate.substitutor::substituteOrSelf)
@@ -242,7 +242,7 @@ private fun createKPropertyType(
}
private fun createKFunctionType(
function: FirSimpleFunction,
function: FirFunction<*>,
receiverType: ConeKotlinType?,
returnTypeRef: FirResolvedTypeRef
): ConeKotlinType {
@@ -0,0 +1,10 @@
class Klass {
constructor(a: Int) {}
constructor(a: String) {}
}
fun user(f: (Int) -> Klass) {}
fun fn() {
user(::Klass)
}
@@ -0,0 +1,16 @@
FILE: constructors.kt
public final class Klass : R|kotlin/Any| {
public constructor(a: R|kotlin/Int|): R|Klass| {
super<R|kotlin/Any|>()
}
public constructor(a: R|kotlin/String|): R|Klass| {
super<R|kotlin/Any|>()
}
}
public final fun user(f: R|kotlin/Function1<kotlin/Int, Klass>|): R|kotlin/Unit| {
}
public final fun fn(): R|kotlin/Unit| {
R|/user|(::R|/Klass.Klass|)
}
@@ -50,6 +50,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
runTest("compiler/fir/resolve/testData/diagnostics/callableReferences/companions.kt");
}
@TestMetadata("constructors.kt")
public void testConstructors() throws Exception {
runTest("compiler/fir/resolve/testData/diagnostics/callableReferences/constructors.kt");
}
@TestMetadata("differentLevels.kt")
public void testDifferentLevels() throws Exception {
runTest("compiler/fir/resolve/testData/diagnostics/callableReferences/differentLevels.kt");