[FE 1.0] Prohibit array literals inside nested objects of annotation classes
^KT-39041 ^KT-50749 Fixed
This commit is contained in:
committed by
teamcity
parent
c1575edca4
commit
139a800ff7
@@ -60,6 +60,7 @@ public interface Errors {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DiagnosticFactory1<PsiElement, String> UNSUPPORTED = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory1<PsiElement, String> UNSUPPORTED_WARNING = DiagnosticFactory1.create(WARNING);
|
||||
|
||||
DiagnosticFactory1<PsiElement, String> NEW_INFERENCE_ERROR = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory1<PsiElement, String> NEW_INFERENCE_DIAGNOSTIC = DiagnosticFactory1.create(WARNING);
|
||||
|
||||
+1
@@ -716,6 +716,7 @@ public class DefaultErrorMessages {
|
||||
MAP.put(UNSAFE_IMPLICIT_INVOKE_CALL, "Reference has a nullable type ''{0}'', use explicit ''?.invoke()'' to make a function-like call instead", RENDER_TYPE);
|
||||
MAP.put(AMBIGUOUS_LABEL, "Ambiguous label");
|
||||
MAP.put(UNSUPPORTED, "Unsupported [{0}]", STRING);
|
||||
MAP.put(UNSUPPORTED_WARNING, "Unsupported [{0}]. This warning will be an error in future releases", STRING);
|
||||
MAP.put(NEW_INFERENCE_ERROR, "New inference error [{0}]", STRING);
|
||||
MAP.put(NEW_INFERENCE_DIAGNOSTIC, "New inference [{0}]", STRING);
|
||||
MAP.put(NON_APPLICABLE_CALL_FOR_BUILDER_INFERENCE, "Non-applicable call for builder inference");
|
||||
|
||||
@@ -5,21 +5,25 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.builtins.UnsignedTypes
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageFeature.*
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.*
|
||||
import org.jetbrains.kotlin.incremental.KotlinLookupLocation
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
|
||||
import org.jetbrains.kotlin.psi.KtObjectDeclaration
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.COLLECTION_LITERAL_CALL
|
||||
import org.jetbrains.kotlin.resolve.CollectionLiteralResolver.ContainerKind.*
|
||||
import org.jetbrains.kotlin.resolve.calls.CallResolver
|
||||
import org.jetbrains.kotlin.resolve.calls.util.CallMaker
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
@@ -38,13 +42,29 @@ class CollectionLiteralResolver(
|
||||
collectionLiteralExpression: KtCollectionLiteralExpression,
|
||||
context: ExpressionTypingContext
|
||||
): KotlinTypeInfo {
|
||||
if (!isInsideAnnotationEntryOrClass(collectionLiteralExpression)) {
|
||||
context.trace.report(UNSUPPORTED.on(collectionLiteralExpression, "Collection literals outside of annotations"))
|
||||
when (computeKindOfContainer(collectionLiteralExpression)) {
|
||||
AnnotationOrAnnotationClass -> {}
|
||||
CompanionOfAnnotation -> {
|
||||
val factory = when (context.languageVersionSettings.supportsFeature(ProhibitArrayLiteralsInCompanionOfAnnotation)) {
|
||||
true -> UNSUPPORTED
|
||||
false -> UNSUPPORTED_WARNING
|
||||
}
|
||||
reportUnsupportedLiteral(context, factory, collectionLiteralExpression)
|
||||
}
|
||||
Other -> reportUnsupportedLiteral(context, UNSUPPORTED, collectionLiteralExpression)
|
||||
}
|
||||
|
||||
return resolveCollectionLiteralSpecialMethod(collectionLiteralExpression, context)
|
||||
}
|
||||
|
||||
private fun reportUnsupportedLiteral(
|
||||
context: ExpressionTypingContext,
|
||||
diagnosticFactory: DiagnosticFactory1<PsiElement, String>,
|
||||
collectionLiteralExpression: KtCollectionLiteralExpression
|
||||
) {
|
||||
context.trace.report(diagnosticFactory.on(collectionLiteralExpression, "Collection literals outside of annotations"))
|
||||
}
|
||||
|
||||
private fun resolveCollectionLiteralSpecialMethod(
|
||||
expression: KtCollectionLiteralExpression,
|
||||
context: ExpressionTypingContext
|
||||
@@ -79,9 +99,25 @@ class CollectionLiteralResolver(
|
||||
return memberScopeOfKotlinPackage.getContributedFunctions(callName, KotlinLookupLocation(expression))
|
||||
}
|
||||
|
||||
private fun isInsideAnnotationEntryOrClass(expression: KtCollectionLiteralExpression): Boolean {
|
||||
val parent = PsiTreeUtil.getParentOfType(expression, KtAnnotationEntry::class.java, KtClass::class.java)
|
||||
return parent is KtAnnotationEntry || (parent is KtClass && parent.isAnnotation())
|
||||
private enum class ContainerKind {
|
||||
AnnotationOrAnnotationClass,
|
||||
CompanionOfAnnotation,
|
||||
Other
|
||||
}
|
||||
|
||||
private fun computeKindOfContainer(expression: KtCollectionLiteralExpression): ContainerKind {
|
||||
val parent = PsiTreeUtil.getParentOfType(expression, KtAnnotationEntry::class.java, KtClass::class.java, KtObjectDeclaration::class.java)
|
||||
if (parent is KtObjectDeclaration) {
|
||||
val containingAnnotation = PsiTreeUtil.getParentOfType(parent, KtClass::class.java)
|
||||
if (containingAnnotation != null && containingAnnotation.isAnnotation()) {
|
||||
return CompanionOfAnnotation
|
||||
}
|
||||
}
|
||||
return if (parent is KtAnnotationEntry || (parent is KtClass && parent.isAnnotation())) {
|
||||
AnnotationOrAnnotationClass
|
||||
} else {
|
||||
Other
|
||||
}
|
||||
}
|
||||
|
||||
private fun getArrayFunctionCallName(expectedType: KotlinType): Name {
|
||||
|
||||
Reference in New Issue
Block a user