[FIR] Check classes for EXPECTED_PRIVATE_DECLARATION
This commit is contained in:
committed by
Space Team
parent
e7f1da3619
commit
b46d4e5399
+1
-1
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.fir.analysis.checkers
|
||||
import org.jetbrains.kotlin.fir.analysis.cfa.AbstractFirPropertyInitializationChecker
|
||||
import org.jetbrains.kotlin.fir.analysis.cfa.FirCallsEffectAnalyzer
|
||||
import org.jetbrains.kotlin.fir.analysis.cfa.FirPropertyInitializationAnalyzer
|
||||
import org.jetbrains.kotlin.fir.analysis.cfa.FirReturnsImpliesAnalyzer
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.cfa.FirControlFlowChecker
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.*
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.expression.FirAnonymousFunctionParametersChecker
|
||||
@@ -32,6 +31,7 @@ object CommonDeclarationCheckers : DeclarationCheckers() {
|
||||
FirAnnotationChecker,
|
||||
FirPublishedApiChecker,
|
||||
FirOptInMarkedDeclarationChecker,
|
||||
FirExpectConsistencyChecker,
|
||||
)
|
||||
|
||||
override val callableDeclarationCheckers: Set<FirCallableDeclarationChecker>
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.fir.analysis.checkers.declaration
|
||||
|
||||
import org.jetbrains.kotlin.KtFakeSourceElementKind
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.diagnostics.reportOn
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.hasBody
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.visibility
|
||||
|
||||
// See old FE's [DeclarationsChecker]
|
||||
object FirExpectConsistencyChecker : FirBasicDeclarationChecker() {
|
||||
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
val isTopLevel = context.containingDeclarations.size == 1
|
||||
val isInsideClass = context.containingDeclarations.lastOrNull() is FirClass
|
||||
|
||||
if (
|
||||
declaration !is FirMemberDeclaration ||
|
||||
!isTopLevel && !isInsideClass ||
|
||||
!declaration.isExpect ||
|
||||
declaration is FirConstructor
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
val source = declaration.source ?: return
|
||||
if (source.kind is KtFakeSourceElementKind) return
|
||||
|
||||
if (Visibilities.isPrivate(declaration.visibility)) {
|
||||
reporter.reportOn(source, FirErrors.EXPECTED_PRIVATE_DECLARATION, context)
|
||||
}
|
||||
|
||||
if (declaration is FirSimpleFunction && declaration.hasBody) {
|
||||
reporter.reportOn(source, FirErrors.EXPECTED_DECLARATION_WITH_BODY, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
-2
@@ -70,7 +70,5 @@ object FirMemberFunctionsChecker : FirSimpleFunctionChecker() {
|
||||
reporter.reportOn(source, FirErrors.NON_ABSTRACT_FUNCTION_WITH_NO_BODY, functionSymbol, context)
|
||||
}
|
||||
}
|
||||
|
||||
checkExpectDeclarationVisibilityAndBody(function, source, reporter, context)
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -79,7 +79,6 @@ object FirMemberPropertiesChecker : FirClassChecker() {
|
||||
context,
|
||||
reachable
|
||||
)
|
||||
checkExpectDeclarationVisibilityAndBody(property, source, reporter, context)
|
||||
|
||||
val hasAbstractModifier = KtTokens.ABSTRACT_KEYWORD in modifierList
|
||||
val isAbstract = property.isAbstract || hasAbstractModifier
|
||||
|
||||
-4
@@ -29,8 +29,6 @@ object FirTopLevelFunctionsChecker : FirSimpleFunctionChecker() {
|
||||
|
||||
val source = declaration.source ?: return
|
||||
if (source.kind is KtFakeSourceElementKind) return
|
||||
// If multiple (potentially conflicting) modality modifiers are specified, not all modifiers are recorded at `status`.
|
||||
// So, our source of truth should be the full modifier list retrieved from the source.
|
||||
if (declaration.hasModifier(KtTokens.ABSTRACT_KEYWORD)) return
|
||||
if (declaration.isExternal) return
|
||||
if (!declaration.hasBody &&
|
||||
@@ -39,7 +37,5 @@ object FirTopLevelFunctionsChecker : FirSimpleFunctionChecker() {
|
||||
) {
|
||||
reporter.reportOn(source, FirErrors.NON_MEMBER_FUNCTION_NO_BODY, declaration.symbol, context)
|
||||
}
|
||||
|
||||
checkExpectDeclarationVisibilityAndBody(declaration, source, reporter, context)
|
||||
}
|
||||
}
|
||||
|
||||
-18
@@ -46,24 +46,6 @@ object FirTopLevelPropertiesChecker : FirPropertyChecker() {
|
||||
reporter,
|
||||
context
|
||||
)
|
||||
checkExpectDeclarationVisibilityAndBody(declaration, source, reporter, context)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: check class too
|
||||
internal fun checkExpectDeclarationVisibilityAndBody(
|
||||
declaration: FirMemberDeclaration,
|
||||
source: KtSourceElement,
|
||||
reporter: DiagnosticReporter,
|
||||
context: CheckerContext
|
||||
) {
|
||||
if (declaration.isExpect) {
|
||||
if (Visibilities.isPrivate(declaration.visibility)) {
|
||||
reporter.reportOn(source, FirErrors.EXPECTED_PRIVATE_DECLARATION, context)
|
||||
}
|
||||
if (declaration is FirSimpleFunction && declaration.hasBody) {
|
||||
reporter.reportOn(source, FirErrors.EXPECTED_DECLARATION_WITH_BODY, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ expect class A {
|
||||
<!EXPECTED_PRIVATE_DECLARATION!>private<!> val bar: String
|
||||
<!EXPECTED_PRIVATE_DECLARATION!>private<!> fun Int.memExt(): Any
|
||||
|
||||
private class Nested
|
||||
<!EXPECTED_PRIVATE_DECLARATION!>private<!> class Nested
|
||||
}
|
||||
|
||||
// MODULE: m1-jvm()()(m1-common)
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<!EXPECTED_PRIVATE_DECLARATION!>private<!> expect val bar: String
|
||||
<!EXPECTED_PRIVATE_DECLARATION!>private<!> expect fun Int.memExt(): Any
|
||||
|
||||
private expect class Foo
|
||||
<!EXPECTED_PRIVATE_DECLARATION!>private<!> expect class Foo
|
||||
|
||||
// MODULE: m2-jvm()()(m1-common)
|
||||
// FILE: jvm.kt
|
||||
|
||||
Reference in New Issue
Block a user