[Analysis API FIR] fix candidate collection for delegatedConstructor call

^KTIJ-20446
This commit is contained in:
Ilya Kirillov
2022-07-20 19:20:49 +02:00
parent 2c48d25c96
commit a0f0fa5a47
19 changed files with 457 additions and 39 deletions
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFir
import org.jetbrains.kotlin.analysis.low.level.api.fir.resolver.AllCandidatesResolver
import org.jetbrains.kotlin.analysis.utils.printer.parentOfType
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.expandedClass
import org.jetbrains.kotlin.fir.analysis.checkers.toRegularClassSymbol
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
import org.jetbrains.kotlin.fir.declarations.fullyExpandedClass
@@ -843,6 +842,7 @@ internal class KtFirCallResolver(
resolveFragmentOfCall
).toKtCallCandidateInfos()
is FirResolvedQualifier -> toKtCallCandidateInfos()
is FirDelegatedConstructorCall -> collectCallCandidatesForDelegatedConstructorCall(psi, resolveFragmentOfCall)
else -> toKtCallInfo(psi, resolveCalleeExpressionOfFunctionCall, resolveFragmentOfCall).toKtCallCandidateInfos()
}
}
@@ -912,6 +912,26 @@ internal class KtFirCallResolver(
}
}
private fun FirDelegatedConstructorCall.collectCallCandidatesForDelegatedConstructorCall(
psi: KtElement,
resolveFragmentOfCall: Boolean
): List<KtCallCandidateInfo> {
val candidates = AllCandidatesResolver(analysisSession.useSiteSession).getAllCandidatesForDelegatedConstructor(
analysisSession.firResolveSession,
this,
psi
)
return candidates.mapNotNull {
convertToKtCallCandidateInfo(
this,
psi,
it.candidate,
it.isInBestCandidates,
resolveFragmentOfCall
)
}
}
private fun KtCallInfo?.toKtCallCandidateInfos(): List<KtCallCandidateInfo> {
return when (this) {
is KtSuccessCallInfo -> listOf(KtApplicableCallCandidateInfo(call, isInBestCandidates = true))
@@ -921,13 +941,13 @@ internal class KtFirCallResolver(
}
private fun convertToKtCallCandidateInfo(
functionCall: FirFunctionCall,
resolvable: FirResolvable,
element: KtElement,
candidate: Candidate,
isInBestCandidates: Boolean,
resolveFragmentOfCall: Boolean
): KtCallCandidateInfo? {
val call = createKtCall(element, functionCall, candidate, resolveFragmentOfCall)
val call = createKtCall(element, resolvable, candidate, resolveFragmentOfCall)
?: error("expect `createKtCall` to succeed for candidate")
if (candidate.isSuccessful) {
return KtApplicableCallCandidateInfo(call, isInBestCandidates)
@@ -936,7 +956,7 @@ internal class KtFirCallResolver(
val diagnostic = createConeDiagnosticForCandidateWithError(candidate.currentApplicability, candidate)
if (diagnostic is ConeHiddenCandidateError) return null
val ktDiagnostic =
functionCall.source?.let { diagnostic.asKtDiagnostic(it, element.toKtPsiSourceElement()) }
resolvable.source?.let { diagnostic.asKtDiagnostic(it, element.toKtPsiSourceElement()) }
?: KtNonBoundToPsiErrorDiagnostic(factoryName = null, diagnostic.reason, token)
return KtInapplicableCallCandidateInfo(call, isInBestCandidates, ktDiagnostic)
}
@@ -79,6 +79,12 @@ public class FirIdeNormalAnalysisSourceModuleResolveCandidatesTestGenerated exte
runTest("analysis/analysis-api/testData/components/callResolver/resolveCandidates/multipleCandidates/ambiguousWithInferredTypeParameters.kt");
}
@Test
@TestMetadata("delegatedConstructor.kt")
public void testDelegatedConstructor() throws Exception {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCandidates/multipleCandidates/delegatedConstructor.kt");
}
@Test
@TestMetadata("implicitInvoke.kt")
public void testImplicitInvoke() throws Exception {
@@ -207,6 +213,24 @@ public class FirIdeNormalAnalysisSourceModuleResolveCandidatesTestGenerated exte
runTest("analysis/analysis-api/testData/components/callResolver/resolveCandidates/singleCandidate/consecutiveImplicitInvoke3.kt");
}
@Test
@TestMetadata("delegatedConstructorApplicable.kt")
public void testDelegatedConstructorApplicable() throws Exception {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCandidates/singleCandidate/delegatedConstructorApplicable.kt");
}
@Test
@TestMetadata("delegatedConstructorInapplicable.kt")
public void testDelegatedConstructorInapplicable() throws Exception {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCandidates/singleCandidate/delegatedConstructorInapplicable.kt");
}
@Test
@TestMetadata("delegatedConstructorInapplicableDifferentParametersCount.kt")
public void testDelegatedConstructorInapplicableDifferentParametersCount() throws Exception {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCandidates/singleCandidate/delegatedConstructorInapplicableDifferentParametersCount.kt");
}
@Test
@TestMetadata("eqEqCall_fromAny.kt")
public void testEqEqCall_fromAny() throws Exception {