[FE 1.0] Prohibit call of constructor of functional supertype

^KT-46344
^KT-50730 Fixed
This commit is contained in:
Dmitriy Novozhilov
2022-01-11 17:17:45 +03:00
committed by teamcity
parent ecc890efe8
commit ff7eb79bb1
16 changed files with 319 additions and 1 deletions
@@ -837,6 +837,7 @@ public interface Errors {
DiagnosticFactory0<PsiElement> TAIL_RECURSION_IN_TRY_IS_NOT_SUPPORTED = DiagnosticFactory0.create(WARNING, CALL_EXPRESSION);
DiagnosticFactory0<PsiElement> NO_CONSTRUCTOR = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NO_CONSTRUCTOR_WARNING = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<PsiElement> CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtExpression> NOT_A_CLASS = DiagnosticFactory0.create(ERROR);
@@ -440,6 +440,7 @@ public class DefaultErrorMessages {
MAP.put(DELEGATION_NOT_TO_INTERFACE, "Only interfaces can be delegated to");
MAP.put(DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE, "Delegated member ''{0}'' hides supertype override: {1}. Please specify proper override explicitly", COMPACT, commaSeparated(SHORT_NAMES_IN_TYPES));
MAP.put(NO_CONSTRUCTOR, "This class does not have a constructor");
MAP.put(NO_CONSTRUCTOR_WARNING, "This class does not have a constructor. This warning will be an error in future releases");
MAP.put(RESOLUTION_TO_CLASSIFIER, "{2}", NAME, TO_STRING, STRING);
MAP.put(NOT_A_CLASS, "Not a class");
MAP.put(ILLEGAL_ESCAPE_SEQUENCE, "Illegal escape sequence");
@@ -382,6 +382,7 @@ public class CallResolver {
KtReferenceExpression functionReference = expression.getConstructorReferenceExpression();
KtTypeReference typeReference = expression.getTypeReference();
if (functionReference == null || typeReference == null) {
CallResolverUtilKt.checkForConstructorCallOnFunctionalType(typeReference, context);
return checkArgumentTypesAndFail(context); // No type there
}
KotlinType constructedType = typeResolver.resolveType(context.scope, typeReference, context.trace, true);
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtToken
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
@@ -376,3 +377,16 @@ internal fun PSIKotlinCall.replaceArguments(
callKind, psiCall, tracingStrategy, newReceiverArgument, dispatchReceiverForInvokeExtension, name, typeArguments, newArguments,
externalArgument, startingDataFlowInfo, resultDataFlowInfo, dataFlowInfoForArguments, isForImplicitInvoke
)
fun checkForConstructorCallOnFunctionalType(
typeReference: KtTypeReference?,
context: BasicCallResolutionContext
) {
if (typeReference?.typeElement is KtFunctionType) {
val factory = when (context.languageVersionSettings.supportsFeature(LanguageFeature.ProhibitConstructorCallOnFunctionalSupertype)) {
true -> Errors.NO_CONSTRUCTOR
false -> Errors.NO_CONSTRUCTOR_WARNING
}
context.trace.report(factory.on(context.call.getValueArgumentListOrElement()))
}
}