[K1] Make Volatile diagnostics applicable to kotlin.concurrent.Volatile

#KT-55628
This commit is contained in:
Kirill Rakhman
2023-04-03 13:15:59 +02:00
committed by Space Team
parent e93628d0e6
commit 91adb88eff
11 changed files with 166 additions and 25 deletions
@@ -367,6 +367,9 @@ public interface Errors {
DiagnosticFactory1<PsiElement, String> REDUNDANT_ANNOTATION_TARGET = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<KtAnnotationUseSiteTarget> INAPPLICABLE_FILE_TARGET = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> VOLATILE_ON_VALUE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> VOLATILE_ON_DELEGATE = DiagnosticFactory0.create(ERROR);
// Classes and interfaces
DiagnosticFactory0<KtTypeProjection> PROJECTION_IN_IMMEDIATE_ARGUMENT_TO_SUPERTYPE =
@@ -1208,6 +1208,9 @@ public class DefaultErrorMessages {
MAP.put(UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL, "To use contextual declarations, specify the `-Xcontext-receivers` compiler option");
MAP.put(SUBTYPING_BETWEEN_CONTEXT_RECEIVERS, "Subtyping relation between context receivers is prohibited");
MAP.put(VOLATILE_ON_VALUE, "'@Volatile' annotation cannot be used on immutable properties");
MAP.put(VOLATILE_ON_DELEGATE, "'@Volatile' annotation cannot be used on delegated properties");
MAP.setImmutable();
for (Field field : Errors.class.getFields()) {
@@ -57,6 +57,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
UnsupportedUntilRangeDeclarationChecker,
DataObjectContentChecker,
EnumEntriesRedeclarationChecker,
VolatileAnnotationChecker,
)
private val DEFAULT_CALL_CHECKERS = listOf(
@@ -0,0 +1,40 @@
/*
* 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.resolve.checkers
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FieldDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
object VolatileAnnotationChecker : DeclarationChecker {
private val JVM_VOLATILE_ANNOTATION_FQ_NAME = FqName("kotlin.jvm.Volatile")
private val CONCURRENT_VOLATILE_ANNOTATION_FQ_NAME = FqName("kotlin.concurrent.Volatile")
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is PropertyDescriptor) return
val fieldAnnotation = descriptor.backingField?.findVolatileAnnotation()
if (fieldAnnotation != null && !descriptor.isVar) {
val annotationEntry = DescriptorToSourceUtils.getSourceFromAnnotation(fieldAnnotation) ?: return
context.trace.report(Errors.VOLATILE_ON_VALUE.on(annotationEntry))
}
val delegateAnnotation = descriptor.delegateField?.findVolatileAnnotation()
if (delegateAnnotation != null) {
val annotationEntry = DescriptorToSourceUtils.getSourceFromAnnotation(delegateAnnotation) ?: return
context.trace.report(Errors.VOLATILE_ON_DELEGATE.on(annotationEntry))
}
}
private fun FieldDescriptor.findVolatileAnnotation(): AnnotationDescriptor? {
return annotations.firstOrNull { it.fqName == JVM_VOLATILE_ANNOTATION_FQ_NAME || it.fqName == CONCURRENT_VOLATILE_ANNOTATION_FQ_NAME }
}
}