[K/N] KT-58562: Implement frontend checkers for HiddenFromObjC on classes
This commit is contained in:
committed by
Space Team
parent
f3a22e0ac4
commit
c57c34525f
+3
-1
@@ -46,7 +46,6 @@ object NATIVE_DIAGNOSTICS_LIST : DiagnosticList("FirNativeErrors") {
|
||||
parameter<FirBasedSymbol<*>>("declaration")
|
||||
parameter<Collection<FirRegularClassSymbol>>("containingClasses")
|
||||
}
|
||||
val INVALID_OBJC_REFINEMENT_TARGETS by error<KtElement>()
|
||||
val INAPPLICABLE_OBJC_NAME by error<KtElement>()
|
||||
val INVALID_OBJC_NAME by error<KtElement>()
|
||||
val INVALID_OBJC_NAME_CHARS by error<KtElement> {
|
||||
@@ -63,5 +62,8 @@ object NATIVE_DIAGNOSTICS_LIST : DiagnosticList("FirNativeErrors") {
|
||||
val INAPPLICABLE_EXACT_OBJC_NAME by error<KtElement>()
|
||||
val MISSING_EXACT_OBJC_NAME by error<KtElement>()
|
||||
val NON_LITERAL_OBJC_NAME_ARG by error<KtElement>()
|
||||
val INVALID_OBJC_HIDES_TARGETS by error<KtElement>()
|
||||
val INVALID_REFINES_IN_SWIFT_TARGETS by error<KtElement>()
|
||||
val SUBTYPE_OF_HIDDEN_FROM_OBJC by error<KtElement>()
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -35,7 +35,6 @@ object FirNativeErrors {
|
||||
val INVALID_CHARACTERS_NATIVE by deprecationError1<PsiElement, String>(ProhibitInvalidCharsInNativeIdentifiers, SourceElementPositioningStrategies.NAME_IDENTIFIER)
|
||||
val REDUNDANT_SWIFT_REFINEMENT by error0<KtElement>()
|
||||
val INCOMPATIBLE_OBJC_REFINEMENT_OVERRIDE by error2<KtElement, FirBasedSymbol<*>, Collection<FirRegularClassSymbol>>()
|
||||
val INVALID_OBJC_REFINEMENT_TARGETS by error0<KtElement>()
|
||||
val INAPPLICABLE_OBJC_NAME by error0<KtElement>()
|
||||
val INVALID_OBJC_NAME by error0<KtElement>()
|
||||
val INVALID_OBJC_NAME_CHARS by error1<KtElement, String>()
|
||||
@@ -45,6 +44,9 @@ object FirNativeErrors {
|
||||
val INAPPLICABLE_EXACT_OBJC_NAME by error0<KtElement>()
|
||||
val MISSING_EXACT_OBJC_NAME by error0<KtElement>()
|
||||
val NON_LITERAL_OBJC_NAME_ARG by error0<KtElement>()
|
||||
val INVALID_OBJC_HIDES_TARGETS by error0<KtElement>()
|
||||
val INVALID_REFINES_IN_SWIFT_TARGETS by error0<KtElement>()
|
||||
val SUBTYPE_OF_HIDDEN_FROM_OBJC by error0<KtElement>()
|
||||
|
||||
init {
|
||||
RootDiagnosticRendererFactory.registerFactory(FirNativeErrorsDefaultMessages)
|
||||
|
||||
+10
-3
@@ -22,14 +22,16 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INCO
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INCOMPATIBLE_THROWS_INHERITED
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INCOMPATIBLE_THROWS_OVERRIDE
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INVALID_CHARACTERS_NATIVE
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INVALID_OBJC_HIDES_TARGETS
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INVALID_OBJC_NAME
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INVALID_OBJC_NAME_CHARS
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INVALID_OBJC_NAME_FIRST_CHAR
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INVALID_OBJC_REFINEMENT_TARGETS
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INVALID_REFINES_IN_SWIFT_TARGETS
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.MISSING_EXACT_OBJC_NAME
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.MISSING_EXCEPTION_IN_THROWS_ON_SUSPEND
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.REDUNDANT_SWIFT_REFINEMENT
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.NON_LITERAL_OBJC_NAME_ARG
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.SUBTYPE_OF_HIDDEN_FROM_OBJC
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.THROWS_LIST_EMPTY
|
||||
|
||||
object FirNativeErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
|
||||
@@ -60,8 +62,12 @@ object FirNativeErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
|
||||
SYMBOLS
|
||||
)
|
||||
map.put(
|
||||
INVALID_OBJC_REFINEMENT_TARGETS,
|
||||
"Refines annotations are only applicable to annotations with targets CLASS, FUNCTION and/or PROPERTY"
|
||||
INVALID_OBJC_HIDES_TARGETS,
|
||||
"@HidesFromObjC annotation is only applicable to annotations with targets CLASS, FUNCTION and/or PROPERTY"
|
||||
)
|
||||
map.put(
|
||||
INVALID_REFINES_IN_SWIFT_TARGETS,
|
||||
"@RefinesInSwift annotation is only applicable to annotations with targets FUNCTION and/or PROPERTY"
|
||||
)
|
||||
map.put(INAPPLICABLE_OBJC_NAME, "@ObjCName is not applicable on overrides")
|
||||
map.put(INVALID_OBJC_NAME, "@ObjCName should have a name and/or swiftName")
|
||||
@@ -72,5 +78,6 @@ object FirNativeErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
|
||||
map.put(INAPPLICABLE_EXACT_OBJC_NAME, "Exact @ObjCName is only applicable to classes, objects and interfaces")
|
||||
map.put(MISSING_EXACT_OBJC_NAME, "Exact @ObjCName is required to have an ObjC name")
|
||||
map.put(NON_LITERAL_OBJC_NAME_ARG, "@ObjCName accepts only literal string and boolean values")
|
||||
map.put(SUBTYPE_OF_HIDDEN_FROM_OBJC, "Only @HiddenFromObjC declaration can be a subtype of @HiddenFromObjC declaration")
|
||||
}
|
||||
}
|
||||
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.native.checkers
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.diagnostics.reportOn
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.classKind
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirRegularClassChecker
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.getContainingClassSymbol
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors
|
||||
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
|
||||
import org.jetbrains.kotlin.fir.declarations.toAnnotationClassLikeSymbol
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.superConeTypes
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.visibility
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
|
||||
/**
|
||||
* Check that the given class does not inherit from class or implements interface that is
|
||||
* marked as HiddenFromObjC (aka "marked with annotation that is marked as HidesFromObjC").
|
||||
*/
|
||||
object FirNativeHiddenFromObjCInheritanceChecker : FirRegularClassChecker() {
|
||||
override fun check(declaration: FirRegularClass, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
// Enum entries inherit from their enum class.
|
||||
if (declaration.classKind == ClassKind.ENUM_ENTRY) {
|
||||
return
|
||||
}
|
||||
// Non-public types do not leak to Objective-C API surface, so it is OK for them
|
||||
// to inherit from hidden types.
|
||||
if (!declaration.visibility.isPublicAPI) return
|
||||
val session = context.session
|
||||
// No need to report anything on class that is hidden itself.
|
||||
if (checkIsHiddenFromObjC(declaration.symbol, session)) {
|
||||
return
|
||||
}
|
||||
|
||||
val superTypes = declaration.superConeTypes
|
||||
.filterNot { it.isAny || it.isNullableAny }
|
||||
.mapNotNull { it.toSymbol(session) }
|
||||
|
||||
superTypes.firstOrNull { st -> checkIsHiddenFromObjC(st, session) }?.let {
|
||||
reporter.reportOn(declaration.source, FirNativeErrors.SUBTYPE_OF_HIDDEN_FROM_OBJC, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkContainingClassIsHidden(classSymbol: FirClassLikeSymbol<*>, session: FirSession): Boolean {
|
||||
return classSymbol.getContainingClassSymbol(session)?.let {
|
||||
if (checkIsHiddenFromObjC(it, session)) {
|
||||
true
|
||||
} else {
|
||||
checkContainingClassIsHidden(it, session)
|
||||
}
|
||||
} ?: false
|
||||
}
|
||||
|
||||
private fun checkIsHiddenFromObjC(classSymbol: FirClassLikeSymbol<*>, session: FirSession): Boolean {
|
||||
classSymbol.annotations.forEach { annotation ->
|
||||
val annotationClass = annotation.toAnnotationClassLikeSymbol(session) ?: return@forEach
|
||||
val objCExportMetaAnnotations = annotationClass.annotations.findMetaAnnotations(session)
|
||||
if (objCExportMetaAnnotations.hidesFromObjCAnnotation != null) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return checkContainingClassIsHidden(classSymbol, session)
|
||||
}
|
||||
+32
-17
@@ -6,9 +6,11 @@
|
||||
package org.jetbrains.kotlin.fir.analysis.native.checkers
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.diagnostics.reportOn
|
||||
import org.jetbrains.kotlin.fir.FirAnnotationContainer
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirRegularClassChecker
|
||||
@@ -21,12 +23,13 @@ import org.jetbrains.kotlin.fir.expressions.FirAnnotation
|
||||
|
||||
object FirNativeObjCRefinementAnnotationChecker : FirRegularClassChecker() {
|
||||
|
||||
private val supportedTargets = arrayOf(KotlinTarget.FUNCTION, KotlinTarget.PROPERTY, KotlinTarget.CLASS)
|
||||
private val hidesFromObjCSupportedTargets = arrayOf(KotlinTarget.FUNCTION, KotlinTarget.PROPERTY, KotlinTarget.CLASS)
|
||||
private val refinesInSwiftSupportedTargets = arrayOf(KotlinTarget.FUNCTION, KotlinTarget.PROPERTY)
|
||||
|
||||
override fun check(declaration: FirRegularClass, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
if (declaration.classKind != ClassKind.ANNOTATION_CLASS) return
|
||||
val session = context.session
|
||||
val (objCAnnotation, swiftAnnotation) = declaration.findMetaAnnotations(session)
|
||||
val (objCAnnotation, swiftAnnotation) = declaration.annotations.findMetaAnnotations(session)
|
||||
if (objCAnnotation == null && swiftAnnotation == null) return
|
||||
if (objCAnnotation != null && swiftAnnotation != null) {
|
||||
reporter.reportOn(
|
||||
@@ -36,23 +39,35 @@ object FirNativeObjCRefinementAnnotationChecker : FirRegularClassChecker() {
|
||||
)
|
||||
}
|
||||
val targets = declaration.getAllowedAnnotationTargets(session)
|
||||
val unsupportedTargets = targets - supportedTargets
|
||||
if (unsupportedTargets.isNotEmpty()) {
|
||||
objCAnnotation?.let { reporter.reportOn(it.source, FirNativeErrors.INVALID_OBJC_REFINEMENT_TARGETS, context) }
|
||||
swiftAnnotation?.let { reporter.reportOn(it.source, FirNativeErrors.INVALID_OBJC_REFINEMENT_TARGETS, context) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirRegularClass.findMetaAnnotations(session: FirSession): Pair<FirAnnotation?, FirAnnotation?> {
|
||||
var objCAnnotation: FirAnnotation? = null
|
||||
var swiftAnnotation: FirAnnotation? = null
|
||||
for (annotation in annotations) {
|
||||
when (annotation.toAnnotationClassId(session)) {
|
||||
hidesFromObjCClassId -> objCAnnotation = annotation
|
||||
refinesInSwiftClassId -> swiftAnnotation = annotation
|
||||
objCAnnotation?.let {
|
||||
if ((targets - hidesFromObjCSupportedTargets).isNotEmpty()) {
|
||||
reporter.reportOn(it.source, FirNativeErrors.INVALID_OBJC_HIDES_TARGETS, context)
|
||||
}
|
||||
}
|
||||
swiftAnnotation?.let {
|
||||
if ((targets - refinesInSwiftSupportedTargets).isNotEmpty()) {
|
||||
reporter.reportOn(it.source, FirNativeErrors.INVALID_REFINES_IN_SWIFT_TARGETS, context)
|
||||
}
|
||||
if (objCAnnotation != null && swiftAnnotation != null) break
|
||||
}
|
||||
return objCAnnotation to swiftAnnotation
|
||||
}
|
||||
}
|
||||
|
||||
internal data class ObjCExportMetaAnnotations(
|
||||
val hidesFromObjCAnnotation: FirAnnotation?,
|
||||
val refinesInSwiftAnnotation: FirAnnotation?,
|
||||
)
|
||||
|
||||
internal fun List<FirAnnotation>.findMetaAnnotations(session: FirSession): ObjCExportMetaAnnotations {
|
||||
var objCAnnotation: FirAnnotation? = null
|
||||
var swiftAnnotation: FirAnnotation? = null
|
||||
for (annotation in this) {
|
||||
when (annotation.toAnnotationClassId(session)) {
|
||||
hidesFromObjCClassId -> objCAnnotation = annotation
|
||||
refinesInSwiftClassId -> swiftAnnotation = annotation
|
||||
}
|
||||
if (objCAnnotation != null && swiftAnnotation != null) break
|
||||
}
|
||||
return ObjCExportMetaAnnotations(objCAnnotation, swiftAnnotation)
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -30,6 +30,7 @@ object NativeDeclarationCheckers : DeclarationCheckers() {
|
||||
|
||||
override val regularClassCheckers: Set<FirRegularClassChecker>
|
||||
get() = setOf(
|
||||
FirNativeObjCRefinementAnnotationChecker
|
||||
FirNativeObjCRefinementAnnotationChecker,
|
||||
FirNativeHiddenFromObjCInheritanceChecker,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user