[FE 1.0] Report warnings or errors for upper bounded type variables by an empty intersection type

This commit is contained in:
Victor Petukhov
2022-02-11 11:49:59 +03:00
committed by teamcity
parent 9474406375
commit 65213e9a42
25 changed files with 233 additions and 12 deletions
@@ -897,6 +897,8 @@ public interface Errors {
DiagnosticFactoryForDeprecation1<PsiElement, TypeParameterDescriptor> TYPE_INFERENCE_ONLY_INPUT_TYPES =
DiagnosticFactoryForDeprecation1.create(LanguageFeature.StrictOnlyInputTypesChecks);
DiagnosticFactoryForDeprecation2<PsiElement, String, Collection<KotlinType>> INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION =
DiagnosticFactoryForDeprecation2.create(LanguageFeature.ForbidInferringTypeVariablesIntoEmptyIntersection);
DiagnosticFactory1<PsiElement, InferenceErrorData> TYPE_INFERENCE_UPPER_BOUND_VIOLATED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<KtElement, KotlinType, KotlinType> TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH = DiagnosticFactory2.create(ERROR);
DiagnosticFactory0<PsiElement> TYPE_INFERENCE_CANDIDATE_WITH_SAM_AND_VARARG = DiagnosticFactory0.create(WARNING);
@@ -964,6 +964,7 @@ public class DefaultErrorMessages {
MAP.put(TYPE_INFERENCE_INCORPORATION_ERROR, "Type inference failed. Please try to specify type arguments explicitly.");
MAP.put(TYPE_INFERENCE_ONLY_INPUT_TYPES, "Type inference failed. The value of the type parameter {0} should be mentioned in input types " +
"(argument types, receiver type or expected type). Try to specify it explicitly.", NAME);
MAP.put(INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION, "Type argument for a type parameter {0} can''t be inferred because it''s upper bounded by incompatible types: {1}", TO_STRING, RENDER_COLLECTION_OF_TYPES);
MAP.put(TYPE_INFERENCE_UPPER_BOUND_VIOLATED, "{0}", TYPE_INFERENCE_UPPER_BOUND_VIOLATED_RENDERER);
MAP.put(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH, "Type inference failed. Expected type mismatch: inferred type is {1} but {0} was expected", RENDER_TYPE, RENDER_TYPE);
MAP.put(TYPE_INFERENCE_CANDIDATE_WITH_SAM_AND_VARARG, "Please use spread operator to pass an array as vararg. It will be an error in 1.5.");
@@ -601,6 +601,21 @@ class DiagnosticReporterByTrackingStrategy(
)
}
}
InferredEmptyIntersectionError::class.java, InferredEmptyIntersectionWarning::class.java -> {
val typeVariable = (error as InferredEmptyIntersection).typeVariable
psiKotlinCall.psiCall.calleeExpression?.let {
val typeVariableText = (typeVariable as? TypeVariableFromCallableDescriptor)?.originalTypeParameter?.name?.asString()
?: typeVariable.toString()
trace.reportDiagnosticOnce(
@Suppress("UNCHECKED_CAST")
INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION.on(
context.languageVersionSettings, it, typeVariableText,
error.incompatibleTypes as Collection<KotlinType>
)
)
}
}
}
}