Add base declaration checker for fun interfaces

This commit is contained in:
Mikhail Zarechenskiy
2020-02-04 17:41:30 +03:00
parent 6735cc8937
commit 45bbdaf73f
11 changed files with 505 additions and 1 deletions
@@ -360,6 +360,13 @@ public interface Errors {
DiagnosticFactory0<PsiElement> RESULT_CLASS_IN_RETURN_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> RESULT_CLASS_WITH_NULLABLE_OPERATOR = DiagnosticFactory1.create(ERROR);
// Fun interfaces
DiagnosticFactory0<PsiElement> FUN_INTERFACE_WRONG_COUNT_OF_ABSTRACT_MEMBERS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> FUN_INTERFACE_CANNOT_HAVE_ABSTRACT_PROPERTIES = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> FUN_INTERFACE_ABSTRACT_METHOD_WITH_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
// Secondary constructors
DiagnosticFactory0<KtConstructorDelegationReferenceExpression> CYCLIC_CONSTRUCTOR_DELEGATION_CALL = DiagnosticFactory0.create(ERROR);
@@ -700,6 +700,11 @@ public class DefaultErrorMessages {
MAP.put(RESULT_CLASS_IN_RETURN_TYPE, "'kotlin.Result' cannot be used as a return type");
MAP.put(RESULT_CLASS_WITH_NULLABLE_OPERATOR, "Expression of type 'kotlin.Result' cannot be used as a left operand of ''{0}''", STRING);
MAP.put(FUN_INTERFACE_WRONG_COUNT_OF_ABSTRACT_MEMBERS, "Fun interfaces must have exactly one abstract method");
MAP.put(FUN_INTERFACE_CANNOT_HAVE_ABSTRACT_PROPERTIES, "Fun interfaces cannot have abstract properties");
MAP.put(FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS, "Single abstract member cannot declare type parameters");
MAP.put(FUN_INTERFACE_ABSTRACT_METHOD_WITH_DEFAULT_VALUE, "Single abstract member cannot declare default values");
MAP.put(VARIANCE_ON_TYPE_PARAMETER_NOT_ALLOWED, "Variance annotations are only allowed for type parameters of classes and interfaces");
MAP.put(BOUND_ON_TYPE_ALIAS_PARAMETER_NOT_ALLOWED, "Bounds are not allowed on type alias parameters");
@@ -35,7 +35,8 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
ExplicitApiDeclarationChecker(),
TailrecFunctionChecker,
TrailingCommaDeclarationChecker,
MissingDependencySupertypeChecker.ForDeclarations
MissingDependencySupertypeChecker.ForDeclarations,
FunInterfaceDeclarationChecker()
)
private val DEFAULT_CALL_CHECKERS = listOf(
@@ -0,0 +1,71 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
import org.jetbrains.kotlin.resolve.sam.getAbstractMembers
import org.jetbrains.kotlin.resolve.source.getPsi
class FunInterfaceDeclarationChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (declaration !is KtClass) return
if (descriptor !is ClassDescriptor || !descriptor.isFun) return
val funKeyword = declaration.getFunKeyword() ?: return
val abstractMembers = getAbstractMembers(descriptor)
for (abstractMember in abstractMembers) {
if (abstractMember !is PropertyDescriptor) continue
val reportOnProperty = abstractMember.containingDeclaration == descriptor
val reportOn = if (reportOnProperty) {
(abstractMember.source.getPsi() as? KtProperty)?.valOrVarKeyword ?: funKeyword
} else {
funKeyword
}
context.trace.report(Errors.FUN_INTERFACE_CANNOT_HAVE_ABSTRACT_PROPERTIES.on(reportOn))
if (!reportOnProperty) return // It's enough to report only once if abstract properties are in the base class
}
val abstractMember = abstractMembers.filterIsInstance<FunctionDescriptor>().singleOrNull()
if (abstractMember == null) {
context.trace.report(Errors.FUN_INTERFACE_WRONG_COUNT_OF_ABSTRACT_MEMBERS.on(funKeyword))
return
}
checkSingleAbstractMember(abstractMember, funKeyword, context)
}
private fun checkSingleAbstractMember(
abstractMember: FunctionDescriptor,
funInterfaceKeyword: PsiElement,
context: DeclarationCheckerContext,
) {
val ktFunction = abstractMember.source.getPsi() as? KtNamedFunction
if (abstractMember.typeParameters.isNotEmpty()) {
val reportOn = ktFunction?.typeParameterList ?: ktFunction?.funKeyword ?: funInterfaceKeyword
context.trace.report(Errors.FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS.on(reportOn))
return
}
for (parameter in abstractMember.valueParameters) {
if (parameter.hasDefaultValue()) {
val reportOn = parameter.source.getPsi() ?: ktFunction?.funKeyword ?: funInterfaceKeyword
context.trace.report(Errors.FUN_INTERFACE_ABSTRACT_METHOD_WITH_DEFAULT_VALUE.on(reportOn))
return
}
}
}
}