Volatile is no more allowed on values #KT-7670 Fixed

This commit is contained in:
Mikhail Glukhikh
2015-10-13 11:33:25 +03:00
parent 8473be357f
commit ccf0c363fa
8 changed files with 51 additions and 1 deletions
@@ -128,6 +128,20 @@ public class JvmNameAnnotationChecker : DeclarationChecker {
}
}
public class VolatileAnnotationChecker : DeclarationChecker {
override fun check(declaration: JetDeclaration,
descriptor: DeclarationDescriptor,
diagnosticHolder: DiagnosticSink,
bindingContext: BindingContext
) {
val volatileAnnotation = DescriptorUtils.getVolatileAnnotation(descriptor)
if (volatileAnnotation != null && descriptor is PropertyDescriptor && !descriptor.isVar) {
val annotationEntry = DescriptorToSourceUtils.getSourceFromAnnotation(volatileAnnotation) ?: return
diagnosticHolder.report(ErrorsJvm.VOLATILE_ON_VALUE.on(annotationEntry))
}
}
}
public class OverloadsAnnotationChecker: DeclarationChecker {
override fun check(
@@ -53,6 +53,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(ErrorsJvm.OVERLOADS_PRIVATE, "''JvmOverloads'' annotation has no effect on private and local declarations");
MAP.put(ErrorsJvm.INAPPLICABLE_JVM_NAME, "''JvmName'' annotation is not applicable to this declaration");
MAP.put(ErrorsJvm.ILLEGAL_JVM_NAME, "Illegal JVM name");
MAP.put(ErrorsJvm.VOLATILE_ON_VALUE, "''Volatile'' annotation cannot be used on immutable properties");
MAP.put(ErrorsJvm.EXTERNAL_DECLARATION_CANNOT_BE_ABSTRACT, "External declaration can not be abstract");
MAP.put(ErrorsJvm.EXTERNAL_DECLARATION_CANNOT_HAVE_BODY, "External declaration can not have a body");
MAP.put(ErrorsJvm.EXTERNAL_DECLARATION_IN_INTERFACE, "Members of interfaces can not be external");
@@ -50,6 +50,8 @@ public interface ErrorsJvm {
DiagnosticFactory1<JetAnnotationEntry, String> INAPPLICABLE_JVM_FIELD = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<JetAnnotationEntry> VOLATILE_ON_VALUE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetDeclaration> OVERLOADS_WITHOUT_DEFAULT_ARGUMENTS = DiagnosticFactory0.create(WARNING, DECLARATION_SIGNATURE);
DiagnosticFactory0<JetDeclaration> OVERLOADS_ABSTRACT = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<JetDeclaration> OVERLOADS_PRIVATE = DiagnosticFactory0.create(WARNING, DECLARATION_SIGNATURE);
@@ -67,6 +69,8 @@ public interface ErrorsJvm {
DiagnosticFactory0<JetElement> INTERFACE_CANT_CALL_DEFAULT_METHOD_VIA_SUPER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetElement> INAPPLICABLE_PUBLIC_FIELD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetElement> NO_REFLECTION_IN_CLASS_PATH = DiagnosticFactory0.create(WARNING);
DiagnosticFactory2<JetElement, JetType, JetType> JAVA_CLASS_ON_COMPANION = DiagnosticFactory2.create(WARNING);
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.jvm.RuntimeAssertionsTypeChecker
import org.jetbrains.kotlin.load.kotlin.JavaAnnotationCallChecker
import org.jetbrains.kotlin.load.kotlin.JavaAnnotationMethodCallChecker
import org.jetbrains.kotlin.load.kotlin.nativeDeclarations.NativeFunChecker
import org.jetbrains.kotlin.resolve.PlatformConfigurator
import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.jvm.checkers.*
import org.jetbrains.kotlin.types.DynamicTypesSettings
@@ -31,6 +31,7 @@ public object JvmPlatformConfigurator : PlatformConfigurator(
additionalDeclarationCheckers = listOf(
PlatformStaticAnnotationChecker(),
JvmNameAnnotationChecker(),
VolatileAnnotationChecker(),
LocalFunInlineChecker(),
ReifiedTypeParameterAnnotationChecker(),
NativeFunChecker(),
@@ -67,3 +68,4 @@ public object JvmPlatformConfigurator : PlatformConfigurator(
container.useImpl<ReflectionAPICallChecker>()
}
}
@@ -0,0 +1,7 @@
import kotlin.jvm.Volatile
class My {
<!VOLATILE_ON_VALUE!>@Volatile<!> val x = 0
// ok
@Volatile var y = 1
}
@@ -0,0 +1,10 @@
package
public final class My {
public constructor My()
@kotlin.jvm.Volatile() public final val x: kotlin.Int = 0
@kotlin.jvm.Volatile() public final var y: kotlin.Int
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
}
@@ -109,6 +109,12 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic
doTest(fileName);
}
@TestMetadata("Volatile.kt")
public void testVolatile() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/Volatile.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -54,6 +54,7 @@ public class DescriptorUtils {
public static final Name ENUM_VALUE_OF = Name.identifier("valueOf");
public static final FqName JVM_NAME = new FqName("kotlin.jvm.JvmName");
public static final FqName PLATFORM_NAME = new FqName("kotlin.platform.platformName");
public static final FqName VOLATILE = new FqName("kotlin.jvm.Volatile");
private DescriptorUtils() {
}
@@ -605,6 +606,11 @@ public class DescriptorUtils {
return getJvmNameAnnotation(annotated.getAnnotations());
}
@Nullable
public static AnnotationDescriptor getVolatileAnnotation(@NotNull Annotated annotated) {
return annotated.getAnnotations().findAnnotation(VOLATILE);
}
@NotNull
public static SourceFile getContainingSourceFile(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof PropertySetterDescriptor) {