[FIR] Report DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE

^KT-59418 Fixed
This commit is contained in:
Nikolay Lunyak
2023-08-21 11:46:21 +03:00
committed by Space Team
parent 7d48a7934f
commit ed6a0af0a6
10 changed files with 60 additions and 1 deletions
@@ -408,6 +408,12 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE) { firDiagnostic ->
DuplicateParameterNameInFunctionTypeImpl(
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS) { firDiagnostic ->
CreatingAnInstanceOfAbstractClassImpl(
firDiagnostic as KtPsiDiagnostic,
@@ -331,6 +331,10 @@ sealed interface KtFirDiagnostic<PSI : PsiElement> : KtDiagnosticWithPsi<PSI> {
val reference: String
}
interface DuplicateParameterNameInFunctionType : KtFirDiagnostic<KtTypeReference> {
override val diagnosticClass get() = DuplicateParameterNameInFunctionType::class
}
interface CreatingAnInstanceOfAbstractClass : KtFirDiagnostic<KtExpression> {
override val diagnosticClass get() = CreatingAnInstanceOfAbstractClass::class
}
@@ -381,6 +381,11 @@ internal class UnresolvedImportImpl(
token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<PsiElement>(firDiagnostic, token), KtFirDiagnostic.UnresolvedImport
internal class DuplicateParameterNameInFunctionTypeImpl(
firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<KtTypeReference>(firDiagnostic, token), KtFirDiagnostic.DuplicateParameterNameInFunctionType
internal class CreatingAnInstanceOfAbstractClassImpl(
firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken,
@@ -174,6 +174,8 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
val UNRESOLVED_IMPORT by error<PsiElement>(PositioningStrategy.IMPORT_LAST_NAME) {
parameter<String>("reference")
}
val DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE by error<KtTypeReference>()
}
val CALL_RESOLUTION by object : DiagnosticGroup("Call resolution") {
@@ -172,6 +172,7 @@ object FirErrors {
val API_NOT_AVAILABLE by error2<PsiElement, ApiVersion, ApiVersion>(SourceElementPositioningStrategies.SELECTOR_BY_QUALIFIED)
val UNRESOLVED_REFERENCE_WRONG_RECEIVER by error1<PsiElement, Collection<FirBasedSymbol<*>>>()
val UNRESOLVED_IMPORT by error1<PsiElement, String>(SourceElementPositioningStrategies.IMPORT_LAST_NAME)
val DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE by error0<KtTypeReference>()
// Call resolution
val CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS by error0<KtExpression>()
@@ -52,6 +52,7 @@ val FIR_NON_SUPPRESSIBLE_ERROR_NAMES: Set<String> = setOf(
"API_NOT_AVAILABLE",
"UNRESOLVED_REFERENCE_WRONG_RECEIVER",
"UNRESOLVED_IMPORT",
"DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE",
"CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS",
"NO_CONSTRUCTOR",
"FUNCTION_CALL_EXPECTED",
@@ -17,6 +17,7 @@ object CommonTypeCheckers : TypeCheckers() {
FirUnsupportedDefaultValueInFunctionTypeParameterChecker,
FirUnsupportedModifiersInFunctionTypeParameterChecker,
FirStarProjectionModifierChecker,
FirDuplicateParameterNameInFunctionTypeChecker,
FirOptionalExpectationTypeChecker
)
}
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2021 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.type
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.declarations.getAnnotationByClassId
import org.jetbrains.kotlin.fir.expressions.FirConstExpression
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.customAnnotations
import org.jetbrains.kotlin.fir.types.type
object FirDuplicateParameterNameInFunctionTypeChecker : FirTypeRefChecker() {
override fun check(typeRef: FirTypeRef, context: CheckerContext, reporter: DiagnosticReporter) {
if (typeRef !is FirResolvedTypeRef) return
val nameToArgumentProjection = typeRef.type.typeArguments.dropLast(1).groupBy {
val type = it.type ?: return@groupBy null
val annotation = type.customAnnotations.getAnnotationByClassId(StandardNames.FqNames.parameterNameClassId, context.session)
val nameEntry = annotation?.argumentMapping?.mapping?.get(StandardNames.NAME)
(nameEntry as? FirConstExpression<*>)?.value as? String
}
for ((name, projections) in nameToArgumentProjection) {
if (name != null && projections.size >= 2) {
reporter.reportOn(typeRef.source, FirErrors.DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE, context)
}
}
}
}
@@ -192,6 +192,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DESERIALIZATION_E
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DIVISION_BY_ZERO
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DSL_SCOPE_VIOLATION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DUPLICATE_LABEL_IN_WHEN
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DYNAMIC_NOT_ALLOWED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DYNAMIC_RECEIVER_NOT_ALLOWED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DYNAMIC_SUPERTYPE
@@ -769,6 +770,7 @@ object FirErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
map.put(MISSING_CONSTRUCTOR_KEYWORD, "Use the 'constructor' keyword after the modifiers of the primary constructor.")
map.put(UNRESOLVED_REFERENCE, "Unresolved reference ''{0}''.", NULLABLE_STRING)
map.put(UNRESOLVED_IMPORT, "Unresolved reference ''{0}''.", NULLABLE_STRING) // &
map.put(DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE, "Duplicate parameter name in a function type.")
map.put(UNRESOLVED_LABEL, "Unresolved label.")
map.put(DESERIALIZATION_ERROR, "Deserialization error.")
map.put(ERROR_FROM_JAVA_RESOLUTION, "Java resolution error.")
@@ -2,6 +2,6 @@ fun test0(f: (String, String) -> Unit) {
f("", "")
}
fun test1(f: (a: Int, a: Int) -> Unit) {
fun test1(f: <!DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE!>(a: Int, a: Int) -> Unit<!>) {
f(1, 1)
}