Forbid experimental markers on various targets #KT-45845 Fixed

In this commit we forbid experimental markers on:
- local variables
- value parameters
- fields, including delegate fields
- property getters
This commit is contained in:
Mikhail Glukhikh
2021-06-07 16:28:34 +03:00
committed by teamcityserver
parent eb9c658c1c
commit 63bc3f9708
6 changed files with 42 additions and 17 deletions
@@ -286,7 +286,7 @@ public interface Errors {
DiagnosticFactory1<KtAnnotationEntry, String> EXPERIMENTAL_ANNOTATION_WITH_WRONG_TARGET = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> EXPERIMENTAL_ANNOTATION_WITH_WRONG_RETENTION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> EXPERIMENTAL_ANNOTATION_ON_GETTER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtAnnotationEntry, String> EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> EXPERIMENTAL_ANNOTATION_ON_OVERRIDE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> EXPERIMENTAL_UNSIGNED_LITERALS = DiagnosticFactory1.create(WARNING);
@@ -170,7 +170,7 @@ public class DefaultErrorMessages {
MAP.put(EXPERIMENTAL_ANNOTATION_WITH_WRONG_TARGET, "Opt-in requirement marker annotation cannot be used on the following code elements: {0}. Please remove these targets", STRING);
MAP.put(EXPERIMENTAL_ANNOTATION_WITH_WRONG_RETENTION, "Opt-in requirement marker annotation cannot be used with SOURCE retention. Please replace retention with BINARY");
MAP.put(EXPERIMENTAL_ANNOTATION_ON_GETTER, "Opt-in requirement marker annotation cannot be used on getter. Please annotate property instead");
MAP.put(EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET, "Opt-in requirement marker annotation cannot be used on {0}", STRING);
MAP.put(EXPERIMENTAL_ANNOTATION_ON_OVERRIDE, "Opt-in requirement marker annotation on override requires the same marker on base declaration");
MAP.put(EXPERIMENTAL_UNSIGNED_LITERALS, "{0}", STRING);
@@ -64,16 +64,31 @@ class ExperimentalMarkerDeclarationAnnotationChecker(private val module: ModuleD
}
val annotationClass = annotation.annotationClass ?: continue
if (annotationClass.annotations.any { it.fqName in ExperimentalUsageChecker.EXPERIMENTAL_FQ_NAMES }) {
val annotationUseSiteTarget = entry.useSiteTarget?.getAnnotationUseSiteTarget()
if (KotlinTarget.PROPERTY_GETTER in actualTargets ||
entry.useSiteTarget?.getAnnotationUseSiteTarget() == AnnotationUseSiteTarget.PROPERTY_GETTER
annotationUseSiteTarget == AnnotationUseSiteTarget.PROPERTY_GETTER
) {
trace.report(Errors.EXPERIMENTAL_ANNOTATION_ON_GETTER.on(entry))
trace.report(Errors.EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET.on(entry, "getter"))
}
if (KotlinTarget.VALUE_PARAMETER in actualTargets && annotationUseSiteTarget == null ||
annotationUseSiteTarget == AnnotationUseSiteTarget.RECEIVER ||
annotationUseSiteTarget == AnnotationUseSiteTarget.SETTER_PARAMETER ||
annotationUseSiteTarget == AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER
) {
trace.report(Errors.EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET.on(entry, "parameter"))
}
if (KotlinTarget.LOCAL_VARIABLE in actualTargets) {
trace.report(Errors.EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET.on(entry, "variable"))
}
if (annotationUseSiteTarget == AnnotationUseSiteTarget.FIELD ||
annotationUseSiteTarget == AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD
) {
trace.report(Errors.EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET.on(entry, "field"))
}
val annotated = entry.getStrictParentOfType<KtAnnotated>() ?: continue
val useSiteTarget = entry.useSiteTarget?.getAnnotationUseSiteTarget()
if (annotated is KtCallableDeclaration &&
annotated !is KtPropertyAccessor &&
useSiteTarget == null &&
annotationUseSiteTarget == null &&
annotated.hasModifier(KtTokens.OVERRIDE_KEYWORD)
) {
val descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, annotated)