diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JvmStaticInPrivateCompanionChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JvmStaticInPrivateCompanionChecker.kt new file mode 100644 index 00000000000..e08d7f0ecae --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JvmStaticInPrivateCompanionChecker.kt @@ -0,0 +1,31 @@ +/* + * 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.jvm.checkers + +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.Visibilities +import org.jetbrains.kotlin.diagnostics.Errors.JVM_STATIC_IN_PRIVATE_COMPANION +import org.jetbrains.kotlin.psi.KtDeclaration +import org.jetbrains.kotlin.resolve.annotations.JVM_STATIC_ANNOTATION_FQ_NAME +import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker +import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext +import org.jetbrains.kotlin.resolve.source.KotlinSourceElement +import org.jetbrains.kotlin.utils.addToStdlib.safeAs + +class JvmStaticInPrivateCompanionChecker : DeclarationChecker { + override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) { + + descriptor.containingDeclaration.safeAs()?.takeIf { + it.isCompanionObject && Visibilities.isPrivate(it.visibility.delegate) + } ?: return + + val jvmStaticAnnotation = descriptor.annotations.findAnnotation(JVM_STATIC_ANNOTATION_FQ_NAME) ?: return + + val reportTarget = jvmStaticAnnotation.source.safeAs()?.psi ?: declaration + context.trace.report(JVM_STATIC_IN_PRIVATE_COMPANION.on(reportTarget)) + } +} \ No newline at end of file diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt index e0c4f15ec17..b480c13f288 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt @@ -95,6 +95,7 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase( ) { override fun configureModuleComponents(container: StorageComponentContainer) { container.useImpl() + container.useImpl() container.useImpl() container.useImpl() container.useImpl() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index d584082260a..d8f128127ca 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -285,6 +285,7 @@ public interface Errors { DiagnosticFactory1 EXPERIMENTAL_UNSIGNED_LITERALS_ERROR = DiagnosticFactory1.create(ERROR); DiagnosticFactory0 NON_PARENTHESIZED_ANNOTATIONS_ON_FUNCTIONAL_TYPES = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 JVM_STATIC_IN_PRIVATE_COMPANION = DiagnosticFactory0.create(WARNING); // Const DiagnosticFactory0 CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT = DiagnosticFactory0.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 6b981c78eb6..fb09ce2e121 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -169,6 +169,7 @@ public class DefaultErrorMessages { MAP.put(EXPERIMENTAL_UNSIGNED_LITERALS_ERROR, "{0}", STRING); MAP.put(NON_PARENTHESIZED_ANNOTATIONS_ON_FUNCTIONAL_TYPES, "Non-parenthesized annotations on function types without receiver aren't yet supported (see KT-31734 for details)"); + MAP.put(JVM_STATIC_IN_PRIVATE_COMPANION, "@JvmStatic is prohibited in private companion objects. This warning will become an error in the next major version"); MAP.put(REDUNDANT_MODIFIER, "Modifier ''{0}'' is redundant because ''{1}'' is present", TO_STRING, TO_STRING); MAP.put(REDUNDANT_OPEN_IN_INTERFACE, "Modifier 'open' is redundant for abstract interface members"); diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/jvmStatic/privateCompanionObject.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/jvmStatic/privateCompanionObject.fir.kt new file mode 100644 index 00000000000..4b6f9d5ae3b --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/jvmStatic/privateCompanionObject.fir.kt @@ -0,0 +1,29 @@ +// Issue: KT-25114 +// !DIAGNOSTICS: -UNUSED_PARAMETER + +class WithPrivateCompanion { + private companion object { + @JvmStatic + val staticVal1: Int = 42 + + val staticVal2: Int + @JvmStatic get() = 42 + + @get:JvmStatic + val staticVal3: Int = 42 + + @JvmStatic + var staticVar1: Int = 42 + + var staticVar2: Int + @JvmStatic get() = 42 + @JvmStatic set(value) {} + + @get: JvmStatic + @set: JvmStatic + var staticVar3: Int = 42 + + @JvmStatic + fun staticFunction() {} + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/jvmStatic/privateCompanionObject.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/jvmStatic/privateCompanionObject.kt index 7a809f942eb..1c718ce215b 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/jvmStatic/privateCompanionObject.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/jvmStatic/privateCompanionObject.kt @@ -1,30 +1,29 @@ // Issue: KT-25114 -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER class WithPrivateCompanion { private companion object { - @JvmStatic + @JvmStatic val staticVal1: Int = 42 val staticVal2: Int - @JvmStatic get() = 42 + @JvmStatic get() = 42 - @get:JvmStatic + @get:JvmStatic val staticVal3: Int = 42 - @JvmStatic + @JvmStatic var staticVar1: Int = 42 var staticVar2: Int - @JvmStatic get() = 42 - @JvmStatic set(value) {} + @JvmStatic get() = 42 + @JvmStatic set(value) {} - @get: JvmStatic - @set: JvmStatic + @get: JvmStatic + @set: JvmStatic var staticVar3: Int = 42 - @JvmStatic + @JvmStatic fun staticFunction() {} } } \ No newline at end of file