diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 91e2e995a5b..64a818f9211 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -633,6 +633,7 @@ public interface Errors { DiagnosticFactory0 OPTIONAL_EXPECTATION_NOT_ON_EXPECTED = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 OPTIONAL_DECLARATION_OUTSIDE_OF_ANNOTATION_ENTRY = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE = 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 f76d09e2558..d5b245031db 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -289,6 +289,7 @@ public class DefaultErrorMessages { MAP.put(OPTIONAL_EXPECTATION_NOT_ON_EXPECTED, "'@OptionalExpectation' can only be used on an expected annotation class"); MAP.put(OPTIONAL_DECLARATION_OUTSIDE_OF_ANNOTATION_ENTRY, "Declaration annotated with '@OptionalExpectation' can only be used inside an annotation entry"); + MAP.put(OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE, "Declaration annotated with '@OptionalExpectation' can only be used in common module sources"); MAP.put(PROJECTION_ON_NON_CLASS_TYPE_ARGUMENT, "Projections are not allowed on type arguments of functions and properties"); MAP.put(SUPERTYPE_NOT_INITIALIZED, "This type has a constructor, and thus must be initialized here"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptionalExpectationUsageChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptionalExpectationUsageChecker.kt index a66d05ef054..496ac9e6920 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptionalExpectationUsageChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptionalExpectationUsageChecker.kt @@ -8,6 +8,8 @@ package org.jetbrains.kotlin.resolve.checkers import com.intellij.psi.PsiElement import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource class OptionalExpectationUsageChecker : ClassifierUsageChecker { override fun check(targetDescriptor: ClassifierDescriptor, element: PsiElement, context: ClassifierUsageCheckerContext) { @@ -16,5 +18,10 @@ class OptionalExpectationUsageChecker : ClassifierUsageChecker { if (!element.isUsageAsAnnotationOrImport()) { context.trace.report(Errors.OPTIONAL_DECLARATION_OUTSIDE_OF_ANNOTATION_ENTRY.on(element)) } + + val ktFile = element.containingFile as KtFile + if (ktFile.isCommonSource != true) { + context.trace.report(Errors.OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE.on(element)) + } } } diff --git a/compiler/testData/codegen/box/multiplatform/optionalExpectation.kt b/compiler/testData/codegen/box/multiplatform/optionalExpectation.kt index 987c870650b..847b399bbc2 100644 --- a/compiler/testData/codegen/box/multiplatform/optionalExpectation.kt +++ b/compiler/testData/codegen/box/multiplatform/optionalExpectation.kt @@ -23,6 +23,8 @@ actual annotation class A(actual val x: Int) // MODULE: main(library) // FILE: main.kt +@file:Suppress("OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE") // TODO: support common sources in the test infrastructure + package usage import a.A diff --git a/compiler/testData/codegen/box/multiplatform/optionalExpectationJvm.kt b/compiler/testData/codegen/box/multiplatform/optionalExpectationJvm.kt index f707f4a305d..2af96ee5bfe 100644 --- a/compiler/testData/codegen/box/multiplatform/optionalExpectationJvm.kt +++ b/compiler/testData/codegen/box/multiplatform/optionalExpectationJvm.kt @@ -9,6 +9,8 @@ expect annotation class Anno(val s: String) // FILE: jvm.kt +@file:Suppress("OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE") // TODO: support common sources in the test infrastructure + import java.lang.reflect.AnnotatedElement @Anno("Foo") diff --git a/compiler/testData/codegen/bytecodeListing/multiplatform/optionalExpectation.kt b/compiler/testData/codegen/bytecodeListing/multiplatform/optionalExpectation.kt index 0466f86963e..29914cf6efd 100644 --- a/compiler/testData/codegen/bytecodeListing/multiplatform/optionalExpectation.kt +++ b/compiler/testData/codegen/bytecodeListing/multiplatform/optionalExpectation.kt @@ -3,6 +3,8 @@ // TARGET_BACKEND: JVM // WITH_RUNTIME +@file:Suppress("OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE") // TODO: support common sources in the test infrastructure + @OptionalExpectation expect annotation class Anno(val s: String) diff --git a/compiler/testData/compileKotlinAgainstKotlin/optionalAnnotation.kt b/compiler/testData/compileKotlinAgainstKotlin/optionalAnnotation.kt index 208bf99ef02..96882b0a575 100644 --- a/compiler/testData/compileKotlinAgainstKotlin/optionalAnnotation.kt +++ b/compiler/testData/compileKotlinAgainstKotlin/optionalAnnotation.kt @@ -14,6 +14,9 @@ expect annotation class B(val s: String) actual annotation class A(actual val x: Int) // FILE: B.kt + +@file:Suppress("OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE") // TODO: support common sources in the test infrastructure + import a.A import a.B import java.lang.reflect.Modifier diff --git a/compiler/testData/multiplatform/optionalExpectationIncorrectUse/output.txt b/compiler/testData/multiplatform/optionalExpectationIncorrectUse/output.txt index 4ea3cd7dd9d..ae14699df26 100644 --- a/compiler/testData/multiplatform/optionalExpectationIncorrectUse/output.txt +++ b/compiler/testData/multiplatform/optionalExpectationIncorrectUse/output.txt @@ -39,11 +39,20 @@ compiler/testData/multiplatform/optionalExpectationIncorrectUse/common.kt:19:20: @InOtherAnnotation(A()) ^ compiler/testData/multiplatform/optionalExpectationIncorrectUse/jvm.kt:1:24: error: declaration annotated with '@OptionalExpectation' can only be used inside an annotation entry +fun useInReturnType(): A? = null + ^ +compiler/testData/multiplatform/optionalExpectationIncorrectUse/jvm.kt:1:24: error: declaration annotated with '@OptionalExpectation' can only be used in common module sources fun useInReturnType(): A? = null ^ compiler/testData/multiplatform/optionalExpectationIncorrectUse/jvm.kt:3:43: error: declaration annotated with '@OptionalExpectation' can only be used inside an annotation entry +annotation class AnotherAnnotation(val a: A) + ^ +compiler/testData/multiplatform/optionalExpectationIncorrectUse/jvm.kt:3:43: error: declaration annotated with '@OptionalExpectation' can only be used in common module sources annotation class AnotherAnnotation(val a: A) ^ compiler/testData/multiplatform/optionalExpectationIncorrectUse/jvm.kt:5:20: error: declaration annotated with '@OptionalExpectation' can only be used inside an annotation entry @AnotherAnnotation(A()) ^ +compiler/testData/multiplatform/optionalExpectationIncorrectUse/jvm.kt:5:20: error: declaration annotated with '@OptionalExpectation' can only be used in common module sources +@AnotherAnnotation(A()) + ^