From 648bf998426cb1e9b7f083bb4b5820acb5a7245f Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 24 Mar 2021 18:51:13 +0300 Subject: [PATCH] FIR: temporarily drop parent manipulation code around super type entries --- .../diagnostics/FirDiagnosticsList.kt | 7 ++- .../fir/analysis/diagnostics/FirErrors.kt | 5 +- .../kotlin/fir/analysis/FirSourceUtils.kt | 52 +------------------ .../FirPrimaryConstructorSuperTypeChecker.kt | 21 ++++---- .../delegation/DelegationNotTotrait.fir.kt | 2 +- .../delegation/DelegationToJavaIface.fir.kt | 2 +- .../tests/delegation/kt40510.fir.kt | 2 +- .../diagnostics/KtFirDataClassConverters.kt | 13 +++-- .../api/fir/diagnostics/KtFirDiagnostics.kt | 9 ++-- .../fir/diagnostics/KtFirDiagnosticsImpl.kt | 15 +++--- 10 files changed, 36 insertions(+), 92 deletions(-) diff --git a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt index 60ef05fd9c6..914879b34c0 100644 --- a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt +++ b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt @@ -102,6 +102,9 @@ object DIAGNOSTICS_LIST : DiagnosticList() { val NON_PRIVATE_OR_PROTECTED_CONSTRUCTOR_IN_SEALED by error() val CYCLIC_CONSTRUCTOR_DELEGATION_CALL by warning() val PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED by warning(PositioningStrategy.SECONDARY_CONSTRUCTOR_DELEGATION_CALL) + + // TODO: change it to KtSuperTypeEntry when possible (after re-targeter implementation) + val SUPERTYPE_NOT_INITIALIZED by error() val SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR by error() val DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR by warning() val PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS by error(PositioningStrategy.DECLARATION_NAME) @@ -168,10 +171,6 @@ object DIAGNOSTICS_LIST : DiagnosticList() { } } - val CLASSES_AND_INTERFACES by object : DiagnosticGroup("Classes and interfaces") { - val SUPERTYPE_NOT_INITIALIZED by error() - } - val INLINE_CLASSES by object : DiagnosticGroup("Inline classes") { val INLINE_CLASS_NOT_TOP_LEVEL by error(PositioningStrategy.INLINE_OR_VALUE_MODIFIER) val INLINE_CLASS_NOT_FINAL by error(PositioningStrategy.MODALITY_MODIFIER) diff --git a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt index 03f6ad6fa05..bb0488f0559 100644 --- a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt +++ b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt @@ -41,7 +41,6 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyDelegate import org.jetbrains.kotlin.psi.KtSimpleNameExpression -import org.jetbrains.kotlin.psi.KtSuperTypeEntry import org.jetbrains.kotlin.psi.KtTypeParameter import org.jetbrains.kotlin.psi.KtTypeParameterList import org.jetbrains.kotlin.psi.KtTypeReference @@ -106,6 +105,7 @@ object FirErrors { val NON_PRIVATE_OR_PROTECTED_CONSTRUCTOR_IN_SEALED by error0() val CYCLIC_CONSTRUCTOR_DELEGATION_CALL by warning0() val PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED by warning0(SourceElementPositioningStrategies.SECONDARY_CONSTRUCTOR_DELEGATION_CALL) + val SUPERTYPE_NOT_INITIALIZED by error0() val SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR by error0() val DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR by warning0() val PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS by error0(SourceElementPositioningStrategies.DECLARATION_NAME) @@ -150,9 +150,6 @@ object FirErrors { val REDUNDANT_OPEN_IN_INTERFACE by warning0(SourceElementPositioningStrategies.OPEN_MODIFIER) val WRONG_MODIFIER_TARGET by error2() - // Classes and interfaces - val SUPERTYPE_NOT_INITIALIZED by error0() - // Inline classes val INLINE_CLASS_NOT_TOP_LEVEL by error0(SourceElementPositioningStrategies.INLINE_OR_VALUE_MODIFIER) val INLINE_CLASS_NOT_FINAL by error0(SourceElementPositioningStrategies.MODALITY_MODIFIER) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/FirSourceUtils.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/FirSourceUtils.kt index 8dc8df478b6..c3b728ffced 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/FirSourceUtils.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/FirSourceUtils.kt @@ -5,8 +5,6 @@ package org.jetbrains.kotlin.fir.analysis -import com.intellij.lang.LighterASTNode -import com.intellij.psi.PsiElement import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.TokenSet import org.jetbrains.kotlin.fir.* @@ -38,54 +36,6 @@ private fun FirPsiSourceElement<*>.getChild(types: Set, index: Int private fun FirLightSourceElement.getChild(types: Set, index: Int, depth: Int): FirSourceElement? { val visitor = LighterTreeElementFinderByType(treeStructure, types, index, depth) - return visitor.find(lighterASTNode)?.let { withNode(it) } + return visitor.find(lighterASTNode)?.toFirLightSourceElement(treeStructure) } -fun FirSourceElement.getParent(type: IElementType, includingSelf: Boolean = false): FirSourceElement? { - return getParent(setOf(type), includingSelf) -} - -fun FirSourceElement.getParent(types: TokenSet, includingSelf: Boolean = false): FirSourceElement? { - return getParent(types.types.toSet(), includingSelf) -} - -fun FirSourceElement.getParent(types: Set, includingSelf: Boolean = false): FirSourceElement? { - return when (this) { - is FirPsiSourceElement<*> -> { - getParent(types, includingSelf) - } - is FirLightSourceElement -> { - getParent(types, includingSelf) - } - else -> null - } -} - -private fun FirPsiSourceElement<*>.getParent(types: Set, includingSelf: Boolean): FirSourceElement? { - var parent: PsiElement? = if (includingSelf) psi else psi.parent - while (parent != null && parent.node.elementType !in types) { - parent = parent.parent - } - return parent?.toFirPsiSourceElement() -} - -private fun FirLightSourceElement.getParent(types: Set, includingSelf: Boolean): FirSourceElement? { - var parent: LighterASTNode? = if (includingSelf) lighterASTNode else treeStructure.getParent(lighterASTNode) - while (parent != null && parent.tokenType !in types) { - parent = treeStructure.getParent(parent) - } - return parent?.let { withNode(it) } -} - -private fun FirLightSourceElement.withNode(newNode: LighterASTNode): FirLightSourceElement { - // It seems sometimes the `startOffset` from a `LighterASTNode` could count from some partial sub tree of the file. If this is the case, - // we need to compute the delta between the corresponding position in this partial tree and the file start. The latter can be retrieved - // from `FirLightSourceElement`. - val startDelta = this.startOffset - lighterASTNode.startOffset - val endDelta = this.endOffset - lighterASTNode.endOffset - return newNode.toFirLightSourceElement( - treeStructure, - startOffset = startDelta + newNode.startOffset, - endOffset = endDelta + newNode.endOffset - ) -} diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPrimaryConstructorSuperTypeChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPrimaryConstructorSuperTypeChecker.kt index e38e8232c7b..3e8324ea541 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPrimaryConstructorSuperTypeChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPrimaryConstructorSuperTypeChecker.kt @@ -6,17 +6,18 @@ package org.jetbrains.kotlin.fir.analysis.checkers.declaration import org.jetbrains.kotlin.KtNodeTypes +import org.jetbrains.kotlin.fir.FirFakeSourceElementKind import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.checkers.toRegularClass import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn -import org.jetbrains.kotlin.fir.analysis.getParent import org.jetbrains.kotlin.fir.declarations.FirConstructor import org.jetbrains.kotlin.fir.declarations.FirRegularClass import org.jetbrains.kotlin.fir.declarations.isInterface import org.jetbrains.kotlin.fir.declarations.primaryConstructor -import org.jetbrains.kotlin.fir.types.coneType +import org.jetbrains.kotlin.fir.symbols.StandardClassIds +import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.FirImplicitAnyTypeRef import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull @@ -54,8 +55,9 @@ object FirPrimaryConstructorSuperTypeChecker : FirRegularClassChecker() { val containingClass = context.containingDeclarations.lastIsInstanceOrNull() val delegatedConstructorCall = primaryConstructor.delegatedConstructor ?: return // No need to check implicit call to the constructor of `kotlin.Any`. - if (delegatedConstructorCall.constructedTypeRef is FirImplicitAnyTypeRef) return - val superClass = delegatedConstructorCall.constructedTypeRef.coneType.toRegularClass(context.session) ?: return + val constructedTypeRef = delegatedConstructorCall.constructedTypeRef + if (constructedTypeRef is FirImplicitAnyTypeRef) return + val superClass = constructedTypeRef.coneType.toRegularClass(context.session) ?: return // Subclassing a singleton should be reported as SINGLETON_IN_SUPERTYPE if (superClass.classKind.isSingleton) return if (regularClass.isEffectivelyExpect(containingClass, context) || @@ -63,12 +65,11 @@ object FirPrimaryConstructorSuperTypeChecker : FirRegularClassChecker() { ) { return } - if (delegatedConstructorCall.source?.elementType != KtNodeTypes.SUPER_TYPE_CALL_ENTRY) { - reporter.reportOn( - delegatedConstructorCall.constructedTypeRef.source?.getParent(KtNodeTypes.SUPER_TYPE_ENTRY), - FirErrors.SUPERTYPE_NOT_INITIALIZED, - context - ) + val delegatedCallSource = delegatedConstructorCall.source ?: return + if (delegatedCallSource.kind !is FirFakeSourceElementKind) return + if (superClass.symbol.classId == StandardClassIds.Enum) return + if (delegatedCallSource.elementType != KtNodeTypes.SUPER_TYPE_CALL_ENTRY) { + reporter.reportOn(constructedTypeRef.source, FirErrors.SUPERTYPE_NOT_INITIALIZED, context) } } diff --git a/compiler/testData/diagnostics/tests/delegation/DelegationNotTotrait.fir.kt b/compiler/testData/diagnostics/tests/delegation/DelegationNotTotrait.fir.kt index a2be1fa6abc..7d51fde3621 100644 --- a/compiler/testData/diagnostics/tests/delegation/DelegationNotTotrait.fir.kt +++ b/compiler/testData/diagnostics/tests/delegation/DelegationNotTotrait.fir.kt @@ -2,7 +2,7 @@ open class Foo() { } -class Barrr() : Foo by Foo() {} +class Barrr() : Foo by Foo() {} interface T {} diff --git a/compiler/testData/diagnostics/tests/delegation/DelegationToJavaIface.fir.kt b/compiler/testData/diagnostics/tests/delegation/DelegationToJavaIface.fir.kt index 07044ecce1d..1e62a0c87bb 100644 --- a/compiler/testData/diagnostics/tests/delegation/DelegationToJavaIface.fir.kt +++ b/compiler/testData/diagnostics/tests/delegation/DelegationToJavaIface.fir.kt @@ -1,4 +1,4 @@ // JAVAC_EXPECTED_FILE class TestIface(r : Runnable) : Runnable by r {} -class TestObject(o : Object) : Object by o {} \ No newline at end of file +class TestObject(o : Object) : Object by o {} diff --git a/compiler/testData/diagnostics/tests/delegation/kt40510.fir.kt b/compiler/testData/diagnostics/tests/delegation/kt40510.fir.kt index 75d72840e0c..18d3d1257db 100644 --- a/compiler/testData/diagnostics/tests/delegation/kt40510.fir.kt +++ b/compiler/testData/diagnostics/tests/delegation/kt40510.fir.kt @@ -11,4 +11,4 @@ public abstract class A { // FILE: main.kt import foo.A -class DelegatedA(val a: A) : A by a +class DelegatedA(val a: A) : A by a diff --git a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt index a6afa523bee..bc527e82fab 100644 --- a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt +++ b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt @@ -32,7 +32,6 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyDelegate import org.jetbrains.kotlin.psi.KtSimpleNameExpression -import org.jetbrains.kotlin.psi.KtSuperTypeEntry import org.jetbrains.kotlin.psi.KtTypeParameter import org.jetbrains.kotlin.psi.KtTypeParameterList import org.jetbrains.kotlin.psi.KtTypeReference @@ -296,6 +295,12 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert token, ) } + add(FirErrors.SUPERTYPE_NOT_INITIALIZED) { firDiagnostic -> + SupertypeNotInitializedImpl( + firDiagnostic as FirPsiDiagnostic<*>, + token, + ) + } add(FirErrors.SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR) { firDiagnostic -> SupertypeInitializedWithoutPrimaryConstructorImpl( firDiagnostic as FirPsiDiagnostic<*>, @@ -555,12 +560,6 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert token, ) } - add(FirErrors.SUPERTYPE_NOT_INITIALIZED) { firDiagnostic -> - SupertypeNotInitializedImpl( - firDiagnostic as FirPsiDiagnostic<*>, - token, - ) - } add(FirErrors.INLINE_CLASS_NOT_TOP_LEVEL) { firDiagnostic -> InlineClassNotTopLevelImpl( firDiagnostic as FirPsiDiagnostic<*>, diff --git a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt index 4e221cae0d2..4c13b53fccf 100644 --- a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt +++ b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt @@ -36,7 +36,6 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyDelegate import org.jetbrains.kotlin.psi.KtSimpleNameExpression -import org.jetbrains.kotlin.psi.KtSuperTypeEntry import org.jetbrains.kotlin.psi.KtTypeParameter import org.jetbrains.kotlin.psi.KtTypeParameterList import org.jetbrains.kotlin.psi.KtTypeReference @@ -219,6 +218,10 @@ sealed class KtFirDiagnostic : KtDiagnosticWithPsi { override val diagnosticClass get() = PrimaryConstructorDelegationCallExpected::class } + abstract class SupertypeNotInitialized : KtFirDiagnostic() { + override val diagnosticClass get() = SupertypeNotInitialized::class + } + abstract class SupertypeInitializedWithoutPrimaryConstructor : KtFirDiagnostic() { override val diagnosticClass get() = SupertypeInitializedWithoutPrimaryConstructor::class } @@ -404,10 +407,6 @@ sealed class KtFirDiagnostic : KtDiagnosticWithPsi { abstract val target: String } - abstract class SupertypeNotInitialized : KtFirDiagnostic() { - override val diagnosticClass get() = SupertypeNotInitialized::class - } - abstract class InlineClassNotTopLevel : KtFirDiagnostic() { override val diagnosticClass get() = InlineClassNotTopLevel::class } diff --git a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt index bdee7bccaca..6019e31252e 100644 --- a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt +++ b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt @@ -38,7 +38,6 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyDelegate import org.jetbrains.kotlin.psi.KtSimpleNameExpression -import org.jetbrains.kotlin.psi.KtSuperTypeEntry import org.jetbrains.kotlin.psi.KtTypeParameter import org.jetbrains.kotlin.psi.KtTypeParameterList import org.jetbrains.kotlin.psi.KtTypeReference @@ -343,6 +342,13 @@ internal class PrimaryConstructorDelegationCallExpectedImpl( override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) } +internal class SupertypeNotInitializedImpl( + firDiagnostic: FirPsiDiagnostic<*>, + override val token: ValidityToken, +) : KtFirDiagnostic.SupertypeNotInitialized(), KtAbstractFirDiagnostic { + override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) +} + internal class SupertypeInitializedWithoutPrimaryConstructorImpl( firDiagnostic: FirPsiDiagnostic<*>, override val token: ValidityToken, @@ -639,13 +645,6 @@ internal class WrongModifierTargetImpl( override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) } -internal class SupertypeNotInitializedImpl( - firDiagnostic: FirPsiDiagnostic<*>, - override val token: ValidityToken, -) : KtFirDiagnostic.SupertypeNotInitialized(), KtAbstractFirDiagnostic { - override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) -} - internal class InlineClassNotTopLevelImpl( firDiagnostic: FirPsiDiagnostic<*>, override val token: ValidityToken,