[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
@@ -168,24 +168,6 @@ class JvmNameAnnotationChecker : DeclarationChecker {
}
}
class VolatileAnnotationChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is PropertyDescriptor) return
val fieldAnnotation = descriptor.backingField?.annotations?.findAnnotation(VOLATILE_ANNOTATION_FQ_NAME)
if (fieldAnnotation != null && !descriptor.isVar) {
val annotationEntry = DescriptorToSourceUtils.getSourceFromAnnotation(fieldAnnotation) ?: return
context.trace.report(ErrorsJvm.VOLATILE_ON_VALUE.on(annotationEntry))
}
val delegateAnnotation = descriptor.delegateField?.annotations?.findAnnotation(VOLATILE_ANNOTATION_FQ_NAME)
if (delegateAnnotation != null) {
val annotationEntry = DescriptorToSourceUtils.getSourceFromAnnotation(delegateAnnotation) ?: return
context.trace.report(ErrorsJvm.VOLATILE_ON_DELEGATE.on(annotationEntry))
}
}
}
class SynchronizedAnnotationChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
val synchronizedAnnotation = descriptor.findSynchronizedAnnotation()
@@ -57,8 +57,6 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(INAPPLICABLE_JVM_NAME, "'@JvmName' annotation is not applicable to this declaration");
MAP.put(ILLEGAL_JVM_NAME, "Illegal JVM name");
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.put(SYNCHRONIZED_ON_ABSTRACT, "'@Synchronized' annotation cannot be used on abstract functions");
MAP.put(SYNCHRONIZED_ON_INLINE, "'@Synchronized' annotation has no effect on inline functions");
MAP.put(SYNCHRONIZED_ON_VALUE_CLASS, "'@Synchronized' annotation has no effect on value classes");
@@ -46,8 +46,6 @@ public interface ErrorsJvm {
DiagnosticFactory0<KtAnnotationEntry> STRICTFP_ON_CLASS = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtAnnotationEntry> VOLATILE_ON_VALUE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> VOLATILE_ON_DELEGATE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> SYNCHRONIZED_ON_ABSTRACT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtElement> SYNCHRONIZED_ON_INLINE = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtElement> SYNCHRONIZED_ON_VALUE_CLASS = DiagnosticFactory0.create(WARNING);
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.types.expressions.GenericArrayClassLiteralSupport
object JvmPlatformConfigurator : PlatformConfiguratorBase(
additionalDeclarationCheckers = listOf(
JvmNameAnnotationChecker(),
VolatileAnnotationChecker(),
SynchronizedAnnotationChecker(),
LocalFunInlineChecker(),
ExternalFunChecker(),
@@ -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 }
}
}
@@ -0,0 +1,63 @@
// !DIAGNOSTICS: -UNUSED_ANONYMOUS_PARAMETER
// !API_VERSION: 1.9
// !LANGUAGE: +MultiPlatformProjects
// MODULE: common
// FILE: common.kt
// TARGET_PLATFORM: Common
import kotlin.concurrent.Volatile
import kotlin.properties.Delegates
@OptIn(ExperimentalStdlibApi::class)
class ConcurrentVolatile {
@Volatile val x = 0
// ok
@Volatile var y = 1
@delegate:Volatile var z: String by Delegates.observable("?") { prop, old, new -> old.hashCode() }
@field:Volatile val w = 2
<!WRONG_ANNOTATION_TARGET!>@Volatile<!>
var noBacking: String
get() = ""
set(value) {}
}
// MODULE: jvm
// FILE: jvm.kt
// TARGET_PLATFORM: JVM
import kotlin.jvm.Volatile as JvmVolatile
import kotlin.concurrent.Volatile
import kotlin.properties.Delegates
@OptIn(ExperimentalStdlibApi::class)
class ConcurrentVolatileOnJvm {
<!VOLATILE_ON_VALUE!>@Volatile<!> val x = 0
// ok
@Volatile var y = 1
<!VOLATILE_ON_DELEGATE!>@delegate:Volatile<!> var z: String by Delegates.observable("?") { prop, old, new -> old.hashCode() }
<!VOLATILE_ON_VALUE!>@field:Volatile<!> val w = 2
<!WRONG_ANNOTATION_TARGET!>@Volatile<!>
var noBacking: String
get() = ""
set(value) {}
}
class JvmVolatile {
<!VOLATILE_ON_VALUE!>@JvmVolatile<!> val x = 0
// ok
@JvmVolatile var y = 1
<!VOLATILE_ON_DELEGATE!>@delegate:JvmVolatile<!> var z: String by Delegates.observable("?") { prop, old, new -> old.hashCode() }
<!VOLATILE_ON_VALUE!>@field:JvmVolatile<!> val w = 2
<!WRONG_ANNOTATION_TARGET!>@JvmVolatile<!>
var noBacking: String
get() = ""
set(value) {}
}
@@ -1,12 +1,38 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_ANONYMOUS_PARAMETER
// !API_VERSION: 1.9
import kotlin.jvm.Volatile as JvmVolatile
// !LANGUAGE: +MultiPlatformProjects
// MODULE: common
// FILE: common.kt
// TARGET_PLATFORM: Common
import kotlin.concurrent.Volatile
import kotlin.properties.Delegates
@OptIn(ExperimentalStdlibApi::class)
class ConcurrentVolatile {
<!VOLATILE_ON_VALUE, VOLATILE_ON_VALUE{JVM}!>@Volatile<!> val x = 0
// ok
@Volatile var y = 1
<!VOLATILE_ON_DELEGATE, VOLATILE_ON_DELEGATE{JVM}!>@delegate:Volatile<!> var z: String by Delegates.observable("?") { prop, old, new -> old.hashCode() }
<!VOLATILE_ON_VALUE, VOLATILE_ON_VALUE{JVM}!>@field:Volatile<!> val w = 2
<!WRONG_ANNOTATION_TARGET, WRONG_ANNOTATION_TARGET{JVM}!>@Volatile<!>
var noBacking: String
get() = ""
set(value) {}
}
// MODULE: jvm
// FILE: jvm.kt
// TARGET_PLATFORM: JVM
import kotlin.jvm.Volatile as JvmVolatile
import kotlin.concurrent.Volatile
import kotlin.properties.Delegates
@OptIn(ExperimentalStdlibApi::class)
class ConcurrentVolatileOnJvm {
<!VOLATILE_ON_VALUE!>@Volatile<!> val x = 0
// ok
@Volatile var y = 1
@@ -1,7 +1,35 @@
// -- Module: <common> --
package
@kotlin.OptIn(markerClass = {kotlin.ExperimentalStdlibApi::class}) public final class ConcurrentVolatile {
public constructor ConcurrentVolatile()
@field:kotlin.concurrent.Volatile public final var noBacking: kotlin.String
@field:kotlin.concurrent.Volatile public final val w: kotlin.Int = 2
@field:kotlin.concurrent.Volatile public final val x: kotlin.Int = 0
@field:kotlin.concurrent.Volatile public final var y: kotlin.Int
@delegate:kotlin.concurrent.Volatile public final var z: kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
// -- Module: <jvm> --
package
@kotlin.OptIn(markerClass = {kotlin.ExperimentalStdlibApi::class}) public final class ConcurrentVolatile {
public constructor ConcurrentVolatile()
@field:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final var noBacking: kotlin.String
@field:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final val w: kotlin.Int = 2
@field:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final val x: kotlin.Int = 0
@field:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final var y: kotlin.Int
@delegate:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final var z: kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.OptIn(markerClass = {kotlin.ExperimentalStdlibApi::class}) public final class ConcurrentVolatileOnJvm {
public constructor ConcurrentVolatileOnJvm()
@field:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final var noBacking: kotlin.String
@field:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final val w: kotlin.Int = 2
@field:kotlin.concurrent.Volatile /* = kotlin.jvm.Volatile */ public final val x: kotlin.Int = 0