[FIR] Implement FirNativeThreadLocalChecker
* INAPPLICABLE_THREAD_LOCAL * INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL
This commit is contained in:
+2
@@ -28,5 +28,7 @@ object NATIVE_DIAGNOSTICS_LIST : DiagnosticList("FirNativeErrors") {
|
||||
}
|
||||
val INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY by error<KtElement>()
|
||||
val INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL by error<KtElement>()
|
||||
val INAPPLICABLE_THREAD_LOCAL by error<KtElement>()
|
||||
val INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL by error<KtElement>()
|
||||
}
|
||||
}
|
||||
+2
@@ -26,6 +26,8 @@ object FirNativeErrors {
|
||||
val MISSING_EXCEPTION_IN_THROWS_ON_SUSPEND by error1<KtElement, FqName>()
|
||||
val INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY by error0<KtElement>()
|
||||
val INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL by error0<KtElement>()
|
||||
val INAPPLICABLE_THREAD_LOCAL by error0<KtElement>()
|
||||
val INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL by error0<KtElement>()
|
||||
|
||||
init {
|
||||
RootDiagnosticRendererFactory.registerFactory(FirNativeErrorsDefaultMessages)
|
||||
|
||||
+7
@@ -13,6 +13,8 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticRenderers.SYMB
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.checkMissingMessages
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INAPPLICABLE_THREAD_LOCAL
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors.INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL
|
||||
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.MISSING_EXCEPTION_IN_THROWS_ON_SUSPEND
|
||||
@@ -32,6 +34,11 @@ object FirNativeErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
|
||||
"@SharedImmutable is applicable only to val with backing field or to property with delegation"
|
||||
)
|
||||
map.put(INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL, "@SharedImmutable is applicable only to top level declarations")
|
||||
map.put(
|
||||
INAPPLICABLE_THREAD_LOCAL,
|
||||
"@ThreadLocal is applicable only to property with backing field, to property with delegation or to objects"
|
||||
)
|
||||
map.put(INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL, "@ThreadLocal is applicable only to top level declarations")
|
||||
|
||||
map.checkMissingMessages(FirNativeErrors)
|
||||
}
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2022 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.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0
|
||||
import org.jetbrains.kotlin.diagnostics.reportOn
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.getAnnotationByClassId
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
fun DiagnosticReporter.reportIfHasAnnotation(
|
||||
declaration: FirDeclaration,
|
||||
annotationClassId: ClassId,
|
||||
error: KtDiagnosticFactory0,
|
||||
context: CheckerContext
|
||||
) {
|
||||
val annotation = declaration.getAnnotationByClassId(annotationClassId)
|
||||
if (annotation != null) {
|
||||
reportOn(annotation.source, error, context)
|
||||
}
|
||||
}
|
||||
+19
-13
@@ -18,26 +18,30 @@ import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
object FirNativeSharedImmutableChecker : FirBasicDeclarationChecker() {
|
||||
private val sharedImmutableFqName = ClassId.topLevel(FqName("kotlin.native.concurrent.SharedImmutable"))
|
||||
private val sharedImmutableClassId = ClassId.topLevel(FqName("kotlin.native.concurrent.SharedImmutable"))
|
||||
|
||||
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
if (declaration is FirVariable) {
|
||||
if (declaration !is FirValueParameter || context.containingDeclarations.lastOrNull() !is FirPrimaryConstructor) {
|
||||
val hasBackingField = declaration is FirProperty && declaration.hasBackingField
|
||||
if ((declaration.isVar || !hasBackingField) && declaration.delegate == null) {
|
||||
val annotation = declaration.getAnnotationByClassId(sharedImmutableFqName)
|
||||
if (annotation != null) {
|
||||
reporter.reportOn(annotation.source, FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY, context)
|
||||
}
|
||||
reporter.reportIfHasAnnotation(
|
||||
declaration,
|
||||
sharedImmutableClassId,
|
||||
FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY,
|
||||
context
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (declaration.source?.kind is KtFakeSourceElementKind) return
|
||||
|
||||
val annotation = declaration.getAnnotationByClassId(sharedImmutableFqName)
|
||||
if (annotation != null) {
|
||||
reporter.reportOn(annotation.source, FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY, context)
|
||||
}
|
||||
reporter.reportIfHasAnnotation(
|
||||
declaration,
|
||||
sharedImmutableClassId,
|
||||
FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY,
|
||||
context
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -45,10 +49,12 @@ object FirNativeSharedImmutableChecker : FirBasicDeclarationChecker() {
|
||||
if (declaration.source?.kind is KtFakeSourceElementKind) return
|
||||
|
||||
if (context.containingDeclarations.lastOrNull() !is FirFile) {
|
||||
val annotation = declaration.getAnnotationByClassId(sharedImmutableFqName)
|
||||
if (annotation != null) {
|
||||
reporter.reportOn(annotation.source, FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL, context)
|
||||
}
|
||||
reporter.reportIfHasAnnotation(
|
||||
declaration,
|
||||
sharedImmutableClassId,
|
||||
FirNativeErrors.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL,
|
||||
context
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2010-2022 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.KtFakeSourceElementKind
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirBasicDeclarationChecker
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.native.FirNativeErrors
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.hasBackingField
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
object FirNativeThreadLocalChecker : FirBasicDeclarationChecker() {
|
||||
private val threadLocalClassId = ClassId.topLevel(FqName("kotlin.native.concurrent.ThreadLocal"))
|
||||
|
||||
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
val isObject = declaration is FirClass && declaration.classKind == ClassKind.OBJECT
|
||||
val isOk = declaration is FirVariable &&
|
||||
(declaration is FirProperty && declaration.hasBackingField || declaration.delegate != null) || isObject
|
||||
if (!isOk) {
|
||||
reporter.reportIfHasAnnotation(declaration, threadLocalClassId, FirNativeErrors.INAPPLICABLE_THREAD_LOCAL, context)
|
||||
}
|
||||
|
||||
if (declaration.source?.kind is KtFakeSourceElementKind) return
|
||||
|
||||
if (context.containingDeclarations.lastOrNull() !is FirFile && !isObject) {
|
||||
reporter.reportIfHasAnnotation(declaration, threadLocalClassId, FirNativeErrors.INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.resolve.annotations.KOTLIN_THROWS_ANNOTATION_FQ_NAME
|
||||
|
||||
object FirNativeThrowsChecker : FirBasicDeclarationChecker() {
|
||||
private val throwsFqName = ClassId.topLevel(KOTLIN_THROWS_ANNOTATION_FQ_NAME)
|
||||
private val throwsClassId = ClassId.topLevel(KOTLIN_THROWS_ANNOTATION_FQ_NAME)
|
||||
|
||||
private val cancellationExceptionFqName = FqName("kotlin.coroutines.cancellation.CancellationException")
|
||||
|
||||
@@ -43,7 +43,7 @@ object FirNativeThrowsChecker : FirBasicDeclarationChecker() {
|
||||
)
|
||||
|
||||
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
val throwsAnnotation = declaration.getAnnotationByClassId(throwsFqName) as? FirAnnotationCall
|
||||
val throwsAnnotation = declaration.getAnnotationByClassId(throwsClassId) as? FirAnnotationCall
|
||||
|
||||
if (!checkInheritance(declaration, throwsAnnotation, context, reporter)) return
|
||||
|
||||
@@ -121,7 +121,7 @@ object FirNativeThrowsChecker : FirBasicDeclarationChecker() {
|
||||
val annotation = if (overriddenFunction.isSubstitutionOrIntersectionOverride) {
|
||||
null
|
||||
} else {
|
||||
overriddenFunction.getAnnotationByClassId(throwsFqName) as? FirAnnotationCall
|
||||
overriddenFunction.getAnnotationByClassId(throwsClassId) as? FirAnnotationCall
|
||||
}
|
||||
getInheritedThrows(annotation, overriddenFunction)
|
||||
}
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ object NativeDeclarationCheckers : DeclarationCheckers() {
|
||||
override val basicDeclarationCheckers: Set<FirBasicDeclarationChecker>
|
||||
get() = setOf(
|
||||
FirNativeThrowsChecker,
|
||||
FirNativeSharedImmutableChecker
|
||||
FirNativeSharedImmutableChecker,
|
||||
FirNativeThreadLocalChecker
|
||||
)
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
// FILE: annotation.kt
|
||||
package kotlin.native.concurrent
|
||||
|
||||
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class ThreadLocal
|
||||
|
||||
// FILE: test.kt
|
||||
import kotlin.native.concurrent.ThreadLocal
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
data class Point(val x: Double, val y: Double)
|
||||
|
||||
class Person(val name: String) {
|
||||
@ThreadLocal
|
||||
var surname: String? = null
|
||||
}
|
||||
|
||||
abstract class Information {
|
||||
abstract var field: String
|
||||
}
|
||||
|
||||
@ThreadLocal
|
||||
class Person1(val name: String) {
|
||||
var surname: String? = null
|
||||
@ThreadLocal
|
||||
val extraInfo: Information = object : Information() {
|
||||
override var field: String = "extra info"
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadLocal
|
||||
val extraInfo: Information = object : Information() {
|
||||
override var field: String = "extra info"
|
||||
}
|
||||
|
||||
@ThreadLocal
|
||||
val point1 = Point(1.0, 1.0)
|
||||
|
||||
@ThreadLocal
|
||||
val cornerPoint: Point
|
||||
get() = point1
|
||||
|
||||
@ThreadLocal
|
||||
val person = Person1("aaaaa")
|
||||
|
||||
class Delegate {
|
||||
val value: Int = 10
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
|
||||
return value
|
||||
}
|
||||
|
||||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadLocal
|
||||
var field1: Int by Delegate()
|
||||
|
||||
@ThreadLocal
|
||||
object WithDelegate {
|
||||
var field1: Int by Delegate()
|
||||
}
|
||||
|
||||
class Bar {
|
||||
@ThreadLocal
|
||||
object SomeObject {
|
||||
var field1: Int = 10
|
||||
var field2: String? = null
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// FILE: annotation.kt
|
||||
package kotlin.native.concurrent
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ class Bar2 {
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadLocal
|
||||
<!INAPPLICABLE_THREAD_LOCAL!>@ThreadLocal<!>
|
||||
enum class Color(var rgb: Int) {
|
||||
RED(0xFF0000),
|
||||
GREEN(0x00FF00),
|
||||
|
||||
Reference in New Issue
Block a user