Added Native-specific checker for properties of top level singletons (#3172)

This commit is contained in:
LepilkinaElena
2020-03-06 14:58:09 +03:00
committed by GitHub
parent eba45d4f89
commit 61f5523805
7 changed files with 403 additions and 2 deletions
@@ -25,6 +25,12 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
"@SharedImmutable is applicable only to val with backing field or to property with delegation"
)
put(ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL, "@SharedImmutable is applicable only to top level declarations")
put(
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")
}
}
@@ -24,7 +24,12 @@ object ErrorsNative {
val INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
@JvmField
val INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
@JvmField
val VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL = DiagnosticFactory0.create<KtElement>(Severity.WARNING)
@JvmField
val ENUM_THREAD_LOCAL_INAPPLICABLE = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
@JvmField
val VARIABLE_IN_ENUM = DiagnosticFactory0.create<KtElement>(Severity.WARNING)
init {
Errors.Initializer.initializeFactoryNames(ErrorsNative::class.java)
}
@@ -0,0 +1,50 @@
/*
* 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.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
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
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 ->
val hasBackingFieldWithDefaultSetter = descriptor.hasBackingField(context.trace.bindingContext) &&
descriptor.setter?.isDefault == true
val hasDelegate = if (declaration is KtProperty) declaration.delegate != null else false
if (descriptor.isVar && (DescriptorUtils.isEnumClass(parent) || DescriptorUtils.isEnumEntry(parent)) &&
hasBackingFieldWithDefaultSetter && !hasDelegate) {
context.trace.report(ErrorsNative.VARIABLE_IN_ENUM.on(declaration))
} else if (parent.kind.isSingleton) {
parent.annotations.findAnnotation(threadLocalFqName) ?: run {
if (descriptor.isVar && !hasDelegate && hasBackingFieldWithDefaultSetter) {
context.trace.report(ErrorsNative.VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL.on(declaration))
}
}
}
}
}
}
@@ -16,10 +16,14 @@ 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.NativeThrowsChecker
import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeTopLevelSingletonChecker
object NativePlatformConfigurator : PlatformConfiguratorBase(
additionalCallCheckers = listOf(SuperCallWithDefaultArgumentsChecker()),
additionalDeclarationCheckers = listOf(NativeThrowsChecker, NativeSharedImmutableChecker)
additionalDeclarationCheckers = listOf(
NativeThrowsChecker, NativeSharedImmutableChecker,
NativeTopLevelSingletonChecker
)
) {
override fun configureModuleComponents(container: StorageComponentContainer) {
container.useInstance(NativeInliningRule)