From 1e3621a896b37526ab62f0558e98a74b97bfb27c Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 19 Nov 2020 15:38:47 +0300 Subject: [PATCH] FIR checkers: simplify hasPrimaryConstructor by source element check --- .../resolve/typeAliasWithTypeArguments.kt | 2 +- .../FirConstructorInInterfaceChecker.kt | 38 +------------------ .../LightTreePositioningStrategies.kt | 3 ++ .../tests/SupertypeListChecks.fir.kt | 2 +- .../tests/TraitWithConstructor.fir.kt | 10 ++--- .../traitSupertypeList.fir.kt | 2 +- 6 files changed, 12 insertions(+), 45 deletions(-) diff --git a/compiler/fir/analysis-tests/testData/resolve/typeAliasWithTypeArguments.kt b/compiler/fir/analysis-tests/testData/resolve/typeAliasWithTypeArguments.kt index 6ab505ccc78..baaee1fb118 100644 --- a/compiler/fir/analysis-tests/testData/resolve/typeAliasWithTypeArguments.kt +++ b/compiler/fir/analysis-tests/testData/resolve/typeAliasWithTypeArguments.kt @@ -10,7 +10,7 @@ interface C { fun baz() } -interface Inv() { +interface Inv() { fun k(): K fun t(): T } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirConstructorInInterfaceChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirConstructorInInterfaceChecker.kt index 36caea305fe..87a397feedf 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirConstructorInInterfaceChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirConstructorInInterfaceChecker.kt @@ -5,22 +5,14 @@ package org.jetbrains.kotlin.fir.analysis.checkers.declaration -import com.intellij.lang.LighterASTNode -import com.intellij.openapi.util.Ref -import com.intellij.psi.PsiElement -import com.intellij.psi.PsiErrorElement -import com.intellij.util.diff.FlyweightCapableTreeStructure -import org.jetbrains.kotlin.KtNodeTypes.PRIMARY_CONSTRUCTOR import org.jetbrains.kotlin.descriptors.ClassKind -import org.jetbrains.kotlin.fir.FirLightSourceElement import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors +import org.jetbrains.kotlin.fir.analysis.diagnostics.hasPrimaryConstructor import org.jetbrains.kotlin.fir.declarations.FirClass import org.jetbrains.kotlin.fir.declarations.FirDeclaration -import org.jetbrains.kotlin.fir.psi -import org.jetbrains.kotlin.psi.KtPrimaryConstructor object FirConstructorInInterfaceChecker : FirBasicDeclarationChecker() { override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) { @@ -33,34 +25,6 @@ object FirConstructorInInterfaceChecker : FirBasicDeclarationChecker() { } } - private fun FirSourceElement.hasPrimaryConstructor(): Boolean { - val localPsi = psi - val localLightNode = lighterASTNode - - if (localPsi != null && localPsi !is PsiErrorElement) { - return localPsi.hasPrimaryConstructor() - } else if (this is FirLightSourceElement) { - return localLightNode.hasPrimaryConstructor(treeStructure) - } - - return false - } - - private fun PsiElement.hasPrimaryConstructor(): Boolean { - return lastChild !is PsiErrorElement && lastChild is KtPrimaryConstructor - } - - private fun LighterASTNode.hasPrimaryConstructor(tree: FlyweightCapableTreeStructure): Boolean { - val children = getChildren(tree) - return children.lastOrNull()?.tokenType == PRIMARY_CONSTRUCTOR - } - - private fun LighterASTNode.getChildren(tree: FlyweightCapableTreeStructure): List { - val children = Ref>() - val count = tree.getChildren(this, children) - return if (count > 0) children.get().filterNotNull() else emptyList() - } - private fun DiagnosticReporter.report(source: FirSourceElement?) { source?.let { report(FirErrors.CONSTRUCTOR_IN_INTERFACE.on(it)) } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt index 48083e57f21..4644b7cebd0 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt @@ -150,6 +150,9 @@ fun FirSourceElement.hasValOrVar(): Boolean = fun FirSourceElement.hasVar(): Boolean = treeStructure.findChildByType(lighterASTNode, KtTokens.VAR_KEYWORD) != null +fun FirSourceElement.hasPrimaryConstructor(): Boolean = + treeStructure.primaryConstructor(lighterASTNode) != null + private fun FlyweightCapableTreeStructure.constructorKeyword(node: LighterASTNode): LighterASTNode? = findChildByType(node, KtTokens.CONSTRUCTOR_KEYWORD) diff --git a/compiler/testData/diagnostics/tests/SupertypeListChecks.fir.kt b/compiler/testData/diagnostics/tests/SupertypeListChecks.fir.kt index ff937931bf4..637e06a5411 100644 --- a/compiler/testData/diagnostics/tests/SupertypeListChecks.fir.kt +++ b/compiler/testData/diagnostics/tests/SupertypeListChecks.fir.kt @@ -21,7 +21,7 @@ interface T1 {} interface T2 {} -interface Test() { +interface Test() { } interface Test1 : C2() {} diff --git a/compiler/testData/diagnostics/tests/TraitWithConstructor.fir.kt b/compiler/testData/diagnostics/tests/TraitWithConstructor.fir.kt index 6d9d6109a25..fc2b752110b 100644 --- a/compiler/testData/diagnostics/tests/TraitWithConstructor.fir.kt +++ b/compiler/testData/diagnostics/tests/TraitWithConstructor.fir.kt @@ -2,11 +2,11 @@ class C(val a: String) {} -interface T1(val x: String) {} +interface T1(val x: String) {} -interface T2 constructor() {} +interface T2 constructor() {} -interface T3 private constructor(a: Int) {} +interface T3 private constructor(a: Int) {} interface T4 { constructor(a: Int) { @@ -14,5 +14,5 @@ interface T4 { } } -interface T5 private () : T4 {} -interface T6 private : T5 {} \ No newline at end of file +interface T5 private () : T4 {} +interface T6 private : T5 {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/traitWithRequired/traitSupertypeList.fir.kt b/compiler/testData/diagnostics/tests/traitWithRequired/traitSupertypeList.fir.kt index 2b3afe2bd13..d1a66fcaece 100644 --- a/compiler/testData/diagnostics/tests/traitWithRequired/traitSupertypeList.fir.kt +++ b/compiler/testData/diagnostics/tests/traitWithRequired/traitSupertypeList.fir.kt @@ -1,6 +1,6 @@ open class bar() -interface Foo() : bar(), bar, bar { +interface Foo() : bar(), bar, bar { } interface Foo2 : bar, Foo {