Skip to content

Commit 360c000

Browse files
cypressiousSpace Team
authored andcommitted
[FIR] Contextualize native checkers
1 parent edeacd4 commit 360c000

15 files changed

+96
-102
lines changed

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeHelpers.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
1515
import org.jetbrains.kotlin.name.ClassId
1616
import org.jetbrains.kotlin.name.NativeForwardDeclarationKind
1717

18+
context(context: CheckerContext)
1819
fun DiagnosticReporter.reportIfHasAnnotation(
1920
declaration: FirDeclaration,
2021
annotationClassId: ClassId,
21-
error: KtDiagnosticFactory0,
22-
context: CheckerContext
22+
error: KtDiagnosticFactory0
2323
) {
2424
val annotation = declaration.getAnnotationByClassId(annotationClassId, context.session)
2525
if (annotation != null) {
26-
reportOn(annotation.source, error, context)
26+
reportOn(annotation.source, error)
2727
}
2828
}
2929

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeIdentifierChecker.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ object FirNativeIdentifierChecker : FirBasicDeclarationChecker(MppCheckerKind.Co
2727
override fun check(declaration: FirDeclaration) {
2828
val source = declaration.source
2929
when (declaration) {
30-
is FirRegularClass -> checkNameAndReport(declaration.name, source, context, reporter)
31-
is FirSimpleFunction -> checkNameAndReport(declaration.name, source, context, reporter)
32-
is FirTypeParameter -> checkNameAndReport(declaration.name, source, context, reporter)
33-
is FirProperty -> checkNameAndReport(declaration.name, source, context, reporter)
34-
is FirTypeAlias -> checkNameAndReport(declaration.name, source, context, reporter)
35-
is FirValueParameter -> checkNameAndReport(declaration.name, source, context, reporter)
36-
is FirEnumEntry -> checkNameAndReport(declaration.name, source, context, reporter)
30+
is FirRegularClass -> checkNameAndReport(declaration.name, source)
31+
is FirSimpleFunction -> checkNameAndReport(declaration.name, source)
32+
is FirTypeParameter -> checkNameAndReport(declaration.name, source)
33+
is FirProperty -> checkNameAndReport(declaration.name, source)
34+
is FirTypeAlias -> checkNameAndReport(declaration.name, source)
35+
is FirValueParameter -> checkNameAndReport(declaration.name, source)
36+
is FirEnumEntry -> checkNameAndReport(declaration.name, source)
3737
else -> return
3838
}
3939
}
4040

41-
internal fun checkNameAndReport(name: Name, source: KtSourceElement?, context: CheckerContext, reporter: DiagnosticReporter) {
41+
context(context: CheckerContext, reporter: DiagnosticReporter)
42+
internal fun checkNameAndReport(
43+
name: Name,
44+
source: KtSourceElement?,
45+
) {
4246
if (source != null && source.kind !is KtFakeSourceElementKind && !name.isSpecial) {
4347
val text = name.asString()
4448
val message = when {
@@ -49,7 +53,7 @@ object FirNativeIdentifierChecker : FirBasicDeclarationChecker(MppCheckerKind.Co
4953
}
5054

5155
if (message != null) {
52-
reporter.reportOn(source, FirNativeErrors.INVALID_CHARACTERS_NATIVE, message, context)
56+
reporter.reportOn(source, FirNativeErrors.INVALID_CHARACTERS_NATIVE, message)
5357
}
5458
}
5559
}

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeObjCNameCallableChecker.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sealed class FirNativeObjCNameCallableChecker(mppKind: MppCheckerKind) : FirCall
2222
override fun check(declaration: FirCallableDeclaration) {
2323
val containingClass = context.containingDeclarations.lastOrNull() as? FirClassSymbol<*> ?: return
2424
if (containingClass.isExpect) return
25-
check(declaration, containingClass, context, reporter)
25+
check(declaration, containingClass)
2626
}
2727
}
2828

@@ -31,18 +31,17 @@ sealed class FirNativeObjCNameCallableChecker(mppKind: MppCheckerKind) : FirCall
3131
override fun check(declaration: FirCallableDeclaration) {
3232
val containingClass = context.containingDeclarations.lastOrNull() as? FirClassSymbol<*> ?: return
3333
if (!containingClass.isExpect) return
34-
check(declaration, containingClass, context, reporter)
34+
check(declaration, containingClass)
3535
}
3636
}
3737

38+
context(context: CheckerContext, reporter: DiagnosticReporter)
3839
protected fun check(
3940
declaration: FirCallableDeclaration,
4041
containingClass: FirClassSymbol<*>,
41-
context: CheckerContext,
42-
reporter: DiagnosticReporter,
4342
) {
4443
if (declaration !is FirSimpleFunction && declaration !is FirProperty) return
4544
val firTypeScope = containingClass.unsubstitutedScope(context)
46-
FirNativeObjCNameUtilities.checkCallableMember(firTypeScope, declaration.symbol, declaration, context, reporter)
45+
FirNativeObjCNameUtilities.checkCallableMember(firTypeScope, declaration.symbol, declaration)
4746
}
4847
}

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeObjCNameChecker.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@ import org.jetbrains.kotlin.fir.declarations.FirClass
2626
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
2727
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
2828
import org.jetbrains.kotlin.fir.declarations.utils.isOverride
29-
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
3029
import org.jetbrains.kotlin.fir.expressions.FirLiteralExpression
31-
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
32-
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
33-
import org.jetbrains.kotlin.name.ClassId
34-
import org.jetbrains.kotlin.name.FqName
35-
import org.jetbrains.kotlin.name.Name
3630

