FIR: support separate ANONYMOUS_FUNCTION_WITH_NAME

This commit is contained in:
Mikhail Glukhikh
2021-04-16 11:18:21 +03:00
parent 6b95bcdbdb
commit eb831b9afc
8 changed files with 28 additions and 9 deletions
@@ -471,6 +471,7 @@ object DIAGNOSTICS_LIST : DiagnosticList() {
}
val FUNCTION_DECLARATION_WITH_NO_NAME by error<KtFunction>(PositioningStrategy.DECLARATION_SIGNATURE)
val ANONYMOUS_FUNCTION_WITH_NAME by error<FirSourceElement, KtFunction>()
// TODO: val ANONYMOUS_FUNCTION_WITH_NAME by error1<PsiElement, Name>(SourceElementPositioningStrategies.DECLARATION_NAME)
val ANONYMOUS_FUNCTION_PARAMETER_WITH_DEFAULT_VALUE by error<KtParameter>(PositioningStrategy.PARAMETER_DEFAULT_VALUE)
@@ -292,6 +292,7 @@ object FirErrors {
val PRIVATE_FUNCTION_WITH_NO_BODY by error1<KtFunction, FirMemberDeclaration>(SourceElementPositioningStrategies.VISIBILITY_MODIFIER)
val NON_MEMBER_FUNCTION_NO_BODY by error1<KtFunction, FirMemberDeclaration>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE)
val FUNCTION_DECLARATION_WITH_NO_NAME by error0<KtFunction>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE)
val ANONYMOUS_FUNCTION_WITH_NAME by error0<FirSourceElement, KtFunction>()
val ANONYMOUS_FUNCTION_PARAMETER_WITH_DEFAULT_VALUE by error0<KtParameter>(SourceElementPositioningStrategies.PARAMETER_DEFAULT_VALUE)
val USELESS_VARARG_ON_PARAMETER by warning0<KtParameter>()
val MULTIPLE_VARARG_PARAMETERS by error0<KtParameter>(SourceElementPositioningStrategies.PARAMETER_VARARG_MODIFIER)
@@ -174,10 +174,10 @@ private fun ConeSimpleDiagnostic.getFactory(source: FirSourceElement): FirDiagno
DiagnosticKind.RecursionInImplicitTypes -> FirErrors.RECURSION_IN_IMPLICIT_TYPES
DiagnosticKind.Java -> FirErrors.ERROR_FROM_JAVA_RESOLUTION
DiagnosticKind.SuperNotAllowed -> FirErrors.SUPER_IS_NOT_AN_EXPRESSION
DiagnosticKind.ExpressionExpected -> if (source.elementType == KtNodeTypes.BINARY_EXPRESSION) {
FirErrors.ASSIGNMENT_IN_EXPRESSION_CONTEXT
} else {
FirErrors.EXPRESSION_EXPECTED
DiagnosticKind.ExpressionExpected -> when (source.elementType) {
KtNodeTypes.BINARY_EXPRESSION -> FirErrors.ASSIGNMENT_IN_EXPRESSION_CONTEXT
KtNodeTypes.FUN -> FirErrors.ANONYMOUS_FUNCTION_WITH_NAME
else -> FirErrors.EXPRESSION_EXPECTED
}
DiagnosticKind.JumpOutsideLoop -> FirErrors.BREAK_OR_CONTINUE_OUTSIDE_A_LOOP
DiagnosticKind.NotLoopLabel -> FirErrors.NOT_A_LOOP_LABEL
@@ -4,7 +4,7 @@
fun foo(block: () -> (() -> Int)) {}
fun test() {
val x = <!EXPRESSION_EXPECTED!>fun named1(x: Int): Int { return 1 }<!>
val x = <!ANONYMOUS_FUNCTION_WITH_NAME!>fun named1(x: Int): Int { return 1 }<!>
x <!INAPPLICABLE_CANDIDATE!>checkType<!> { <!INAPPLICABLE_CANDIDATE!>_<!><Function1<Int, Int>>() }
foo { fun named2(): Int {return 1} }
@@ -45,12 +45,12 @@ fun test() {
x4 checkType { _<Function1<Int, Unit>>() }
{ y: Int -> fun named14(): Int {return 1} }
val b = (<!EXPRESSION_EXPECTED, UNRESOLVED_REFERENCE!>fun named15(): Boolean { return true }<!>)()
val b = (<!ANONYMOUS_FUNCTION_WITH_NAME, UNRESOLVED_REFERENCE!>fun named15(): Boolean { return true }<!>)()
baz(<!EXPRESSION_EXPECTED!>fun named16(){}<!>)
baz(<!ANONYMOUS_FUNCTION_WITH_NAME!>fun named16(){}<!>)
}
fun bar() = <!EXPRESSION_EXPECTED!>fun named() {}<!>
fun bar() = <!ANONYMOUS_FUNCTION_WITH_NAME!>fun named() {}<!>
fun <T> run(block: () -> T): T = null!!
fun run2(block: () -> Unit): Unit = null!!
@@ -5,5 +5,5 @@ fun foo() {
fun A.foo() {}
(fun A.foo() {})
run(<!EXPRESSION_EXPECTED!>fun foo() {}<!>)
run(<!ANONYMOUS_FUNCTION_WITH_NAME!>fun foo() {}<!>)
}
@@ -1320,6 +1320,12 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.ANONYMOUS_FUNCTION_WITH_NAME) { firDiagnostic ->
AnonymousFunctionWithNameImpl(
firDiagnostic as FirPsiDiagnostic<*>,
token,
)
}
add(FirErrors.ANONYMOUS_FUNCTION_PARAMETER_WITH_DEFAULT_VALUE) { firDiagnostic ->
AnonymousFunctionParameterWithDefaultValueImpl(
firDiagnostic as FirPsiDiagnostic<*>,
@@ -937,6 +937,10 @@ sealed class KtFirDiagnostic<PSI: PsiElement> : KtDiagnosticWithPsi<PSI> {
override val diagnosticClass get() = FunctionDeclarationWithNoName::class
}
abstract class AnonymousFunctionWithName : KtFirDiagnostic<KtFunction>() {
override val diagnosticClass get() = AnonymousFunctionWithName::class
}
abstract class AnonymousFunctionParameterWithDefaultValue : KtFirDiagnostic<KtParameter>() {
override val diagnosticClass get() = AnonymousFunctionParameterWithDefaultValue::class
}
@@ -1508,6 +1508,13 @@ internal class FunctionDeclarationWithNoNameImpl(
override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic)
}
internal class AnonymousFunctionWithNameImpl(
firDiagnostic: FirPsiDiagnostic<*>,
override val token: ValidityToken,
) : KtFirDiagnostic.AnonymousFunctionWithName(), KtAbstractFirDiagnostic<KtFunction> {
override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic)
}
internal class AnonymousFunctionParameterWithDefaultValueImpl(
firDiagnostic: FirPsiDiagnostic<*>,
override val token: ValidityToken,