From 70d71f0f070414230c32b28ba34c8f7cc920ec1a Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Wed, 8 Jan 2020 14:57:26 +0100 Subject: [PATCH] Don't generate type annotations on synthetic accessors #KT-35843 Fixed --- .../kotlin/codegen/AnnotationCodegen.java | 15 +++++---- .../asmLike/typeAnnotations/property.txt | 2 -- .../codegen/asmLike/typeAnnotations/simple.kt | 6 +--- .../asmLike/typeAnnotations/simple.txt | 16 ++-------- .../typeAnnotations/syntheticAccessors.kt | 32 +++++++++++++++++++ .../typeAnnotations/syntheticAccessors.txt | 31 ++++++++++++++++++ ...smLikeInstructionListingTestGenerated.java | 5 +++ 7 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.kt create mode 100644 compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.txt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java index 802c931326a..23b20153cdd 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java @@ -139,7 +139,7 @@ public abstract class AnnotationCodegen { } generateAdditionalAnnotations(annotated, returnType, annotationDescriptorsAlreadyPresent); - generateTypeAnnotations(typeForTypeAnnotations); + generateTypeAnnotations(annotated, typeForTypeAnnotations); } private void generateAdditionalAnnotations( @@ -192,15 +192,17 @@ public abstract class AnnotationCodegen { } private static boolean isInvisibleFromTheOutside(@Nullable DeclarationDescriptor descriptor) { - if (descriptor instanceof CallableMemberDescriptor && KotlinTypeMapper.isAccessor((CallableMemberDescriptor) descriptor)) { - return true; - } + if (isAccessor(descriptor)) return true; if (descriptor instanceof MemberDescriptor) { return AsmUtil.getVisibilityAccessFlag((MemberDescriptor) descriptor) == Opcodes.ACC_PRIVATE; } return false; } + private static boolean isAccessor(@Nullable Annotated descriptor) { + return descriptor instanceof CallableMemberDescriptor && KotlinTypeMapper.isAccessor((CallableMemberDescriptor) descriptor); + } + private void generateNullabilityAnnotation(@Nullable KotlinType type, @NotNull Set annotationDescriptorsAlreadyPresent) { if (type == null) return; @@ -665,8 +667,9 @@ public abstract class AnnotationCodegen { return av == null ? NO_ANNOTATION_VISITOR : av; } - private void generateTypeAnnotations(@Nullable KotlinType type) { - if (type == null || + private void generateTypeAnnotations(@NotNull Annotated annotated, @Nullable KotlinType type) { + if (isAccessor(annotated) || + type == null || state.getTarget() == JvmTarget.JVM_1_6 || !state.getConfiguration().getBoolean(JVMConfigurationKeys.EMIT_JVM_TYPE_ANNOTATIONS)) { return; diff --git a/compiler/testData/codegen/asmLike/typeAnnotations/property.txt b/compiler/testData/codegen/asmLike/typeAnnotations/property.txt index 427aab1193b..5e14aac1f8d 100644 --- a/compiler/testData/codegen/asmLike/typeAnnotations/property.txt +++ b/compiler/testData/codegen/asmLike/typeAnnotations/property.txt @@ -58,8 +58,6 @@ public final class foo/Kotlin : java/lang/Object { public void () public final static java.lang.String access$getCompanionVarProperty$cp() - @Lfoo/TypeAnn;([name="1"]) : METHOD_RETURN, null - @Lfoo/TypeAnnBinary;([]) : METHOD_RETURN, null // invisible public final static java.lang.String access$getJvmStatic$cp() diff --git a/compiler/testData/codegen/asmLike/typeAnnotations/simple.kt b/compiler/testData/codegen/asmLike/typeAnnotations/simple.kt index 883c8ced461..a0a27a07ca6 100644 --- a/compiler/testData/codegen/asmLike/typeAnnotations/simple.kt +++ b/compiler/testData/codegen/asmLike/typeAnnotations/simple.kt @@ -18,7 +18,7 @@ annotation class TypeAnnSource class Kotlin { - private fun foo(s: @TypeAnn("1") @TypeAnnBinary @TypeAnnSource String) { + fun foo(s: @TypeAnn("1") @TypeAnnBinary @TypeAnnSource String) { } fun foo2(): @TypeAnn("2") @TypeAnnBinary @TypeAnnSource String { @@ -29,10 +29,6 @@ class Kotlin { } fun fooArray2(): Array<@TypeAnn("4") @TypeAnnBinary @TypeAnnSource String>? { - { - foo2() - foo("123") - }() return null } diff --git a/compiler/testData/codegen/asmLike/typeAnnotations/simple.txt b/compiler/testData/codegen/asmLike/typeAnnotations/simple.txt index 1eb63b93dd4..beaa704f814 100644 --- a/compiler/testData/codegen/asmLike/typeAnnotations/simple.txt +++ b/compiler/testData/codegen/asmLike/typeAnnotations/simple.txt @@ -1,19 +1,7 @@ -final class foo/Kotlin$fooArray2$1 : kotlin/jvm/internal/Lambda, kotlin/jvm/functions/Function0 { - final foo.Kotlin this$0 - - void (foo.Kotlin p0) - - public java.lang.Object invoke() - - public final void invoke() -} - public final class foo/Kotlin : java/lang/Object { public void () - public final static void access$foo(foo.Kotlin $this, java.lang.String s) - - private final void foo(java.lang.String s) + public final void foo(java.lang.String s) @Lfoo/TypeAnn;([name="1"]) : METHOD_FORMAL_PARAMETER 0, null @Lfoo/TypeAnnBinary;([]) : METHOD_FORMAL_PARAMETER 0, null // invisible @@ -40,4 +28,4 @@ public abstract interface foo/TypeAnnBinary : java/lang/Object, java/lang/annota public abstract interface foo/TypeAnnSource : java/lang/Object, java/lang/annotation/Annotation { -} +} \ No newline at end of file diff --git a/compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.kt b/compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.kt new file mode 100644 index 00000000000..fbc25f158e7 --- /dev/null +++ b/compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.kt @@ -0,0 +1,32 @@ +// KOTLIN_CONFIGURATION_FLAGS: +JVM.EMIT_JVM_TYPE_ANNOTATIONS +// TYPE_ANNOTATIONS +// IGNORE_BACKEND_FIR: JVM_IR +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +package foo + +@Target(AnnotationTarget.TYPE) +annotation class TypeAnn(val name: String) + +@Target(AnnotationTarget.TYPE) +@Retention(AnnotationRetention.BINARY) +annotation class TypeAnnBinary + +@Target(AnnotationTarget.TYPE) +@Retention(AnnotationRetention.SOURCE) +annotation class TypeAnnSource + +class Kotlin { + + private fun foo(s: @TypeAnn("1") @TypeAnnBinary @TypeAnnSource String): @TypeAnn("2") @TypeAnnBinary @TypeAnnSource String { + return "OK" + } + + inner class A { + + fun fooArray2() { + foo("123") + } + } + +} diff --git a/compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.txt b/compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.txt new file mode 100644 index 00000000000..4b7d92b7593 --- /dev/null +++ b/compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.txt @@ -0,0 +1,31 @@ +public final class foo/Kotlin$A : java/lang/Object { + final foo.Kotlin this$0 + + public void (foo.Kotlin $outer) + + public final void fooArray2() +} + +public final class foo/Kotlin : java/lang/Object { + public void () + + public final static java.lang.String access$foo(foo.Kotlin $this, java.lang.String s) + + private final java.lang.String foo(java.lang.String s) + @Lfoo/TypeAnn;([name="2"]) : METHOD_RETURN, null + @Lfoo/TypeAnn;([name="1"]) : METHOD_FORMAL_PARAMETER 0, null + @Lfoo/TypeAnnBinary;([]) : METHOD_RETURN, null // invisible + @Lfoo/TypeAnnBinary;([]) : METHOD_FORMAL_PARAMETER 0, null // invisible +} + +public abstract interface foo/TypeAnn : java/lang/Object, java/lang/annotation/Annotation { + public abstract java.lang.String name() +} + +public abstract interface foo/TypeAnnBinary : java/lang/Object, java/lang/annotation/Annotation { + +} + +public abstract interface foo/TypeAnnSource : java/lang/Object, java/lang/annotation/Annotation { + +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/AsmLikeInstructionListingTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/AsmLikeInstructionListingTestGenerated.java index 1f8c8d975d5..3745900f1b1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/AsmLikeInstructionListingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/AsmLikeInstructionListingTestGenerated.java @@ -193,5 +193,10 @@ public class AsmLikeInstructionListingTestGenerated extends AbstractAsmLikeInstr public void testStaticNested() throws Exception { runTest("compiler/testData/codegen/asmLike/typeAnnotations/staticNested.kt"); } + + @TestMetadata("syntheticAccessors.kt") + public void testSyntheticAccessors() throws Exception { + runTest("compiler/testData/codegen/asmLike/typeAnnotations/syntheticAccessors.kt"); + } } }