Minor fixes around FirKClassWithIncorrectTypeArgumentChecker

This commit is contained in:
Mikhail Glukhikh
2021-03-11 12:41:31 +03:00
parent 4c08d10cce
commit 0b9598d880
2 changed files with 4 additions and 6 deletions
@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.fir.resolve.inference.isKClassType
import org.jetbrains.kotlin.fir.scopes.impl.toConeType
import org.jetbrains.kotlin.fir.types.*
// FE1.0 [KClassWithIncorrectTypeArgumentChecker]
// See FE1.0 [KClassWithIncorrectTypeArgumentChecker]
object FirKClassWithIncorrectTypeArgumentChecker : FirFileChecker() {
override fun check(declaration: FirFile, context: CheckerContext, reporter: DiagnosticReporter) {
for (topLevelDeclaration in declaration.declarations) {
@@ -36,14 +36,11 @@ object FirKClassWithIncorrectTypeArgumentChecker : FirFileChecker() {
context: CheckerContext,
reporter: DiagnosticReporter
) {
// prevent duplicate reporting
if (declaration is FirPropertyAccessor) return
val source = declaration.source ?: return
if (source.kind is FirFakeSourceElementKind) return
val returnType = declaration.returnTypeRef.coneType
if (!returnType.isKClassWithBadArgument(context.session)) return
if (!returnType.isKClassTypeWithErrorOrNullableArgument(context.session)) return
val typeArgument = (returnType.typeArguments[0] as ConeKotlinTypeProjection).type
typeArgument.typeParameterFromError?.let {
@@ -51,7 +48,7 @@ object FirKClassWithIncorrectTypeArgumentChecker : FirFileChecker() {
}
}
private fun ConeKotlinType.isKClassWithBadArgument(session: FirSession): Boolean {
private fun ConeKotlinType.isKClassTypeWithErrorOrNullableArgument(session: FirSession): Boolean {
if (!this.isKClassType()) return false
val argumentType = typeArguments.toList().singleOrNull()?.let {
when (it) {
@@ -51,6 +51,7 @@ private fun ConeDiagnostic.toFirDiagnostic(source: FirSourceElement): FirDiagnos
is ConeIntermediateDiagnostic -> null
is ConeContractDescriptionError -> FirErrors.ERROR_IN_CONTRACT_DESCRIPTION.on(source, this.reason)
is ConeTypeParameterSupertype -> FirErrors.SUPERTYPE_NOT_A_CLASS_OR_INTERFACE.on(source, this.reason)
is ConeTypeParameterInQualifiedAccess -> null // reported in various checkers instead
else -> throw IllegalArgumentException("Unsupported diagnostic type: ${this.javaClass}")
}