3731
object FirNativeObjCNameChecker : FirBasicDeclarationChecker(MppCheckerKind.Platform) {
3832
context(context: CheckerContext, reporter: DiagnosticReporter)
@@ -45,42 +39,46 @@ object FirNativeObjCNameChecker : FirBasicDeclarationChecker(MppCheckerKind.Plat
4539
reporter.reportOn(objCName.annotation.source, INAPPLICABLE_OBJC_NAME)
4640
}
4741
}
48-
objCNames.forEach { checkObjCName(it, declaration, context, reporter) }
42+
objCNames.forEach { checkObjCName(it, declaration) }
4943
}
5044

5145
// We only allow valid ObjC identifiers (even for Swift names)
5246
private val validFirstChars = ('A'..'Z').toSet() + ('a'..'z').toSet() + '_'
5347
private val validChars = validFirstChars + ('0'..'9').toSet()
5448

55-
private fun checkObjCName(objCName: ObjCName, declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
49+
context(context: CheckerContext, reporter: DiagnosticReporter)
50+
private fun checkObjCName(
51+
objCName: ObjCName,
52+
declaration: FirDeclaration,
53+
) {
5654
val annotationSource = objCName.annotation.source
5755
for ((_, argument) in objCName.annotation.argumentMapping.mapping) {
5856
if (argument is FirLiteralExpression) continue
59-
reporter.reportOn(argument.source, NON_LITERAL_OBJC_NAME_ARG, context)
57+
reporter.reportOn(argument.source, NON_LITERAL_OBJC_NAME_ARG)
6058
}
6159
if (objCName.name == null && objCName.swiftName == null) {
62-
reporter.reportOn(annotationSource, INVALID_OBJC_NAME, context)
60+
reporter.reportOn(annotationSource, INVALID_OBJC_NAME)
6361
}
6462
val invalidNameFirstChar = objCName.name?.firstOrNull()?.takeUnless(validFirstChars::contains)
6563
val invalidSwiftNameFirstChar = objCName.swiftName?.firstOrNull()?.takeUnless(validFirstChars::contains)
6664
val invalidFirstChars = setOfNotNull(invalidNameFirstChar, invalidSwiftNameFirstChar)
6765
if (invalidFirstChars.isNotEmpty()) {
68-
reporter.reportOn(annotationSource, INVALID_OBJC_NAME_FIRST_CHAR, invalidFirstChars.joinToString(""), context)
66+
reporter.reportOn(annotationSource, INVALID_OBJC_NAME_FIRST_CHAR, invalidFirstChars.joinToString(""))
6967
}
7068
if (objCName.name?.isEmpty() == true || objCName.swiftName?.isEmpty() == true) {
71-
reporter.reportOn(annotationSource, EMPTY_OBJC_NAME, context)
69+
reporter.reportOn(annotationSource, EMPTY_OBJC_NAME)
7270
}
7371
val invalidNameChars = objCName.name?.toSet()?.subtract(validChars) ?: emptySet()
7472
val invalidSwiftNameChars = objCName.swiftName?.toSet()?.subtract(validChars) ?: emptySet()
7573
val invalidChars = invalidNameChars + invalidSwiftNameChars
7674
if (invalidChars.isNotEmpty()) {
77-
reporter.reportOn(annotationSource, INVALID_OBJC_NAME_CHARS, invalidFirstChars.joinToString(""), context)
75+
reporter.reportOn(annotationSource, INVALID_OBJC_NAME_CHARS, invalidFirstChars.joinToString(""))
7876
}
7977
if (objCName.exact && (declaration !is FirClass || declaration.classKind == ClassKind.ENUM_ENTRY)) {
80-
reporter.reportOn(annotationSource, INAPPLICABLE_EXACT_OBJC_NAME, context)
78+
reporter.reportOn(annotationSource, INAPPLICABLE_EXACT_OBJC_NAME)
8179
}
8280
if (objCName.exact && objCName.name == null) {
83-
reporter.reportOn(annotationSource, MISSING_EXACT_OBJC_NAME, context)
81+
reporter.reportOn(annotationSource, MISSING_EXACT_OBJC_NAME)
8482
}
8583
}
8684
}

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeObjCNameOverridesChecker.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ sealed class FirNativeObjCNameOverridesChecker(mppKind: MppCheckerKind) : FirCla
4040
val firTypeScope = declaration.unsubstitutedScope(context)
4141
firTypeScope.processAllFunctions { symbol ->
4242
if (!symbol.isIntersectionOverride) return@processAllFunctions
43-
checkCallableMember(firTypeScope, symbol, declaration, context, reporter)
43+
checkCallableMember(firTypeScope, symbol, declaration)
4444
}
4545
firTypeScope.processAllProperties { symbol ->
4646
if (!symbol.isIntersectionOverride) return@processAllProperties
47-
checkCallableMember(firTypeScope, symbol, declaration, context, reporter)
47+
checkCallableMember(firTypeScope, symbol, declaration)
4848
}
4949
}
5050
}

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeObjCNameUtilities.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,16 @@ object FirNativeObjCNameUtilities {
7676
}
7777
}
7878

