Synchronizing by values and primitives is forbidden
Signed-off-by: zhelenskiy <zhelenskiy2000@yandex.ru>
This commit is contained in:
committed by
Ilmir Usmanov
parent
877b53201b
commit
ea10b4a781
+6
@@ -16543,6 +16543,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/reservedMembersAndConstructsInsideInlineClass.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("synchronizedForbidden.kt")
|
||||
public void testSynchronizedForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/synchronizedForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("unsignedLiteralsWithoutArtifactOnClasspath.kt")
|
||||
public void testUnsignedLiteralsWithoutArtifactOnClasspath() throws Exception {
|
||||
|
||||
+6
@@ -16543,6 +16543,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/reservedMembersAndConstructsInsideInlineClass.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("synchronizedForbidden.kt")
|
||||
public void testSynchronizedForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/synchronizedForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("unsignedLiteralsWithoutArtifactOnClasspath.kt")
|
||||
public void testUnsignedLiteralsWithoutArtifactOnClasspath() throws Exception {
|
||||
|
||||
+6
@@ -16543,6 +16543,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/reservedMembersAndConstructsInsideInlineClass.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("synchronizedForbidden.kt")
|
||||
public void testSynchronizedForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/synchronizedForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("unsignedLiteralsWithoutArtifactOnClasspath.kt")
|
||||
public void testUnsignedLiteralsWithoutArtifactOnClasspath() throws Exception {
|
||||
|
||||
+2
@@ -199,6 +199,8 @@ class SynchronizedAnnotationChecker : DeclarationChecker {
|
||||
context.trace.report(ErrorsJvm.SYNCHRONIZED_ON_INLINE.on(annotationEntry))
|
||||
} else if (descriptor.isSuspend) {
|
||||
context.trace.report(ErrorsJvm.SYNCHRONIZED_ON_SUSPEND.on(annotationEntry))
|
||||
} else if (descriptor.containingDeclaration.let { it is ClassDescriptor && it.isValue }) {
|
||||
context.trace.report(ErrorsJvm.SYNCHRONIZED_ON_VALUE_CLASS.on(annotationEntry))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -62,6 +62,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
|
||||
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");
|
||||
MAP.put(SYNCHRONIZED_IN_INTERFACE, "'@Synchronized' annotation cannot be used on interface members");
|
||||
MAP.put(EXTERNAL_DECLARATION_CANNOT_BE_ABSTRACT, "External declaration can not be abstract");
|
||||
MAP.put(EXTERNAL_DECLARATION_CANNOT_HAVE_BODY, "External declaration can not have a body");
|
||||
|
||||
@@ -48,6 +48,7 @@ public interface ErrorsJvm {
|
||||
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);
|
||||
DiagnosticFactory0<PsiElement> SYNCHRONIZED_ON_SUSPEND = DiagnosticFactory0.create(WARNING);
|
||||
DiagnosticFactory0<KtAnnotationEntry> SYNCHRONIZED_IN_INTERFACE = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
|
||||
@@ -874,6 +874,7 @@ public interface Errors {
|
||||
DiagnosticFactory2<KtBinaryExpression, KotlinType, KotlinType> DEPRECATED_IDENTITY_EQUALS = DiagnosticFactory2.create(WARNING);
|
||||
DiagnosticFactory2<KtBinaryExpression, KotlinType, KotlinType> IMPLICIT_BOXING_IN_IDENTITY_EQUALS = DiagnosticFactory2.create(WARNING);
|
||||
DiagnosticFactory2<KtBinaryExpression, KotlinType, KotlinType> FORBIDDEN_IDENTITY_EQUALS = DiagnosticFactory2.create(ERROR);
|
||||
DiagnosticFactory1<PsiElement, KotlinType> FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES = DiagnosticFactory1.create(WARNING);
|
||||
|
||||
DiagnosticFactory2<PsiElement, FunctionDescriptor, String> DEPRECATED_BINARY_MOD = DiagnosticFactory2.create(WARNING);
|
||||
DiagnosticFactory2<PsiElement, FunctionDescriptor, String> FORBIDDEN_BINARY_MOD = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
+1
@@ -460,6 +460,7 @@ public class DefaultErrorMessages {
|
||||
MAP.put(DEPRECATED_IDENTITY_EQUALS, "Identity equality for arguments of types {0} and {1} is deprecated", RENDER_TYPE, RENDER_TYPE);
|
||||
MAP.put(IMPLICIT_BOXING_IN_IDENTITY_EQUALS, "Identity equality for arguments of types {0} and {1} can be unstable because of implicit boxing", RENDER_TYPE, RENDER_TYPE);
|
||||
MAP.put(FORBIDDEN_IDENTITY_EQUALS, "Identity equality for arguments of types {0} and {1} is forbidden", RENDER_TYPE, RENDER_TYPE);
|
||||
MAP.put(FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES, "Synchronizing by {0} is forbidden", RENDER_TYPE);
|
||||
|
||||
MAP.put(DEPRECATED_BINARY_MOD, "Deprecated convention for ''{0}''. Use ''{1}''", NAME, STRING);
|
||||
MAP.put(FORBIDDEN_BINARY_MOD, "Convention for ''{0}'' is forbidden. Use ''{1}''", NAME, STRING);
|
||||
|
||||
@@ -50,7 +50,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
|
||||
)
|
||||
|
||||
private val DEFAULT_CALL_CHECKERS = listOf(
|
||||
CapturingInClosureChecker(), InlineCheckerWrapper(), SafeCallChecker(), TrailingCommaCallChecker,
|
||||
CapturingInClosureChecker(), InlineCheckerWrapper(), SynchronizedByValueChecker(), SafeCallChecker(), TrailingCommaCallChecker,
|
||||
DeprecatedCallChecker, CallReturnsArrayOfNothingChecker(), InfixCallChecker(), OperatorCallChecker(),
|
||||
ConstructorHeaderCallChecker, ProtectedConstructorCallChecker, ApiVersionCallChecker,
|
||||
CoroutineSuspendCallChecker, BuilderFunctionsCallChecker, DslScopeViolationCallChecker, MissingDependencyClassChecker,
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.calls.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.isTopLevelInPackage
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.util.getType
|
||||
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
class SynchronizedByValueChecker : CallChecker {
|
||||
private fun KotlinType.isValueOrPrimitive(): Boolean = KotlinBuiltIns.isPrimitiveType(this)
|
||||
|| constructor.declarationDescriptor.let { it is ClassDescriptor && it.isValue }
|
||||
|| constructor.let { manyTypes -> manyTypes is IntegerLiteralTypeConstructor && manyTypes.possibleTypes.any { it.isValueOrPrimitive() } }
|
||||
|
||||
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
|
||||
if (resolvedCall.resultingDescriptor?.isTopLevelInPackage("synchronized", "kotlin") != true) return
|
||||
val argument = resolvedCall.valueArgumentsByIndex?.get(0)?.arguments?.firstOrNull() ?: return
|
||||
val type = argument.getArgumentExpression()?.getType(context.trace.bindingContext) ?: return
|
||||
if (type.isValueOrPrimitive()) {
|
||||
context.trace.report(Errors.FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES.on(reportOn, type))
|
||||
}
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
// TARGET_BACKEND: JVM
|
||||
// WITH_RUNTIME
|
||||
// SKIP_TXT
|
||||
// KT-49339
|
||||
|
||||
@JvmInline
|
||||
value class A(val a: Int) {
|
||||
@get:Synchronized
|
||||
val f0
|
||||
get() = Unit
|
||||
|
||||
@Synchronized
|
||||
fun f1() = Unit
|
||||
|
||||
@Synchronized
|
||||
fun String.f2() = Unit
|
||||
|
||||
@get:Synchronized
|
||||
val String.f3
|
||||
get() = Unit
|
||||
|
||||
@get:Synchronized
|
||||
val A.f4
|
||||
get() = Unit
|
||||
|
||||
@Synchronized
|
||||
fun A.f5() = Unit
|
||||
|
||||
val f6
|
||||
@Synchronized
|
||||
get() = Unit
|
||||
|
||||
val A.f7
|
||||
@Synchronized
|
||||
get() = Unit
|
||||
|
||||
val String.f8
|
||||
@Synchronized
|
||||
get() = Unit
|
||||
}
|
||||
|
||||
class Usual {
|
||||
|
||||
@get:Synchronized
|
||||
val A.f9
|
||||
get() = Unit
|
||||
|
||||
@Synchronized
|
||||
fun A.f10() = Unit
|
||||
|
||||
val A.f11
|
||||
@Synchronized
|
||||
get() = Unit
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun A.f12() = Unit
|
||||
|
||||
@get:Synchronized
|
||||
val A.f13
|
||||
get() = Unit
|
||||
|
||||
val A.f14
|
||||
@Synchronized
|
||||
get() = Unit
|
||||
|
||||
fun main() {
|
||||
val a = A(2)
|
||||
synchronized(a) {}
|
||||
synchronized(2) {}
|
||||
synchronized(0x2) {}
|
||||
synchronized(2U) {}
|
||||
synchronized(true) {}
|
||||
synchronized(2L) {}
|
||||
synchronized(2.to(1).first) {}
|
||||
synchronized(2.toByte()) {}
|
||||
synchronized(2UL) {}
|
||||
synchronized(2F) {}
|
||||
synchronized(2.0) {}
|
||||
synchronized('2') {}
|
||||
synchronized(block={}, lock='2')
|
||||
synchronized(block={}, lock=a)
|
||||
for (b in listOf(a)) {
|
||||
synchronized(b) {}
|
||||
synchronized(b.to(1).first) {}
|
||||
synchronized(block={}, lock=a)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
// TARGET_BACKEND: JVM
|
||||
// WITH_RUNTIME
|
||||
// SKIP_TXT
|
||||
// KT-49339
|
||||
|
||||
@JvmInline
|
||||
value class A(val a: Int) {
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@get:Synchronized<!>
|
||||
val f0
|
||||
get() = Unit
|
||||
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@Synchronized<!>
|
||||
fun f1() = Unit
|
||||
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@Synchronized<!>
|
||||
fun String.f2() = Unit
|
||||
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@get:Synchronized<!>
|
||||
val String.f3
|
||||
get() = Unit
|
||||
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@get:Synchronized<!>
|
||||
val A.f4
|
||||
get() = Unit
|
||||
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@Synchronized<!>
|
||||
fun A.f5() = Unit
|
||||
|
||||
val f6
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@Synchronized<!>
|
||||
get() = Unit
|
||||
|
||||
val A.f7
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@Synchronized<!>
|
||||
get() = Unit
|
||||
|
||||
val String.f8
|
||||
<!SYNCHRONIZED_ON_VALUE_CLASS!>@Synchronized<!>
|
||||
get() = Unit
|
||||
}
|
||||
|
||||
class Usual {
|
||||
|
||||
@get:Synchronized
|
||||
val A.f9
|
||||
get() = Unit
|
||||
|
||||
@Synchronized
|
||||
fun A.f10() = Unit
|
||||
|
||||
val A.f11
|
||||
@Synchronized
|
||||
get() = Unit
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun A.f12() = Unit
|
||||
|
||||
@get:Synchronized
|
||||
val A.f13
|
||||
get() = Unit
|
||||
|
||||
val A.f14
|
||||
@Synchronized
|
||||
get() = Unit
|
||||
|
||||
fun main() {
|
||||
val a = A(2)
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(a) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(0x2) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2U) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(true) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2L) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2.to(1).first) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2.toByte()) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2UL) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2F) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(2.0) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>('2') {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(block={}, lock='2')
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(block={}, lock=a)
|
||||
for (b in listOf(a)) {
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(b) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(b.to(1).first) {}
|
||||
<!FORBIDDEN_SYNCHRONIZED_BY_VALUE_CLASSES_OR_PRIMITIVES!>synchronized<!>(block={}, lock=a)
|
||||
}
|
||||
}
|
||||
Generated
+6
@@ -16549,6 +16549,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/reservedMembersAndConstructsInsideInlineClass.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("synchronizedForbidden.kt")
|
||||
public void testSynchronizedForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inlineClasses/synchronizedForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("unsignedLiteralsWithoutArtifactOnClasspath.kt")
|
||||
public void testUnsignedLiteralsWithoutArtifactOnClasspath() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user