FIR checker: improve diagnostics message for SEALED_INHERITOR_IN_DIFFERENT_PACKAGE

This commit is contained in:
Tianyu Geng
2021-08-24 17:13:58 +00:00
committed by TeamCityServer
parent 10d4dfef04
commit 1e8e38e5ac
7 changed files with 5 additions and 25 deletions
@@ -160,10 +160,7 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
val CLASS_IN_SUPERTYPE_FOR_ENUM by error<KtTypeReference>()
val SEALED_SUPERTYPE by error<KtTypeReference>()
val SEALED_SUPERTYPE_IN_LOCAL_CLASS by error<KtTypeReference>()
val SEALED_INHERITOR_IN_DIFFERENT_PACKAGE by error<KtTypeReference> {
parameter<FqName>("subclassPackage")
parameter<FqName>("basePackage")
}
val SEALED_INHERITOR_IN_DIFFERENT_PACKAGE by error<KtTypeReference>()
val SEALED_INHERITOR_IN_DIFFERENT_MODULE by error<KtTypeReference>()
val CLASS_INHERITS_JAVA_SEALED_CLASS by error<KtTypeReference>()
val SUPERTYPE_NOT_A_CLASS_OR_INTERFACE by error<KtElement> {
@@ -168,7 +168,7 @@ object FirErrors {
val CLASS_IN_SUPERTYPE_FOR_ENUM by error0<KtTypeReference>()
val SEALED_SUPERTYPE by error0<KtTypeReference>()
val SEALED_SUPERTYPE_IN_LOCAL_CLASS by error0<KtTypeReference>()
val SEALED_INHERITOR_IN_DIFFERENT_PACKAGE by error2<KtTypeReference, FqName, FqName>()
val SEALED_INHERITOR_IN_DIFFERENT_PACKAGE by error0<KtTypeReference>()
val SEALED_INHERITOR_IN_DIFFERENT_MODULE by error0<KtTypeReference>()
val CLASS_INHERITS_JAVA_SEALED_CLASS by error0<KtTypeReference>()
val SUPERTYPE_NOT_A_CLASS_OR_INTERFACE by error1<KtElement, String>()
@@ -48,13 +48,7 @@ object FirSealedSupertypeChecker : FirClassChecker() {
}
val superClassPackage = superClass.classId.packageFqName
if (superClassPackage != subclassPackage) {
reporter.reportOn(
superTypeRef.source,
FirErrors.SEALED_INHERITOR_IN_DIFFERENT_PACKAGE,
subclassPackage,
superClassPackage,
context
)
reporter.reportOn(superTypeRef.source, FirErrors.SEALED_INHERITOR_IN_DIFFERENT_PACKAGE, context)
}
if (superClass.moduleData != declaration.moduleData) {
// TODO: implement logic like in org.jetbrains.kotlin.resolve.checkers.SealedInheritorInSameModuleChecker for MPP support.
@@ -573,14 +573,9 @@ class FirDefaultErrorMessages {
map.put(CLASS_IN_SUPERTYPE_FOR_ENUM, "Enum class cannot inherit from classes")
map.put(SEALED_SUPERTYPE, "This type is sealed, so it can be inherited by only its own nested classes or objects")
map.put(SEALED_SUPERTYPE_IN_LOCAL_CLASS, "Local class cannot extend a sealed class")
map.put(
SEALED_INHERITOR_IN_DIFFERENT_PACKAGE,
"Inheritor of sealed class or interface declared in package {0} but it must be in package {1} where base class is declared",
TO_STRING,
TO_STRING
)
map.put(SEALED_INHERITOR_IN_DIFFERENT_PACKAGE, "A class can only inherit from a sealed class or interface declared in the same package")
map.put(SEALED_INHERITOR_IN_DIFFERENT_MODULE, "Inheritance of sealed classes or interfaces from different module is prohibited")
map.put(CLASS_INHERITS_JAVA_SEALED_CLASS, "Inheritance of java sealed classes is prohibited")
map.put(CLASS_INHERITS_JAVA_SEALED_CLASS, "Inheritance of Java sealed classes is prohibited")
map.put(SUPERTYPE_NOT_A_CLASS_OR_INTERFACE, "Supertype is not a class or interface", TO_STRING)
map.put(CYCLIC_INHERITANCE_HIERARCHY, "There's a cycle in the inheritance hierarchy for this type")
@@ -452,8 +452,6 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
}
add(FirErrors.SEALED_INHERITOR_IN_DIFFERENT_PACKAGE) { firDiagnostic ->
SealedInheritorInDifferentPackageImpl(
firDiagnostic.a,
firDiagnostic.b,
firDiagnostic as FirPsiDiagnostic,
token,
)
@@ -343,8 +343,6 @@ sealed class KtFirDiagnostic<PSI : PsiElement> : KtDiagnosticWithPsi<PSI> {
abstract class SealedInheritorInDifferentPackage : KtFirDiagnostic<KtTypeReference>() {
override val diagnosticClass get() = SealedInheritorInDifferentPackage::class
abstract val subclassPackage: FqName
abstract val basePackage: FqName
}
abstract class SealedInheritorInDifferentModule : KtFirDiagnostic<KtTypeReference>() {
@@ -523,8 +523,6 @@ internal class SealedSupertypeInLocalClassImpl(
}
internal class SealedInheritorInDifferentPackageImpl(
override val subclassPackage: FqName,
override val basePackage: FqName,
firDiagnostic: FirPsiDiagnostic,
override val token: ValidityToken,
) : KtFirDiagnostic.SealedInheritorInDifferentPackage(), KtAbstractFirDiagnostic<KtTypeReference> {