Skip to content

Commit

Permalink
(WIP) fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Feb 21, 2025
1 parent a163f5a commit 6190bad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.jetbrains.kotlin.analysis.api.resolution.KaExplicitReceiverValue
import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
import org.jetbrains.kotlin.analysis.api.resolution.KaImplicitReceiverValue
import org.jetbrains.kotlin.analysis.api.resolution.singleFunctionCallOrNull
import org.jetbrains.kotlin.analysis.api.resolution.singleVariableAccessCall
import org.jetbrains.kotlin.analysis.api.resolution.successfulCallOrNull
import org.jetbrains.kotlin.analysis.api.resolution.successfulFunctionCallOrNull
import org.jetbrains.kotlin.analysis.api.resolution.symbol
Expand Down Expand Up @@ -64,14 +63,12 @@ import org.jetbrains.kotlin.psi.KtCallElement
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtParameter
Expand Down Expand Up @@ -882,7 +879,6 @@ fun KaType.simpleName(): String? = withKaSession {
return lowerBoundIfFlexible().symbol?.name?.asString()
}

@Deprecated("use kotlin-analysis-api instead", ReplaceWith("this.determineType()"))
fun PsiElement?.determineType(bindingContext: BindingContext): KotlinType? =
this?.let {
when (this) {
Expand All @@ -901,34 +897,6 @@ fun PsiElement?.determineType(bindingContext: BindingContext): KotlinType? =

}

fun PsiElement?.determineType(): KaType? = withKaSession {
this?.let {
when (this@determineType) {
is KtCallExpression -> determineTypeFromCall()
is KtParameter -> symbol.returnType
is KtTypeReference -> type
is KtProperty -> determineTypeFromCall()
is KtDotQualifiedExpression -> determineTypeFromCall()
is KtReferenceExpression -> determineTypeFromCall()
is KtFunction -> (symbol as KaFunctionSymbol).returnType
is KtClass -> returnType
is KtConstantExpression -> this@determineType.expressionType
is KtStringTemplateExpression -> this@determineType.expressionType
is KtLambdaExpression -> this@determineType.expressionType
is KtExpression -> determineTypeFromCall()
is KtValueArgument -> this@determineType.getArgumentExpression()?.determineType()
else -> null
}
}
}

private fun KtElement.determineTypeFromCall(): KaType? = withKaSession {
this@determineTypeFromCall.resolveToCall()?.let {
(it.successfulFunctionCallOrNull() ?: it.singleVariableAccessCall())
?.partiallyAppliedSymbol?.symbol?.returnType
}
}

fun KotlinType.isSupertypeOf(other: KotlinType): Boolean {
return findCorrespondingSupertype(other, this).let { it != null && it != other }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class DelegationPatternCheck : AbstractCheck() {
}
}

private fun checkNamedFunction(function: KtNamedFunction, superInterfaces: Set<KaClassSymbol>, context: KotlinFileContext) {
private fun checkNamedFunction(function: KtNamedFunction, superInterfaces: Set<KaClassSymbol>, context: KotlinFileContext) = withKaSession {
if (!(function.isPublic && function.overrides())) return
val delegeeType = getDelegeeOrNull(function)?.determineType() ?: return
val delegeeType = getDelegeeOrNull(function)?.expressionType ?: return

if (getCommonSuperInterfaces(superInterfaces, delegeeType).any {
isFunctionInInterface(function, it)
Expand All @@ -72,13 +72,9 @@ class DelegationPatternCheck : AbstractCheck() {
}
}

private fun isFunctionInInterface(
function: KtNamedFunction,
superInterface1: KaClassSymbol
): Boolean = withKaSession {
val classDeclaration = superInterface1.psi as? KtClass ?: return false
return classDeclaration.declarations.any {
it is KtNamedFunction && haveCompatibleFunctionSignature(it.symbol, function.symbol)
private fun isFunctionInInterface(function: KtNamedFunction, superInterface: KaClassSymbol): Boolean = withKaSession {
superInterface.declaredMemberScope.declarations.any {
it is KaFunctionSymbol && haveCompatibleFunctionSignature(it, function.symbol)
}
}

Expand Down Expand Up @@ -131,7 +127,7 @@ private fun getDelegeeOrNull(function: KtNamedFunction): KtNameReferenceExpressi
private fun isDelegatedParameter(parameter: KtParameter, arguments: KtValueArgument): Boolean = withKaSession {
val argumentExpression = arguments.getArgumentExpression() as? KtNameReferenceExpression ?: return false
if (parameter.name != argumentExpression.getReferencedName()) return false
val argumentType = argumentExpression.determineType() ?: return false
val argumentType = argumentExpression.expressionType ?: return false
return parameter.symbol.returnType.semanticallyEquals(argumentType)
}

Expand Down

0 comments on commit 6190bad

Please sign in to comment.