FIR: don't report CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT on variables

This commit is contained in:
Mikhail Glukhikh
2021-03-04 11:56:19 +03:00
parent dd3bc1a964
commit d7a6fc80b0
5 changed files with 7 additions and 9 deletions
@@ -38,5 +38,5 @@ object F {
}
fun foo() {
<!CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT!>const<!> val a = "2"
const val a = "2"
}
@@ -12,13 +12,11 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.fir.declarations.isConst
object FirConstPropertyChecker : FirPropertyChecker() {
override fun check(declaration: FirProperty, context: CheckerContext, reporter: DiagnosticReporter) {
val modifierList = with(FirModifierList) { declaration.source.getModifierList() }
if (modifierList?.modifiers?.any { it.token == KtTokens.CONST_KEYWORD } != true)
return
if (!declaration.isConst) return
val classKind = (context.containingDeclarations.lastOrNull() as? FirRegularClass)?.classKind
if (classKind != ClassKind.OBJECT && context.containingDeclarations.size > 1)
reporter.reportOn(declaration.source, FirErrors.CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT, context)
@@ -8,7 +8,7 @@ fun test() {
val <T> a0 = 0
val <T : __UNRESOLVED__> a1 = ""
val <T : String> a2 = 0
<!CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT!>const<!> val <T> a3 = 0
const val <T> a3 = 0
<!INAPPLICABLE_LATEINIT_MODIFIER!>lateinit<!> val <T> a4 = 0
val <T> a5 by Delegate<Int>()
val <T> a6 by <!INAPPLICABLE_CANDIDATE!>Delegate<!><<!UNRESOLVED_REFERENCE!>T<!>>()
@@ -8,7 +8,7 @@ fun test() {
val <T> a0 = 0
val <T : __UNRESOLVED__> a1 = ""
val <T : String> a2 = 0
<!CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT!>const<!> val <T> a3 = 0
const val <T> a3 = 0
<!INAPPLICABLE_LATEINIT_MODIFIER!>lateinit<!> val <T> a4 = 0
val <T> a5 by Delegate<Int>()
val <T> a6 by <!INAPPLICABLE_CANDIDATE!>Delegate<!><<!UNRESOLVED_REFERENCE!>T<!>>()
@@ -12,7 +12,7 @@ object A {
const val inObject: Int = 4
}
class B(<!CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT!>const<!> val constructor: Int = 5)
class B(const val constructor: Int = 5)
abstract class C {
<!INCOMPATIBLE_MODIFIERS!>open<!> <!CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT, INCOMPATIBLE_MODIFIERS!>const<!> val x: Int = 6
@@ -48,7 +48,7 @@ const val withExplicitDefaultGetter: Int = 1
get
fun foo(): Int {
<!CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT!>const<!> val local: Int = 14
const val local: Int = 14
return 15
}