[FIR] Support several annotation class diagnostics

This commit is contained in:
Likholetov Mikhail
2020-04-30 00:12:39 +03:00
committed by Mikhail Glukhikh
parent 1e8dff6a7c
commit aa706d322d
21 changed files with 206 additions and 48 deletions
@@ -0,0 +1,7 @@
annotation class A() {
<!ANNOTATION_CLASS_MEMBER!>constructor(s: Nothing?) {}<!>
<!ANNOTATION_CLASS_MEMBER!>init {}<!>
<!ANNOTATION_CLASS_MEMBER!>fun foo() {}<!>
<!ANNOTATION_CLASS_MEMBER!>val bar: Nothing?<!>
<!ANNOTATION_CLASS_MEMBER!>val baz get() = Unit<!>
}
@@ -0,0 +1,25 @@
FILE: annotationClassMember.kt
public final annotation class A : R|kotlin/Annotation| {
public constructor(): R|A| {
super<R|kotlin/Any|>()
}
public constructor(s: R|kotlin/Nothing?|): R|A| {
this<R|A|>()
}
init {
}
public final fun foo(): R|kotlin/Unit| {
}
public final val bar: R|kotlin/Nothing?|
public get(): R|kotlin/Nothing?|
public final val baz: R|kotlin/Unit|
public get(): R|kotlin/Unit| {
^ Q|kotlin/Unit|
}
}
@@ -0,0 +1,8 @@
fun foo() {
<!LOCAL_ANNOTATION_CLASS_ERROR!>annotation class Ann<!>
@Anno class Local {
// There should also be NESTED_CLASS_NOT_ALLOWED report here.
<!LOCAL_ANNOTATION_CLASS_ERROR!>annotation class Nested<!>
}
}
@@ -0,0 +1,24 @@
FILE: localAnnotationClass.kt
public final fun foo(): R|kotlin/Unit| {
local final annotation class Ann : R|kotlin/Annotation| {
public[local] constructor(): R|Ann| {
super<R|kotlin/Any|>()
}
}
@R|ERROR CLASS: Symbol not found, for `Anno`|() local final class Local : R|kotlin/Any| {
public[local] constructor(): R|Local| {
super<R|kotlin/Any|>()
}
local final annotation class Nested : R|kotlin/Annotation| {
public[local] constructor(): R|Local.Nested| {
super<R|kotlin/Any|>()
}
}
}
}
@@ -0,0 +1,4 @@
annotation class A(
<!VAR_ANNOTATION_PARAMETER!>var<!> a: Int,
<!MISSING_VAL_ON_ANNOTATION_PARAMETER!>b: String<!>
)
@@ -0,0 +1,11 @@
FILE: valOnAnnotationParameter.kt
public final annotation class A : R|kotlin/Annotation| {
public constructor(a: R|kotlin/Int|, b: R|kotlin/String|): R|A| {
super<R|kotlin/Any|>()
}
public final var a: R|kotlin/Int| = R|<local>/a|
public get(): R|kotlin/Int|
public set(value: R|kotlin/Int|): R|kotlin/Unit|
}
@@ -855,6 +855,21 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
public void testSuperclassNotAccessibleFromInterface() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/superclassNotAccessibleFromInterface.kt");
}
@TestMetadata("annotationClassMember.kt")
public void testAnnotationClassMember() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/annotationClassMember.kt");
}
@TestMetadata("localAnnotationClass.kt")
public void testLocalAnnotationClass() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/localAnnotationClass.kt");
}
@TestMetadata("valOnAnnotationParameter.kt")
public void testValOnAnnotationParameter() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/valOnAnnotationParameter.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/expresssions")
@@ -855,6 +855,21 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
public void testSuperclassNotAccessibleFromInterface() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/superclassNotAccessibleFromInterface.kt");
}
@TestMetadata("annotationClassMember.kt")
public void testAnnotationClassMember() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/annotationClassMember.kt");
}
@TestMetadata("localAnnotationClass.kt")
public void testLocalAnnotationClass() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/localAnnotationClass.kt");
}
@TestMetadata("valOnAnnotationParameter.kt")
public void testValOnAnnotationParameter() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/valOnAnnotationParameter.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/expresssions")
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
object DeclarationCheckers {
val DECLARATIONS: List<FirDeclarationChecker<FirDeclaration>> = listOf(
FirAnnotationClassDeclarationChecker,
FirModifierChecker
)
val MEMBER_DECLARATIONS: List<FirDeclarationChecker<FirMemberDeclaration>> = DECLARATIONS + listOf(
@@ -0,0 +1,69 @@
/*
* 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.fir.analysis.checkers.declaration
import com.intellij.lang.LighterASTNode
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.ClassKind.ANNOTATION_CLASS
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticFactory0
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.FirLightSourceElement
import org.jetbrains.kotlin.fir.FirPsiSourceElement
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.KtNodeTypes.FUN
import org.jetbrains.kotlin.lexer.KtTokens.VAL_KEYWORD
import org.jetbrains.kotlin.lexer.KtTokens.VAR_KEYWORD
import org.jetbrains.kotlin.psi.KtParameter
object FirAnnotationClassDeclarationChecker : FirDeclarationChecker<FirDeclaration>() {
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
if (declaration !is FirRegularClass) return
if (declaration.classKind != ANNOTATION_CLASS) return
if (declaration.isLocal) reporter.report(declaration.source, FirErrors.LOCAL_ANNOTATION_CLASS_ERROR)
for (it in declaration.declarations) {
when {
it is FirConstructor && it.isPrimary ->
for (parameter in it.valueParameters)
when (val parameterSourceElement = parameter.source) {
is FirPsiSourceElement<*> -> {
val parameterPsiElement = parameterSourceElement.psi as KtParameter
if (!parameterPsiElement.hasValOrVar())
reporter.report(parameterSourceElement, FirErrors.MISSING_VAL_ON_ANNOTATION_PARAMETER)
else if (parameterPsiElement.isMutable)
reporter.report(parameterSourceElement, FirErrors.VAR_ANNOTATION_PARAMETER)
}
is FirLightSourceElement -> {
val kidsRef = Ref<Array<LighterASTNode?>>()
parameterSourceElement.tree.getChildren(parameterSourceElement.element, kidsRef)
if (kidsRef.get().any { it?.tokenType == VAR_KEYWORD })
reporter.report(parameterSourceElement, FirErrors.VAR_ANNOTATION_PARAMETER)
else if (kidsRef.get().all { it?.tokenType != VAL_KEYWORD })
reporter.report(parameterSourceElement, FirErrors.MISSING_VAL_ON_ANNOTATION_PARAMETER)
}
}
it is FirRegularClass
|| it is FirProperty && it.initializer is FirQualifiedAccessExpression
|| it is FirSimpleFunction && it.source?.elementType != FUN -> {
// DO NOTHING
}
else -> reporter.report(it.source, FirErrors.ANNOTATION_CLASS_MEMBER)
}
}
}
private inline fun <reified T : FirSourceElement, P : PsiElement> DiagnosticReporter.report(
source: T?,
factory: FirDiagnosticFactory0<T, P>
) {
source?.let { report(factory.on(it)) }
}
}
@@ -143,6 +143,10 @@ abstract class AbstractDiagnosticCollector(
visitWithDeclaration(file)
}
override fun visitAnonymousInitializer(anonymousInitializer: FirAnonymousInitializer) {
visitWithDeclaration(anonymousInitializer)
}
private fun visitWithDeclaration(declaration: FirDeclaration) {
declaration.runComponents()
withDeclaration(declaration) {
@@ -61,6 +61,10 @@ class DeclarationCheckersDiagnosticComponent(collector: AbstractDiagnosticCollec
runCheck { DeclarationCheckers.DECLARATIONS.check(anonymousObject, data, it) }
}
override fun visitAnonymousInitializer(anonymousInitializer: FirAnonymousInitializer, data: CheckerContext) {
runCheck { DeclarationCheckers.DECLARATIONS.check(anonymousInitializer, data, it) }
}
private fun <D : FirDeclaration> List<FirDeclarationChecker<D>>.check(
declaration: D,
context: CheckerContext,
@@ -53,6 +53,11 @@ object FirErrors {
val NON_PRIVATE_CONSTRUCTOR_IN_ENUM by existing<FirSourceElement, PsiElement>(Errors.NON_PRIVATE_CONSTRUCTOR_IN_ENUM)
val NON_PRIVATE_CONSTRUCTOR_IN_SEALED by existing<FirSourceElement, PsiElement>(Errors.NON_PRIVATE_CONSTRUCTOR_IN_SEALED)
val ANNOTATION_CLASS_MEMBER by existing<FirSourceElement, PsiElement>(Errors.ANNOTATION_CLASS_MEMBER)
val LOCAL_ANNOTATION_CLASS_ERROR by existing<FirSourceElement, KtClassOrObject>(Errors.LOCAL_ANNOTATION_CLASS_ERROR)
val MISSING_VAL_ON_ANNOTATION_PARAMETER by existing<FirSourceElement, KtParameter>(Errors.MISSING_VAL_ON_ANNOTATION_PARAMETER)
val VAR_ANNOTATION_PARAMETER by existing<FirSourceElement, KtParameter>(Errors.VAR_ANNOTATION_PARAMETER)
// Exposed visibility group
val EXPOSED_TYPEALIAS_EXPANDED_TYPE by error3<FirSourceElement, PsiElement, FirEffectiveVisibility, DeclarationWithRelation, FirEffectiveVisibility>()
val EXPOSED_FUNCTION_RETURN_TYPE by error3<FirSourceElement, PsiElement, FirEffectiveVisibility, DeclarationWithRelation, FirEffectiveVisibility>()
@@ -1,31 +0,0 @@
// !LANGUAGE: +NestedClassesInAnnotations
annotation class Annotation2() {
public val s: String = ""
}
annotation class Annotation3() {
public fun foo() {}
}
annotation class Annotation4() {
class Foo() {}
}
annotation class Annotation5() {
companion object {}
}
annotation class Annotation6() {
init {}
}
annotation class Annotation1() {}
annotation class Annotation7(val name: String) {}
annotation class Annotation8(var name: String = "") {}
annotation class Annotation9(val name: String)
annotation class Annotation10
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NestedClassesInAnnotations
annotation class Annotation2() {
@@ -1,11 +1,11 @@
// !LANGUAGE: -NestedClassesInAnnotations
annotation class Annotation2() {
public val s: String = ""
<!ANNOTATION_CLASS_MEMBER!>public val s: String = ""<!>
}
annotation class Annotation3() {
public fun foo() {}
<!ANNOTATION_CLASS_MEMBER!>public fun foo() {}<!>
}
annotation class Annotation4() {
@@ -17,14 +17,14 @@ annotation class Annotation5() {
}
annotation class Annotation6() {
init {}
<!ANNOTATION_CLASS_MEMBER!>init {}<!>
}
annotation class Annotation1() {}
annotation class Annotation7(val name: String) {}
annotation class Annotation8(var name: String = "") {}
annotation class Annotation8(<!VAR_ANNOTATION_PARAMETER!>var<!> name: String = "") {}
annotation class Annotation9(val name: String)
@@ -1,5 +0,0 @@
annotation class Ann(
val a: Int,
var b: Int,
c: String
)
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
annotation class Ann(
val a: Int,
<!VAR_ANNOTATION_PARAMETER!>var<!> b: Int,
@@ -16,8 +16,8 @@ annotation class Foo {
}
constructor(s: Int) {}
init {}
fun function() {}
val property get() = Unit
<!ANNOTATION_CLASS_MEMBER!>constructor(s: Int) {}<!>
<!ANNOTATION_CLASS_MEMBER!>init {}<!>
<!ANNOTATION_CLASS_MEMBER!>fun function() {}<!>
<!ANNOTATION_CLASS_MEMBER!>val property get() = Unit<!>
}
@@ -1,9 +1,9 @@
// !LANGUAGE: -ProhibitLocalAnnotations
fun f() {
annotation class Anno
<!LOCAL_ANNOTATION_CLASS_ERROR!>annotation class Anno<!>
@Anno class Local {
annotation class Nested
<!LOCAL_ANNOTATION_CLASS_ERROR!>annotation class Nested<!>
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +ProhibitLocalAnnotations
fun f() {
annotation class Anno
<!LOCAL_ANNOTATION_CLASS_ERROR!>annotation class Anno<!>
@Anno class Local {
annotation class Nested
<!LOCAL_ANNOTATION_CLASS_ERROR!>annotation class Nested<!>
}
}