[FE 1.0] Prohibit array literals inside nested objects of annotation classes

^KT-39041
^KT-50749 Fixed
This commit is contained in:
Dmitriy Novozhilov
2022-01-12 11:29:10 +03:00
committed by teamcity
parent c1575edca4
commit 139a800ff7
14 changed files with 384 additions and 6 deletions
@@ -1127,6 +1127,18 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/annotations/annotationsOnNullableTypes.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_after.kt")
public void testArrayLiteralInAnnotationCompanion_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_after.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_before.kt")
public void testArrayLiteralInAnnotationCompanion_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_before.kt");
}
@Test
@TestMetadata("atAnnotationResolve.kt")
public void testAtAnnotationResolve() throws Exception {
@@ -1127,6 +1127,18 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/annotations/annotationsOnNullableTypes.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_after.kt")
public void testArrayLiteralInAnnotationCompanion_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_after.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_before.kt")
public void testArrayLiteralInAnnotationCompanion_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_before.kt");
}
@Test
@TestMetadata("atAnnotationResolve.kt")
public void testAtAnnotationResolve() throws Exception {
@@ -1127,6 +1127,18 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/annotations/annotationsOnNullableTypes.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_after.kt")
public void testArrayLiteralInAnnotationCompanion_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_after.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_before.kt")
public void testArrayLiteralInAnnotationCompanion_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_before.kt");
}
@Test
@TestMetadata("atAnnotationResolve.kt")
public void testAtAnnotationResolve() throws Exception {
@@ -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);
@@ -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 {
@@ -0,0 +1,48 @@
// LANGUAGE: +ProhibitArrayLiteralsInCompanionOfAnnotation
// ISSUE: KT-39041
annotation class Ann(val x: IntArray = [1, 2, 3]) { // OK
companion object {
val y1: IntArray = [1, 2, 3] // Error
val z1: IntArray
get() = [1, 2, 3] // Error
fun test_1(): IntArray {
return [1, 2, 3] // Error
}
class Nested {
val y2: IntArray = [1, 2, 3] // Error
val z2: IntArray
get() = [1, 2, 3] // Error
fun test_2(): IntArray {
return [1, 2, 3] // Error
}
}
}
object Foo {
val y3: IntArray = [1, 2, 3] // Error
val z3: IntArray
get() = [1, 2, 3] // Error
fun test_3(): IntArray {
return [1, 2, 3] // Error
}
}
class Nested {
val y4: IntArray = [1, 2, 3] // Error
val z4: IntArray
get() = [1, 2, 3] // Error
fun test_4(): IntArray {
return [1, 2, 3] // Error
}
}
}
@@ -0,0 +1,48 @@
// LANGUAGE: +ProhibitArrayLiteralsInCompanionOfAnnotation
// ISSUE: KT-39041
annotation class Ann(val x: IntArray = [1, 2, 3]) { // OK
companion object {
val y1: IntArray = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
val z1: IntArray
get() = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
fun test_1(): IntArray {
return <!UNSUPPORTED!>[1, 2, 3]<!> // Error
}
class Nested {
val y2: IntArray = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
val z2: IntArray
get() = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
fun test_2(): IntArray {
return <!UNSUPPORTED!>[1, 2, 3]<!> // Error
}
}
}
object Foo {
val y3: IntArray = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
val z3: IntArray
get() = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
fun test_3(): IntArray {
return <!UNSUPPORTED!>[1, 2, 3]<!> // Error
}
}
class Nested {
val y4: IntArray = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
val z4: IntArray
get() = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
fun test_4(): IntArray {
return <!UNSUPPORTED!>[1, 2, 3]<!> // Error
}
}
}
@@ -0,0 +1,49 @@
package
public final annotation class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ x: kotlin.IntArray = ...)
public final val x: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public final val y1: kotlin.IntArray
public final val z1: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_1(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final class Nested {
public constructor Nested()
public final val y2: kotlin.IntArray
public final val z2: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_2(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public object Foo {
private constructor Foo()
public final val y3: kotlin.IntArray
public final val z3: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_3(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Nested {
public constructor Nested()
public final val y4: kotlin.IntArray
public final val z4: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_4(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,48 @@
// LANGUAGE: -ProhibitArrayLiteralsInCompanionOfAnnotation
// ISSUE: KT-39041
annotation class Ann(val x: IntArray = [1, 2, 3]) { // OK
companion object {
val y1: IntArray = [1, 2, 3] // Error
val z1: IntArray
get() = [1, 2, 3] // Error
fun test_1(): IntArray {
return [1, 2, 3] // Error
}
class Nested {
val y2: IntArray = [1, 2, 3] // Error
val z2: IntArray
get() = [1, 2, 3] // Error
fun test_2(): IntArray {
return [1, 2, 3] // Error
}
}
}
object Foo {
val y3: IntArray = [1, 2, 3] // Error
val z3: IntArray
get() = [1, 2, 3] // Error
fun test_3(): IntArray {
return [1, 2, 3] // Error
}
}
class Nested {
val y4: IntArray = [1, 2, 3] // Error
val z4: IntArray
get() = [1, 2, 3] // Error
fun test_4(): IntArray {
return [1, 2, 3] // Error
}
}
}
@@ -0,0 +1,48 @@
// LANGUAGE: -ProhibitArrayLiteralsInCompanionOfAnnotation
// ISSUE: KT-39041
annotation class Ann(val x: IntArray = [1, 2, 3]) { // OK
companion object {
val y1: IntArray = <!UNSUPPORTED_WARNING!>[1, 2, 3]<!> // Error
val z1: IntArray
get() = <!UNSUPPORTED_WARNING!>[1, 2, 3]<!> // Error
fun test_1(): IntArray {
return <!UNSUPPORTED_WARNING!>[1, 2, 3]<!> // Error
}
class Nested {
val y2: IntArray = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
val z2: IntArray
get() = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
fun test_2(): IntArray {
return <!UNSUPPORTED!>[1, 2, 3]<!> // Error
}
}
}
object Foo {
val y3: IntArray = <!UNSUPPORTED_WARNING!>[1, 2, 3]<!> // Error
val z3: IntArray
get() = <!UNSUPPORTED_WARNING!>[1, 2, 3]<!> // Error
fun test_3(): IntArray {
return <!UNSUPPORTED_WARNING!>[1, 2, 3]<!> // Error
}
}
class Nested {
val y4: IntArray = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
val z4: IntArray
get() = <!UNSUPPORTED!>[1, 2, 3]<!> // Error
fun test_4(): IntArray {
return <!UNSUPPORTED!>[1, 2, 3]<!> // Error
}
}
}
@@ -0,0 +1,50 @@
package
public final annotation class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ x: kotlin.IntArray = ...)
public final val x: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public final val y1: kotlin.IntArray
public final val z1: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_1(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final class Nested {
public constructor Nested()
public final val y2: kotlin.IntArray
public final val z2: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_2(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public object Foo {
private constructor Foo()
public final val y3: kotlin.IntArray
public final val z3: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_3(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Nested {
public constructor Nested()
public final val y4: kotlin.IntArray
public final val z4: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test_4(): kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -1127,6 +1127,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/annotations/annotationsOnNullableTypes.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_after.kt")
public void testArrayLiteralInAnnotationCompanion_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_after.kt");
}
@Test
@TestMetadata("arrayLiteralInAnnotationCompanion_before.kt")
public void testArrayLiteralInAnnotationCompanion_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/arrayLiteralInAnnotationCompanion_before.kt");
}
@Test
@TestMetadata("atAnnotationResolve.kt")
public void testAtAnnotationResolve() throws Exception {
@@ -258,6 +258,7 @@ enum class LanguageFeature(
DisableCheckingChangedProgressionsResolve(KOTLIN_1_9), // KT-49276
ProhibitIllegalValueParameterUsageInDefaultArguments(KOTLIN_1_9, kind = BUG_FIX), // KT-25694
ProhibitConstructorCallOnFunctionalSupertype(KOTLIN_1_9, kind = BUG_FIX), // KT-46344
ProhibitArrayLiteralsInCompanionOfAnnotation(KOTLIN_1_9, kind = BUG_FIX), // KT-39041
// Temporarily disabled, see KT-27084/KT-22379
SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX),