Remove mapping of java.Repeatable to kotlin.Repeatable in JavaAnnotationMapper

The main motivation for this change is that
java.lang.annotation.Repeatable has a parameter for the container
annotation, which is lost during conversion to
kotlin.annotation.Repeatable. To support j.l.a.Repeatable in backend
properly, it's absolutely necessary to be able to load the container
annotation for any repeatable annotation class, so the original
j.l.a.Repeatable needs to be stored in the descriptor and accessible
from the backend.

Instead of mapping j.l.a.Repeatable -> k.a.Repeatable, add a frontend
service PlatformAnnotationFeaturesSupport that will determine if an
annotation is repeatable "according to the platform rules", which for
JVM means that it's annotated with j.l.a.Repeatable.

Some effects of this change include:
- Usages of j.l.a.Repeatable are no longer reported as "deprecated", the
  corresponding test is deleted
- Usages of repeatable annotations declared in Java with non-SOURCE
  retention with LV 1.5 and earlier will now result in a slightly
  different error (REPEATED_ANNOTATION instead of
  NON_SOURCE_REPEATED_ANNOTATION)

 #KT-12794
This commit is contained in:
Alexander Udalov
2021-07-20 18:08:10 +02:00
parent ebf837c135
commit f723389565
15 changed files with 64 additions and 90 deletions
@@ -34,7 +34,8 @@ import org.jetbrains.kotlin.types.isError
class AnnotationChecker(
private val additionalCheckers: Iterable<AdditionalAnnotationChecker>,
private val languageVersionSettings: LanguageVersionSettings
private val languageVersionSettings: LanguageVersionSettings,
private val platformAnnotationFeaturesSupport: PlatformAnnotationFeaturesSupport,
) {
fun check(annotated: KtAnnotated, trace: BindingTrace, descriptor: DeclarationDescriptor? = null) {
val actualTargets = getActualTargetList(annotated, descriptor, trace.bindingContext)
@@ -132,7 +133,7 @@ class AnnotationChecker(
val useSiteTarget = entry.useSiteTarget?.getAnnotationUseSiteTarget() ?: property.getDefaultUseSiteTarget(descriptor)
val existingAnnotations = propertyAnnotations[useSiteTarget] ?: continue
if (classDescriptor in existingAnnotations && !classDescriptor.isRepeatableAnnotation()) {
if (classDescriptor in existingAnnotations && !isRepeatableAnnotation(classDescriptor)) {
if (reportError) {
trace.reportDiagnosticOnce(Errors.REPEATED_ANNOTATION.on(entry))
} else {
@@ -231,7 +232,7 @@ class AnnotationChecker(
val duplicateAnnotation = useSiteTarget in existingTargetsForAnnotation
|| (existingTargetsForAnnotation.any { (it == null) != (useSiteTarget == null) })
if (duplicateAnnotation && !classDescriptor.isRepeatableAnnotation()) {
if (duplicateAnnotation && !isRepeatableAnnotation(classDescriptor)) {
trace.report(Errors.REPEATED_ANNOTATION.on(entry))
}
@@ -292,6 +293,9 @@ class AnnotationChecker(
}
}
private fun isRepeatableAnnotation(descriptor: ClassDescriptor): Boolean =
descriptor.isRepeatableAnnotation() || platformAnnotationFeaturesSupport.isRepeatableAnnotationClass(descriptor)
companion object {
private val TARGET_ALLOWED_TARGETS = Name.identifier("allowedTargets")
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.container.DefaultImplementation
import org.jetbrains.kotlin.descriptors.ClassDescriptor
@DefaultImplementation(PlatformAnnotationFeaturesSupport.Default::class)
interface PlatformAnnotationFeaturesSupport {
fun isRepeatableAnnotationClass(descriptor: ClassDescriptor): Boolean
object Default : PlatformAnnotationFeaturesSupport {
override fun isRepeatableAnnotationClass(descriptor: ClassDescriptor): Boolean = false
}
}