Added Native-specific frontend checker for @SharedImmutable (#3091)

This commit is contained in:
LepilkinaElena
2020-03-04 16:45:37 +03:00
committed by GitHub
parent 8b559b20e2
commit b031706ea9
7 changed files with 321 additions and 1 deletions
@@ -20,6 +20,11 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
ErrorsNative.INCOMPATIBLE_THROWS_INHERITED, "Member inherits different @Throws filters from {0}",
Renderers.commaSeparated(Renderers.NAME)
)
put(
ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY,
"@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")
}
}
@@ -20,6 +20,10 @@ object ErrorsNative {
val INCOMPATIBLE_THROWS_OVERRIDE = DiagnosticFactory1.create<KtElement, DeclarationDescriptor>(Severity.ERROR)
@JvmField
val INCOMPATIBLE_THROWS_INHERITED = DiagnosticFactory1.create<KtDeclaration, Collection<DeclarationDescriptor>>(Severity.ERROR)
@JvmField
val INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
@JvmField
val INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
init {
Errors.Initializer.initializeFactoryNames(ErrorsNative::class.java)
@@ -0,0 +1,49 @@
/*
* 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.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
import org.jetbrains.kotlin.resolve.hasBackingField
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) {
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) {
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))
}
}
}
@@ -14,11 +14,12 @@ import org.jetbrains.kotlin.resolve.*
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.NativeThrowsChecker
object NativePlatformConfigurator : PlatformConfiguratorBase(
additionalCallCheckers = listOf(SuperCallWithDefaultArgumentsChecker()),
additionalDeclarationCheckers = listOf(NativeThrowsChecker)
additionalDeclarationCheckers = listOf(NativeThrowsChecker, NativeSharedImmutableChecker)
) {
override fun configureModuleComponents(container: StorageComponentContainer) {
container.useInstance(NativeInliningRule)