K2: support ForbidInferringTypeVariablesIntoEmptyIntersection on/off

This commit is contained in:
Mikhail Glukhikh
2023-02-28 09:40:19 +01:00
committed by Space Team
parent f7544aff62
commit b64cb67370
43 changed files with 86 additions and 248 deletions
@@ -737,7 +737,9 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
parameter<FqName>("kotlinClass")
}
val INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION by error<PsiElement> {
val INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION by deprecationError<PsiElement>(
LanguageFeature.ForbidInferringTypeVariablesIntoEmptyIntersection
) {
parameter<String>("typeVariableDescription")
parameter<Collection<ConeKotlinType>>("incompatibleTypes")
parameter<String>("description")
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageFeature.ForbidExposingTypesInPrimaryConstructorProperties
import org.jetbrains.kotlin.config.LanguageFeature.ForbidInferringTypeVariablesIntoEmptyIntersection
import org.jetbrains.kotlin.config.LanguageFeature.ForbidUsingExtensionPropertyTypeParameterInDelegate
import org.jetbrains.kotlin.config.LanguageFeature.ModifierNonBuiltinSuspendFunError
import org.jetbrains.kotlin.config.LanguageFeature.ProhibitAssigningSingleElementsToVarargsInNamedForm
@@ -433,7 +434,7 @@ object FirErrors {
val SMARTCAST_IMPOSSIBLE by error4<KtExpression, ConeKotlinType, FirExpression, String, Boolean>()
val REDUNDANT_NULLABLE by warning0<KtTypeReference>(SourceElementPositioningStrategies.REDUNDANT_NULLABLE)
val PLATFORM_CLASS_MAPPED_TO_KOTLIN by warning1<PsiElement, FqName>(SourceElementPositioningStrategies.REFERENCED_NAME_BY_QUALIFIED)
val INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION by error4<PsiElement, String, Collection<ConeKotlinType>, String, String>()
val INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION by deprecationError4<PsiElement, String, Collection<ConeKotlinType>, String, String>(ForbidInferringTypeVariablesIntoEmptyIntersection)
val INFERRED_TYPE_VARIABLE_INTO_POSSIBLE_EMPTY_INTERSECTION by warning4<PsiElement, String, Collection<ConeKotlinType>, String, String>()
val INCORRECT_LEFT_COMPONENT_OF_INTERSECTION by error0<KtTypeReference>()
val INCORRECT_RIGHT_COMPONENT_OF_INTERSECTION by error0<KtTypeReference>()
@@ -292,8 +292,13 @@ private fun mapInapplicableCandidateError(
)
}
is InferredEmptyIntersectionDiagnostic -> reportInferredIntoEmptyIntersectionError(
source, rootCause.typeVariable, rootCause.incompatibleTypes, rootCause.causingTypes, rootCause.kind
is InferredEmptyIntersectionDiagnostic -> reportInferredIntoEmptyIntersection(
source,
rootCause.typeVariable,
rootCause.incompatibleTypes,
rootCause.causingTypes,
rootCause.kind,
isError = rootCause.isError
)
else -> genericDiagnostic
@@ -434,12 +439,13 @@ private fun ConstraintSystemError.toDiagnostic(
is InferredEmptyIntersection -> {
@Suppress("UNCHECKED_CAST")
reportInferredIntoEmptyIntersectionError(
reportInferredIntoEmptyIntersection(
source,
typeVariable as ConeTypeVariable,
incompatibleTypes as Collection<ConeKotlinType>,
causingTypes as Collection<ConeKotlinType>,
kind
kind,
this is InferredEmptyIntersectionError,
)
}
@@ -454,20 +460,24 @@ private fun ConstraintSystemError.toDiagnostic(
}
}
private fun reportInferredIntoEmptyIntersectionError(
private fun reportInferredIntoEmptyIntersection(
source: KtSourceElement,
typeVariable: ConeTypeVariable,
incompatibleTypes: Collection<ConeKotlinType>,
causingTypes: Collection<ConeKotlinType>,
kind: EmptyIntersectionTypeKind
kind: EmptyIntersectionTypeKind,
isError: Boolean
): KtDiagnostic? {
val typeVariableText =
(typeVariable.typeConstructor.originalTypeParameter as? ConeTypeParameterLookupTag)?.name?.asString()
?: typeVariable.toString()
val causingTypesText = if (incompatibleTypes == causingTypes) "" else ": ${causingTypes.joinToString()}"
val factory =
if (kind.isDefinitelyEmpty) FirErrors.INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION
else FirErrors.INFERRED_TYPE_VARIABLE_INTO_POSSIBLE_EMPTY_INTERSECTION
when {
!kind.isDefinitelyEmpty -> FirErrors.INFERRED_TYPE_VARIABLE_INTO_POSSIBLE_EMPTY_INTERSECTION
isError -> FirErrors.INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION.errorFactory
else -> FirErrors.INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION.warningFactory
}
return factory.createOn(source, typeVariableText, incompatibleTypes, kind.description, causingTypesText)
}
@@ -597,7 +597,10 @@ internal object CheckIncompatibleTypeVariableUpperBounds : ResolutionStage() {
upperTypes as List<ConeKotlinType>,
emptyIntersectionTypeInfo.casingTypes.toList() as List<ConeKotlinType>,
variableWithConstraints.typeVariable as ConeTypeVariable,
emptyIntersectionTypeInfo.kind
emptyIntersectionTypeInfo.kind,
isError = context.session.languageVersionSettings.supportsFeature(
LanguageFeature.ForbidInferringTypeVariablesIntoEmptyIntersection
)
)
)
}
@@ -33,7 +33,8 @@ class InferredEmptyIntersectionDiagnostic(
val incompatibleTypes: Collection<ConeKotlinType>,
val causingTypes: Collection<ConeKotlinType>,
val typeVariable: ConeTypeVariable,
val kind: EmptyIntersectionTypeKind
val kind: EmptyIntersectionTypeKind,
val isError: Boolean
) : ResolutionDiagnostic(INAPPLICABLE)
class TooManyArguments(