[low level api] consider return type when matching FIR and PSI declarations

This commit is contained in:
Ilya Kirillov
2022-05-09 14:13:34 +02:00
committed by teamcity
parent 7675361380
commit 875da1ea1e
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.impl.FirImplicitNullableAnyTypeRef
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtConstructor
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.types.Variance
@@ -31,22 +32,28 @@ import org.jetbrains.kotlin.utils.addIfNotNull
// TODO replace with structural type comparison?
object KtDeclarationAndFirDeclarationEqualityChecker {
fun representsTheSameDeclaration(psi: KtCallableDeclaration, fir: FirCallableDeclaration): Boolean {
if ((fir.receiverTypeRef != null) != (psi.receiverTypeReference != null)) return false
if (fir.receiverTypeRef != null
&& !isTheSameTypes(
psi.receiverTypeReference!!,
fir.receiverTypeRef!!,
isVararg = false
)
) {
return false
}
if (!receiverTypeMatch(psi, fir)) return false
if (!returnTypesMatch(psi, fir)) return false
if (fir is FirFunction) {
if (!typeParametersMatch(fir, psi) || !valueParametersMatch(fir, psi)) return false
}
return true
}
private fun receiverTypeMatch(psi: KtCallableDeclaration, fir: FirCallableDeclaration): Boolean {
if ((fir.receiverTypeRef != null) != (psi.receiverTypeReference != null)) return false
if (fir.receiverTypeRef != null && !isTheSameTypes(psi.receiverTypeReference!!, fir.receiverTypeRef!!, isVararg = false)) {
return false
}
return true
}
private fun returnTypesMatch(psi: KtCallableDeclaration, fir: FirCallableDeclaration): Boolean {
if (psi is KtConstructor<*>) return true
return isTheSameTypes(psi.typeReference!!, fir.returnTypeRef, isVararg = false)
}
private fun typeParametersMatch(firFunction: FirCallableDeclaration, psiFunction: KtCallableDeclaration): Boolean {
if (firFunction.typeParameters.size != psiFunction.typeParameters.size) return false
val boundsByName = psiFunction.typeConstraints.groupBy { it.subjectTypeParameterName?.getReferencedName() }