[FIR JS] Stop reporting NON_MEMBER_FUNCTION_NO_BODY for js natives

This commit is contained in:
Nikolay Lunyak
2023-01-04 16:56:39 +02:00
committed by Space Team
parent 85bcef537c
commit 2f9831ed99
16 changed files with 84 additions and 42 deletions
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2023 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.js.checkers
import org.jetbrains.kotlin.fir.analysis.checkers.FirPlatformDiagnosticSuppressor
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.checkers.hasAnnotationOrInsideAnnotatedClass
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.name.JsStandardClassIds
private fun FirDeclaration.isLexicallyInsideJsNative(context: CheckerContext): Boolean {
return JsStandardClassIds.Annotations.nativeAnnotations.any { hasAnnotationOrInsideAnnotatedClass(it, context.session) }
}
object FirJsPlatformDiagnosticSuppressor : FirPlatformDiagnosticSuppressor {
override fun shouldReportNoBody(declaration: FirCallableDeclaration, context: CheckerContext) =
!declaration.isLexicallyInsideJsNative(context)
}
@@ -37,6 +37,7 @@ object JsDeclarationCheckers : DeclarationCheckers() {
override val simpleFunctionCheckers: Set<FirSimpleFunctionChecker>
get() = setOf(
FirJsTopLevelFunctionsChecker,
FirJsNativeInvokeChecker,
FirJsNativeGetterChecker,
FirJsNativeSetterChecker,
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2023 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.js.checkers.declaration
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirTopLevelFunctionsChecker
import org.jetbrains.kotlin.fir.analysis.js.checkers.FirJsPlatformDiagnosticSuppressor
object FirJsTopLevelFunctionsChecker : FirTopLevelFunctionsChecker() {
override val suppressor = FirJsPlatformDiagnosticSuppressor
}
@@ -52,4 +52,9 @@ object JvmDeclarationCheckers : DeclarationCheckers() {
get() = setOf(
FirUpperBoundsChecker,
)
override val simpleFunctionCheckers: Set<FirSimpleFunctionChecker>
get() = setOf(
FirJvmTopLevelFunctionsChecker,
)
}
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2023 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.jvm.checkers.declaration
import org.jetbrains.kotlin.fir.analysis.checkers.FirPlatformDiagnosticSuppressor
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirTopLevelFunctionsChecker
object FirJvmTopLevelFunctionsChecker : FirTopLevelFunctionsChecker() {
override val suppressor = FirPlatformDiagnosticSuppressor.Default
}
@@ -54,7 +54,6 @@ object CommonDeclarationCheckers : DeclarationCheckers() {
FirFunctionTypeParametersSyntaxChecker,
FirOperatorModifierChecker,
FirTailrecFunctionChecker,
FirTopLevelFunctionsChecker,
FirMemberFunctionsChecker,
FirDataObjectContentChecker,
)
@@ -730,3 +730,6 @@ fun FirBasedSymbol<*>.hasAnnotationOrInsideAnnotatedClass(classId: ClassId, sess
val container = getContainingClassSymbol(session) ?: return false
return container.hasAnnotationOrInsideAnnotatedClass(classId, session)
}
fun FirDeclaration.hasAnnotationOrInsideAnnotatedClass(classId: ClassId, session: FirSession) =
symbol.hasAnnotationOrInsideAnnotatedClass(classId, session)
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2023 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
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
interface FirPlatformDiagnosticSuppressor {
fun shouldReportNoBody(declaration: FirCallableDeclaration, context: CheckerContext): Boolean
object Default : FirPlatformDiagnosticSuppressor {
override fun shouldReportNoBody(declaration: FirCallableDeclaration, context: CheckerContext): Boolean = true
}
}
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.analysis.checkers.declaration
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.analysis.checkers.FirPlatformDiagnosticSuppressor
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.checkers.hasModifier
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
@@ -18,7 +19,9 @@ import org.jetbrains.kotlin.fir.declarations.utils.isExternal
import org.jetbrains.kotlin.lexer.KtTokens
// See old FE's [DeclarationsChecker]
object FirTopLevelFunctionsChecker : FirSimpleFunctionChecker() {
abstract class FirTopLevelFunctionsChecker : FirSimpleFunctionChecker() {
abstract val suppressor: FirPlatformDiagnosticSuppressor
override fun check(declaration: FirSimpleFunction, context: CheckerContext, reporter: DiagnosticReporter) {
// Only report on top level callable declarations
if (context.containingDeclarations.size > 1) return
@@ -29,7 +32,7 @@ object FirTopLevelFunctionsChecker : FirSimpleFunctionChecker() {
// So, our source of truth should be the full modifier list retrieved from the source.
if (declaration.hasModifier(KtTokens.ABSTRACT_KEYWORD)) return
if (declaration.isExternal) return
if (!declaration.hasBody && !declaration.isExpect) {
if (!declaration.hasBody && !declaration.isExpect && suppressor.shouldReportNoBody(declaration, context)) {
reporter.reportOn(source, FirErrors.NON_MEMBER_FUNCTION_NO_BODY, declaration.symbol, context)
}
@@ -1,13 +0,0 @@
// !DIAGNOSTICS: -DEPRECATION
<!NON_MEMBER_FUNCTION_NO_BODY!>@nativeGetter
fun String.foo(n: Int): Int?<!>
@nativeGetter
fun String.bar(n: Int): Int? = definedExternally
external interface T {
@nativeGetter
fun foo(d: Double): String?
@nativeGetter
fun bar(d: Double): String?
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -DEPRECATION
@nativeGetter
fun String.foo(n: Int): Int?
@@ -1,13 +0,0 @@
// !DIAGNOSTICS: -DEPRECATION
<!NON_MEMBER_FUNCTION_NO_BODY!>@nativeInvoke
fun String.foo(): Int<!>
@nativeInvoke
fun String.bar(): Int = definedExternally
external object O {
@nativeInvoke
fun foo()
@nativeInvoke
fun bar() { definedExternally }
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -DEPRECATION
@nativeInvoke
fun String.foo(): Int
@@ -1,13 +0,0 @@
// !DIAGNOSTICS: -DEPRECATION
<!NON_MEMBER_FUNCTION_NO_BODY!>@nativeSetter
fun String.foo(n: Int, v: Any)<!>
@nativeSetter
fun String.bar(n: Int, v: Any) {}
external class C {
@nativeSetter
fun foo(d: Double, v: Any): Any
@nativeSetter
fun bar(d: Double, v: Any): Any = definedExternally
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -DEPRECATION
@nativeSetter
fun String.foo(n: Int, v: Any)
@@ -22,6 +22,8 @@ object JsStandardClassIds {
val JsNativeSetter = "nativeSetter".jsId()
val annotationsRequiringExternal = setOf(JsModule, JsQualifier)
val nativeAnnotations = setOf(JsNative, JsNativeInvoke, JsNativeGetter, JsNativeSetter)
}
object Callables {