Native specific frontend checker for @ThreadLocal (#3293)
This commit is contained in:
+5
-1
@@ -29,8 +29,12 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
|
||||
ErrorsNative.VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL,
|
||||
"Variable in singleton without @ThreadLocal can't be changed after initialization"
|
||||
)
|
||||
put(ErrorsNative.ENUM_THREAD_LOCAL_INAPPLICABLE, "@ThreadLocal isn't applicable to enum classes")
|
||||
put(ErrorsNative.VARIABLE_IN_ENUM, "Variable in enum class can't be changed after initialization")
|
||||
put(
|
||||
ErrorsNative.INAPPLICABLE_THREAD_LOCAL,
|
||||
"@ThreadLocal is applicable only to property with backing field, to property with delegation or to objects"
|
||||
)
|
||||
put(ErrorsNative.INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL, "@ThreadLocal is applicable only to top level declarations")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,9 @@ object ErrorsNative {
|
||||
@JvmField
|
||||
val VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL = DiagnosticFactory0.create<KtElement>(Severity.WARNING)
|
||||
@JvmField
|
||||
val ENUM_THREAD_LOCAL_INAPPLICABLE = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
|
||||
val INAPPLICABLE_THREAD_LOCAL = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
|
||||
@JvmField
|
||||
val INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
|
||||
@JvmField
|
||||
val VARIABLE_IN_ENUM = DiagnosticFactory0.create<KtElement>(Severity.WARNING)
|
||||
init {
|
||||
|
||||
+2
-17
@@ -9,12 +9,9 @@ package org.jetbrains.kotlin.resolve.konan.diagnostics
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory0
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
|
||||
@@ -24,26 +21,14 @@ object NativeSharedImmutableChecker : DeclarationChecker {
|
||||
private val sharedImmutableFqName = FqName("kotlin.native.concurrent.SharedImmutable")
|
||||
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
check(declaration, descriptor, context, ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY) {
|
||||
check(sharedImmutableFqName, declaration, descriptor, context, ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY) {
|
||||
val isVariable = descriptor is VariableDescriptor && descriptor.isVar
|
||||
val hasBackingField = descriptor is PropertyDescriptor && descriptor.hasBackingField(context.trace.bindingContext)
|
||||
val hasDelegate = declaration is KtProperty && declaration.delegate != null
|
||||
!isVariable && hasBackingField || hasDelegate
|
||||
}
|
||||
check(declaration, descriptor, context, ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL) {
|
||||
check(sharedImmutableFqName, declaration, descriptor, context, ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL) {
|
||||
DescriptorUtils.isTopLevelDeclaration(descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
fun check(
|
||||
declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext,
|
||||
error: DiagnosticFactory0<KtElement>, successCondition: (DeclarationDescriptor) -> Boolean
|
||||
) {
|
||||
if (successCondition(descriptor)) return
|
||||
val sharedImmutableAnnotation = descriptor.annotations.findAnnotation(sharedImmutableFqName)
|
||||
sharedImmutableAnnotation?.let {
|
||||
val reportLocation = DescriptorToSourceUtils.getSourceFromAnnotation(it) ?: declaration
|
||||
context.trace.report(error.on(reportLocation))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.resolve.konan.diagnostics
|
||||
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory0
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.hasBackingField
|
||||
|
||||
internal fun DeclarationChecker.check(
|
||||
annotationFqName: FqName, declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext,
|
||||
error: DiagnosticFactory0<KtElement>, successCondition: (DeclarationDescriptor) -> Boolean
|
||||
) {
|
||||
if (successCondition(descriptor)) return
|
||||
descriptor.annotations.findAnnotation(annotationFqName)?.let {
|
||||
val reportLocation = DescriptorToSourceUtils.getSourceFromAnnotation(it) ?: declaration
|
||||
context.trace.report(error.on(reportLocation))
|
||||
}
|
||||
}
|
||||
|
||||
object NativeThreadLocalChecker : DeclarationChecker {
|
||||
private val threadLocalFqName = FqName("kotlin.native.concurrent.ThreadLocal")
|
||||
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
check(threadLocalFqName, declaration, descriptor, context, ErrorsNative.INAPPLICABLE_THREAD_LOCAL) {
|
||||
val isVariable = descriptor is VariableDescriptor
|
||||
val hasBackingField = descriptor is PropertyDescriptor && descriptor.hasBackingField(context.trace.bindingContext)
|
||||
val hasDelegate = declaration is KtProperty && declaration.delegate != null
|
||||
(isVariable && (hasBackingField || hasDelegate)) ||
|
||||
(descriptor is ClassDescriptor && descriptor.kind == ClassKind.OBJECT)
|
||||
}
|
||||
check(threadLocalFqName, declaration, descriptor, context, ErrorsNative.INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL) {
|
||||
DescriptorUtils.isTopLevelDeclaration(descriptor) || descriptor is ClassDescriptor && descriptor.kind == ClassKind.OBJECT
|
||||
}
|
||||
}
|
||||
}
|
||||
-9
@@ -5,7 +5,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve.konan.diagnostics
|
||||
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
@@ -20,14 +19,6 @@ object NativeTopLevelSingletonChecker : DeclarationChecker {
|
||||
private val threadLocalFqName = FqName("kotlin.native.concurrent.ThreadLocal")
|
||||
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
// @ThreadLocal on enum has no effect.
|
||||
if (descriptor is ClassDescriptor && DescriptorUtils.isEnumClass(descriptor)) {
|
||||
descriptor.annotations.findAnnotation(threadLocalFqName)?.let {
|
||||
val reportLocation = DescriptorToSourceUtils.getSourceFromAnnotation(it) ?: declaration
|
||||
context.trace.report(ErrorsNative.ENUM_THREAD_LOCAL_INAPPLICABLE.on(reportLocation))
|
||||
}
|
||||
}
|
||||
|
||||
// Check variables inside singletons.
|
||||
if (descriptor !is PropertyDescriptor) return
|
||||
(descriptor.containingDeclaration as? ClassDescriptor)?.let { parent ->
|
||||
|
||||
+2
-1
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
|
||||
import org.jetbrains.kotlin.resolve.inline.ReasonableInlineRule
|
||||
import org.jetbrains.kotlin.resolve.jvm.checkers.SuperCallWithDefaultArgumentsChecker
|
||||
import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeSharedImmutableChecker
|
||||
import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeThreadLocalChecker
|
||||
import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeThrowsChecker
|
||||
import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeTopLevelSingletonChecker
|
||||
|
||||
@@ -22,7 +23,7 @@ object NativePlatformConfigurator : PlatformConfiguratorBase(
|
||||
additionalCallCheckers = listOf(SuperCallWithDefaultArgumentsChecker()),
|
||||
additionalDeclarationCheckers = listOf(
|
||||
NativeThrowsChecker, NativeSharedImmutableChecker,
|
||||
NativeTopLevelSingletonChecker
|
||||
NativeTopLevelSingletonChecker, NativeThreadLocalChecker
|
||||
)
|
||||
) {
|
||||
override fun configureModuleComponents(container: StorageComponentContainer) {
|
||||
|
||||
Reference in New Issue
Block a user