Forbid ExtensionFunctionType on functional types without parameters

#KT-43527 Fixed
This commit is contained in:
Mikhail Glukhikh
2022-01-11 16:42:35 +03:00
parent beb2957726
commit d0fa3eb1d3
14 changed files with 86 additions and 13 deletions
@@ -278,6 +278,7 @@ public interface Errors {
DiagnosticFactory0<KtAnnotationEntry> REPEATED_ANNOTATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> REPEATED_ANNOTATION_WARNING = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtAnnotationEntry> NON_SOURCE_ANNOTATION_ON_INLINED_LAMBDA_EXPRESSION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> WRONG_EXTENSION_FUNCTION_TYPE = DiagnosticFactory0.create(ERROR);
// Annotations
@@ -146,6 +146,7 @@ public class DefaultErrorMessages {
MAP.put(REPEATED_ANNOTATION, "This annotation is not repeatable");
MAP.put(REPEATED_ANNOTATION_WARNING, "This annotation is not repeatable");
MAP.put(NON_SOURCE_ANNOTATION_ON_INLINED_LAMBDA_EXPRESSION, "The lambda expression here is an inlined argument so this annotation cannot be stored anywhere");
MAP.put(WRONG_EXTENSION_FUNCTION_TYPE, "ExtensionFunctionType is forbidden on a function type without parameters");
MAP.put(INAPPLICABLE_TARGET_ON_PROPERTY, "''@{0}:'' annotations could be applied only to property declarations", TO_STRING);
MAP.put(INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE, "''@{0}:'' annotations could be applied only to mutable properties", TO_STRING);
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.isFunctionOrKFunctionTypeWithAnySuspendability
import org.jetbrains.kotlin.config.LanguageFeature.*
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ClassDescriptor
@@ -19,11 +20,13 @@ import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries
import org.jetbrains.kotlin.resolve.constants.ArrayValue
import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.resolve.descriptorUtil.getAnnotationRetention
import org.jetbrains.kotlin.resolve.descriptorUtil.isAnnotatedWithKotlinRepeatable
import org.jetbrains.kotlin.resolve.inline.InlineUtil
@@ -163,9 +166,9 @@ class AnnotationChecker(
checkWithoutLanguageFeature: Boolean = false
) {
val shouldRunCheck = isSuperType || shouldCheckReferenceItself
if (shouldRunCheck) {
for (entry in reference.annotationEntries) {
val descriptor = trace.get(BindingContext.ANNOTATION, entry)
for (entry in reference.annotationEntries) {
val descriptor = trace.get(BindingContext.ANNOTATION, entry)
if (shouldRunCheck) {
if (descriptor is LazyAnnotationDescriptor) {
/*
* There are no users of type annotations until backend, so if there are errors
@@ -181,6 +184,12 @@ class AnnotationChecker(
checkAnnotationEntry(entry, actualTargets, trace)
}
}
if (descriptor?.annotationClass?.classId == StandardClassIds.Annotations.ExtensionFunctionType) {
val type = trace[BindingContext.TYPE, reference]
if (type != null && type.isFunctionOrKFunctionTypeWithAnySuspendability && type.arguments.size <= 1) {
trace.report(Errors.WRONG_EXTENSION_FUNCTION_TYPE.on(entry))
}
}
}
val typeArguments = reference.typeElement?.typeArgumentsAsTypes ?: return
for (typeArgument in typeArguments) {