[FIR] Fix disappeared REPEATED_ANNOTATION for dynamic types

https://youtrack.jetbrains.com/issue/KT-59916/K2-Disappeared-REPEATEDANNOTATION
FirAnnotationChecker does not detect repeated annotation on dynamic type, since FirTypeResolverImpl wrongly did not convert source annotations to attributes of ConeDynamicType.
This MR improves FirTypeResolverImpl to convert attributes of FirDynamicTypeRef to annotations and attach them to ConeDynamicType.

Merge-request: KT-MR-12551
Merged-by: Vladimir Sukharev <Vladimir.Sukharev@jetbrains.com>
This commit is contained in:
Vladimir Sukharev
2023-10-17 10:18:36 +00:00
committed by Space Team
parent 22a01432df
commit 242c1cf5f0
12 changed files with 75 additions and 8 deletions
@@ -2136,6 +2136,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/annotations/typeParameterAsAnnotation.kt");
}
@Test
@TestMetadata("typealiasWithAnnotatedAbstractClass.kt")
public void testTypealiasWithAnnotatedAbstractClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/typealiasWithAnnotatedAbstractClass.kt");
}
@Test
@TestMetadata("UnresolvedAnnotationOnObject.kt")
public void testUnresolvedAnnotationOnObject() throws Exception {
@@ -2136,6 +2136,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/annotations/typeParameterAsAnnotation.kt");
}
@Test
@TestMetadata("typealiasWithAnnotatedAbstractClass.kt")
public void testTypealiasWithAnnotatedAbstractClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/typealiasWithAnnotatedAbstractClass.kt");
}
@Test
@TestMetadata("UnresolvedAnnotationOnObject.kt")
public void testUnresolvedAnnotationOnObject() throws Exception {
@@ -2136,6 +2136,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/annotations/typeParameterAsAnnotation.kt");
}
@Test
@TestMetadata("typealiasWithAnnotatedAbstractClass.kt")
public void testTypealiasWithAnnotatedAbstractClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/typealiasWithAnnotatedAbstractClass.kt");
}
@Test
@TestMetadata("UnresolvedAnnotationOnObject.kt")
public void testUnresolvedAnnotationOnObject() throws Exception {
@@ -2142,6 +2142,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/annotations/typeParameterAsAnnotation.kt");
}
@Test
@TestMetadata("typealiasWithAnnotatedAbstractClass.kt")
public void testTypealiasWithAnnotatedAbstractClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/typealiasWithAnnotatedAbstractClass.kt");
}
@Test
@TestMetadata("UnresolvedAnnotationOnObject.kt")
public void testUnresolvedAnnotationOnObject() throws Exception {
@@ -98,8 +98,13 @@ fun ConeDefinitelyNotNullType.Companion.create(
}
@OptIn(DynamicTypeConstructor::class)
fun ConeDynamicType.Companion.create(session: FirSession): ConeDynamicType =
ConeDynamicType(session.builtinTypes.nothingType.type, session.builtinTypes.nullableAnyType.type)
fun ConeDynamicType.Companion.create(
session: FirSession,
attributes: ConeAttributes = ConeAttributes.Empty,
) = ConeDynamicType(
session.builtinTypes.nothingType.type.withAttributes(attributes),
session.builtinTypes.nullableAnyType.type.withAttributes(attributes),
)
/**
* This call is required if you want to use a type with annotations that are linked to a declaration from this declaration inside another
@@ -417,7 +417,14 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() {
FirTypeResolutionResult(resolvedType, (result as? TypeResolutionResult.Resolved)?.typeCandidate?.diagnostic)
}
is FirFunctionTypeRef -> createFunctionType(typeRef, scopeClassDeclaration.containerDeclaration)
is FirDynamicTypeRef -> FirTypeResolutionResult(ConeDynamicType.create(session), diagnostic = null)
is FirDynamicTypeRef -> {
val attributes = typeRef.annotations.computeTypeAttributes(
session,
containerDeclaration = scopeClassDeclaration.containerDeclaration,
shouldExpandTypeAliases = true
)
FirTypeResolutionResult(ConeDynamicType.create(session, attributes), diagnostic = null)
}
is FirIntersectionTypeRef -> {
val leftType = typeRef.leftType.coneType
if (leftType is ConeTypeParameterType) {
@@ -0,0 +1,14 @@
// FIR_IDENTICAL
@Target(AnnotationTarget.TYPE)
annotation class Ann1
@Target(AnnotationTarget.TYPE)
annotation class Ann2
abstract class AnnGenList<T> : List<@Ann1 T> {}
typealias TestAnnGen1 = AnnGenList<String>
typealias TestAnnGen2 = AnnGenList<@Ann2 String>
typealias TestAnnGen3 = AnnGenList<@Ann1 String> // KT-62602: should REPEATED_ANNOTATION diagnostic be raised here?
fun useAnnGen1(x: TestAnnGen1) = x
fun useAnnGen2(x: TestAnnGen2) = x
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
@Target(AnnotationTarget.TYPE)
annotation class Ann1
@@ -1,16 +1,14 @@
// FIR_IDENTICAL
@Target(AnnotationTarget.TYPE)
annotation class Ann1
@Target(AnnotationTarget.TYPE)
annotation class Ann2
typealias AnnGenList<T> = List<@Ann1 T>
abstract class AnnGenList<T> : List<@Ann1 T> {}
typealias TestAnnGen1 = AnnGenList<dynamic>
typealias TestAnnGen2 = AnnGenList<@Ann2 dynamic>
typealias TestAnnGen3 = AnnGenList<@Ann1 dynamic>
typealias TestAnnGen3 = AnnGenList<@Ann1 dynamic> // KT-62602: should REPEATED_ANNOTATION diagnostic be raised here?
fun useAnnGen1(x: TestAnnGen1) = x
fun useAnnGen2(x: TestAnnGen2) = x
fun testUseAnnGen1(x: List<dynamic>) = useAnnGen1(x)
fun testUseAnnGen2(x: List<dynamic>) = useAnnGen2(x)
@@ -2142,6 +2142,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/annotations/typeParameterAsAnnotation.kt");
}
@Test
@TestMetadata("typealiasWithAnnotatedAbstractClass.kt")
public void testTypealiasWithAnnotatedAbstractClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/typealiasWithAnnotatedAbstractClass.kt");
}
@Test
@TestMetadata("UnresolvedAnnotationOnObject.kt")
public void testUnresolvedAnnotationOnObject() throws Exception {
@@ -428,6 +428,12 @@ public class FirPsiJsOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiJ
runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasWithAnnotatedDynamic.kt");
}
@Test
@TestMetadata("typealiasWithAnnotatedDynamicInAbstractClass.kt")
public void testTypealiasWithAnnotatedDynamicInAbstractClass() throws Exception {
runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasWithAnnotatedDynamicInAbstractClass.kt");
}
@Test
@TestMetadata("typealiasWithDynamic.kt")
public void testTypealiasWithDynamic() throws Exception {
@@ -428,6 +428,12 @@ public class DiagnosticsWithJsStdLibTestGenerated extends AbstractDiagnosticsTes
runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasWithAnnotatedDynamic.kt");
}
@Test
@TestMetadata("typealiasWithAnnotatedDynamicInAbstractClass.kt")
public void testTypealiasWithAnnotatedDynamicInAbstractClass() throws Exception {
runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasWithAnnotatedDynamicInAbstractClass.kt");
}
@Test
@TestMetadata("typealiasWithDynamic.kt")
public void testTypealiasWithDynamic() throws Exception {