79+
context(context: CheckerContext, reporter: DiagnosticReporter)
7980
fun checkCallableMember(
8081
firTypeScope: FirTypeScope,
8182
memberSymbol: FirCallableSymbol<*>,
8283
declarationToReport: FirDeclaration,
83-
context: CheckerContext,
84-
reporter: DiagnosticReporter
8584
) {
8685
val overriddenSymbols =
8786
firTypeScope.getDirectOverriddenSafe(memberSymbol).map { it.unwrapSubstitutionOverrides() }
8887
if (overriddenSymbols.isEmpty()) return
89-
val objCNames = overriddenSymbols.map { it.getFirstBaseSymbol(context).getObjCNames(context.session) }
88+
val objCNames = overriddenSymbols.map { it.getFirstBaseSymbol().getObjCNames(context.session) }
9089
if (!objCNames.allNamesEquals()) {
9190
val containingDeclarations = overriddenSymbols.mapNotNull {
9291
it.containingClassLookupTag()?.toRegularClassSymbol(context.session)
@@ -95,18 +94,18 @@ object FirNativeObjCNameUtilities {
9594
declarationToReport.source,
9695
FirNativeErrors.INCOMPATIBLE_OBJC_NAME_OVERRIDE,
9796
memberSymbol,
98-
containingDeclarations,
99-
context
97+
containingDeclarations
10098
)
10199
}
102100
}
103101

104-
private fun FirCallableSymbol<*>.getFirstBaseSymbol(context: CheckerContext): FirCallableSymbol<*> {
102+
context(context: CheckerContext)
103+
private fun FirCallableSymbol<*>.getFirstBaseSymbol(): FirCallableSymbol<*> {
105104
val session = context.session
106105
val ownScope = containingClassLookupTag()?.toSymbol(session)?.fullyExpandedClass(session)?.unsubstitutedScope(context)
107106
?: return this
108107
val overriddenMemberSymbols = ownScope.getDirectOverriddenSafe(this).map { it.unwrapSubstitutionOverrides() }
109-
return if (overriddenMemberSymbols.isEmpty()) this else overriddenMemberSymbols.first().getFirstBaseSymbol(context)
108+
return if (overriddenMemberSymbols.isEmpty()) this else overriddenMemberSymbols.first().getFirstBaseSymbol()
110109
}
111110

112111
private fun List<List<ObjCName?>>.allNamesEquals(): Boolean {

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeObjCRefinementChecker.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ object FirNativeObjCRefinementChecker : FirCallableDeclarationChecker(MppChecker
4040
firTypeScope,
4141
declaration.symbol,
4242
declaration,
43-
context,
44-
reporter,
4543
objCAnnotations,
4644
swiftAnnotations
4745
)

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeObjCRefinementOverridesChecker.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,20 @@ sealed class FirNativeObjCRefinementOverridesChecker(mppKind: MppCheckerKind) :
5353
val baseScope = declaration.unsubstitutedScope(context)
5454
baseScope.processAllFunctions { symbol ->
5555
if (!symbol.isIntersectionOverride) return@processAllFunctions
56-
check(baseScope, symbol, declaration, context, reporter, emptyList(), emptyList())
56+
check(baseScope, symbol, declaration, emptyList(), emptyList())
5757
}
5858
baseScope.processAllProperties { symbol ->
5959
if (!symbol.isIntersectionOverride) return@processAllProperties
60-
check(baseScope, symbol, declaration, context, reporter, emptyList(), emptyList())
60+
check(baseScope, symbol, declaration, emptyList(), emptyList())
6161
}
6262
}
6363

6464
companion object {
65+
context(context: CheckerContext, reporter: DiagnosticReporter)
6566
fun check(
6667
baseScope: FirTypeScope,
6768
memberSymbol: FirCallableSymbol<*>,
6869
declarationToReport: FirDeclaration,
69-
context: CheckerContext,
70-
reporter: DiagnosticReporter,
7170
objCAnnotations: List<FirAnnotation>,
7271
swiftAnnotations: List<FirAnnotation>
7372
) {
@@ -83,10 +82,10 @@ sealed class FirNativeObjCRefinementOverridesChecker(mppKind: MppCheckerKind) :
8382
if (superIsRefinedInSwift) isRefinedInSwift = true else supersNotRefinedInSwift.add(symbol)
8483
}
8584
if (isHiddenFromObjC && supersNotHiddenFromObjC.isNotEmpty()) {
86-
reporter.reportIncompatibleOverride(declarationToReport, objCAnnotations, supersNotHiddenFromObjC, context)
85+
reporter.reportIncompatibleOverride(declarationToReport, objCAnnotations, supersNotHiddenFromObjC)
8786
}
8887
if (isRefinedInSwift && supersNotRefinedInSwift.isNotEmpty()) {
89-
reporter.reportIncompatibleOverride(declarationToReport, swiftAnnotations, supersNotRefinedInSwift, context)
88+
reporter.reportIncompatibleOverride(declarationToReport, swiftAnnotations, supersNotRefinedInSwift)
9089
}
9190
}
9291

@@ -123,18 +122,19 @@ sealed class FirNativeObjCRefinementOverridesChecker(mppKind: MppCheckerKind) :
123122
return hasObjC to hasSwift
124123
}
125124

125+
context(context: CheckerContext)
126126
private fun DiagnosticReporter.reportIncompatibleOverride(
127127
declaration: FirDeclaration,
128128
annotations: List<FirAnnotation>,
129-
notRefinedSupers: List<FirCallableSymbol<*>>,
130-
context: CheckerContext
129+
notRefinedSupers: List<FirCallableSymbol<*>>
131130
) {
132-
val containingDeclarations = notRefinedSupers.mapNotNull { it.containingClassLookupTag()?.toRegularClassSymbol(context.session) }
131+
val containingDeclarations =
132+
notRefinedSupers.mapNotNull { it.containingClassLookupTag()?.toRegularClassSymbol(context.session) }
133133
if (annotations.isEmpty()) {
134-
reportOn(declaration.source, INCOMPATIBLE_OBJC_REFINEMENT_OVERRIDE, declaration.symbol, containingDeclarations, context)
134+
reportOn(declaration.source, INCOMPATIBLE_OBJC_REFINEMENT_OVERRIDE, declaration.symbol, containingDeclarations)
135135
} else {
136136
for (annotation in annotations) {
137-
reportOn(annotation.source, INCOMPATIBLE_OBJC_REFINEMENT_OVERRIDE, declaration.symbol, containingDeclarations, context)
137+
reportOn(annotation.source, INCOMPATIBLE_OBJC_REFINEMENT_OVERRIDE, declaration.symbol, containingDeclarations)
138138
}
139139
}
140140
}

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeObjcOverrideApplicabilityChecker.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
2121
import org.jetbrains.kotlin.name.NativeStandardInteropNames.Annotations.objCSignatureOverrideClassId
2222
import org.jetbrains.kotlin.utils.SmartSet
2323

24-
private fun FirFunctionSymbol<*>.isInheritedFromObjc(context: CheckerContext): Boolean {
24+
context(context: CheckerContext)
25+
private fun FirFunctionSymbol<*>.isInheritedFromObjc(): Boolean {
2526
return getObjCMethodInfoFromOverriddenFunctions(context.session, context.scopeSession) != null
2627
}
2728

@@ -37,14 +38,17 @@ private fun FirFunctionSymbol<*>.hasDifferentParameterNames(other: FirFunctionSy
3738
}
3839

3940
object NativeConflictDeclarationsDiagnosticDispatcher : PlatformConflictDeclarationsDiagnosticDispatcher {
41+
context(context: CheckerContext)
4042
override fun getDiagnostic(
4143
conflictingDeclaration: FirBasedSymbol<*>,
42-
symbols: SmartSet<FirBasedSymbol<*>>,
43-
context: CheckerContext
44+
symbols: SmartSet<FirBasedSymbol<*>>
4445
): KtDiagnosticFactory1<Collection<FirBasedSymbol<*>>>? {
4546
if (context.languageVersionSettings.supportsFeature(LanguageFeature.ObjCSignatureOverrideAnnotation)) {
4647
if (conflictingDeclaration is FirFunctionSymbol<*> && symbols.all { it is FirFunctionSymbol<*> }) {
47-
if (conflictingDeclaration.isInheritedFromObjc(context) && symbols.all { (it as FirFunctionSymbol<*>).isInheritedFromObjc(context) }) {
48+
if (conflictingDeclaration.isInheritedFromObjc() && symbols.all {
49+
(it as FirFunctionSymbol<*>).isInheritedFromObjc(
50+
)
51+
}) {
4852
if (symbols.all { (it as FirFunctionSymbol<*>).hasDifferentParameterNames(conflictingDeclaration) }) {
4953
if (conflictingDeclaration.hasAnnotation(objCSignatureOverrideClassId, context.session)) {
5054
return null
@@ -55,15 +59,15 @@ object NativeConflictDeclarationsDiagnosticDispatcher : PlatformConflictDeclarat
5559
}
5660
}
5761
}
58-
return PlatformConflictDeclarationsDiagnosticDispatcher.DEFAULT.getDiagnostic(conflictingDeclaration, symbols, context)
62+
return PlatformConflictDeclarationsDiagnosticDispatcher.DEFAULT.getDiagnostic(conflictingDeclaration, symbols)
5963
}
6064
}
6165

6266
object FirNativeObjcOverrideApplicabilityChecker : FirFunctionChecker(MppCheckerKind.Platform) {
6367
context(context: CheckerContext, reporter: DiagnosticReporter)
6468
override fun check(declaration: FirFunction) {
6569
if (declaration.hasAnnotation(objCSignatureOverrideClassId, context.session)) {
66-
if (!declaration.symbol.isInheritedFromObjc(context)) {
70+
if (!declaration.symbol.isInheritedFromObjc()) {
6771
reporter.reportOn(
6872
declaration.getAnnotationByClassId(objCSignatureOverrideClassId, context.session)?.source,
6973
FirNativeErrors.INAPPLICABLE_OBJC_OVERRIDE

compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativePackageDirectiveChecker.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ object FirNativePackageDirectiveChecker : FirFileChecker(MppCheckerKind.Common)
2222
declaration.packageDirective.source?.forEachChildOfType(setOf(REFERENCE_EXPRESSION), depth = -1) {
2323
checkNameAndReport(
2424
Name.identifier(it.text.toString()),
25-
it,
26-
context,
27-
reporter
25+
it
2826
)
2927
}
3028
}

0 commit comments

Comments
 (0)