From 30d45039fb6cd52ef859180449da70998134c76b Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Tue, 24 Oct 2023 11:49:01 +0300 Subject: [PATCH] [decompiler] Fix property and receiver annotations loading for stubs Add annotation loading for property backing field, property delegate and extension receiver to AnnotationLoaderForStubBuilderImpl. Use logic from AnnotationAndConstantLoaderImpl. AnnotationLoaderForStubBuilderImpl is used by KotlinMetadataDecompiler, K2KlibMetadataDecompiler and KotlinJavaScriptMetaFileDecompiler. Stub versions for built-ins (affects metadata and K/N decompilers) and JS are bumped. KTIJ-26761 KTIJ-26961 --- .../AnnotationLoaderForStubBuilderImpl.kt | 32 ++++++++++++++++--- .../AnnotationsOnNullableTypes.kt | 2 -- .../TopLevelMembersKt/TopLevelMembers.kt | 3 -- .../kotlin/psi/stubs/KotlinStubVersions.kt | 4 +-- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/analysis/decompiled/decompiler-to-file-stubs/src/org/jetbrains/kotlin/analysis/decompiler/stub/file/AnnotationLoaderForStubBuilderImpl.kt b/analysis/decompiled/decompiler-to-file-stubs/src/org/jetbrains/kotlin/analysis/decompiler/stub/file/AnnotationLoaderForStubBuilderImpl.kt index 76975d8d7ba..afada744ec0 100644 --- a/analysis/decompiled/decompiler-to-file-stubs/src/org/jetbrains/kotlin/analysis/decompiler/stub/file/AnnotationLoaderForStubBuilderImpl.kt +++ b/analysis/decompiled/decompiler-to-file-stubs/src/org/jetbrains/kotlin/analysis/decompiler/stub/file/AnnotationLoaderForStubBuilderImpl.kt @@ -40,11 +40,19 @@ class AnnotationLoaderForStubBuilderImpl( return annotations.map { loadAnnotation(it, container.nameResolver) } } - override fun loadPropertyBackingFieldAnnotations(container: ProtoContainer, proto: ProtoBuf.Property): List = - emptyList() + override fun loadPropertyBackingFieldAnnotations(container: ProtoContainer, proto: ProtoBuf.Property): List { + val annotations = protocol.propertyBackingFieldAnnotation?.let { proto.getExtension(it) }.orEmpty() + return annotations.map { annotationProto -> + loadAnnotation(annotationProto, container.nameResolver) + } + } - override fun loadPropertyDelegateFieldAnnotations(container: ProtoContainer, proto: ProtoBuf.Property): List = - emptyList() + override fun loadPropertyDelegateFieldAnnotations(container: ProtoContainer, proto: ProtoBuf.Property): List { + val annotations = protocol.propertyDelegatedFieldAnnotation?.let {proto.getExtension(it) }.orEmpty() + return annotations.map { annotationProto -> + loadAnnotation(annotationProto, container.nameResolver) + } + } override fun loadEnumEntryAnnotations(container: ProtoContainer, proto: ProtoBuf.EnumEntry): List = proto.getExtension(protocol.enumEntryAnnotation).orEmpty().map { loadAnnotation(it, container.nameResolver) } @@ -62,7 +70,21 @@ class AnnotationLoaderForStubBuilderImpl( container: ProtoContainer, proto: MessageLite, kind: AnnotatedCallableKind - ): List = emptyList() + ): List { + val annotations = when (proto) { + is ProtoBuf.Function -> protocol.functionExtensionReceiverAnnotation?.let { proto.getExtension(it) } + is ProtoBuf.Property -> when (kind) { + AnnotatedCallableKind.PROPERTY, AnnotatedCallableKind.PROPERTY_GETTER, AnnotatedCallableKind.PROPERTY_SETTER -> { + protocol.propertyExtensionReceiverAnnotation?.let { proto.getExtension(it) } + } + else -> error("Unsupported callable kind with property proto for receiver annotations: $kind") + } + else -> error("Unknown message: $proto") + }.orEmpty() + return annotations.map { annotationProto -> + loadAnnotation(annotationProto, container.nameResolver) + } + } override fun loadTypeAnnotations( proto: ProtoBuf.Type, diff --git a/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/AnnotationsOnNullableTypes/AnnotationsOnNullableTypes.kt b/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/AnnotationsOnNullableTypes/AnnotationsOnNullableTypes.kt index b06464e320f..311a08b5390 100644 --- a/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/AnnotationsOnNullableTypes/AnnotationsOnNullableTypes.kt +++ b/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/AnnotationsOnNullableTypes/AnnotationsOnNullableTypes.kt @@ -1,5 +1,3 @@ -// Issue: KTIJ-26761 -// KNM_K2_IGNORE public class AnnotationsOnNullableTypes { fun B<@A C?>.receiverArgument() {} diff --git a/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/TopLevelMembersKt/TopLevelMembers.kt b/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/TopLevelMembersKt/TopLevelMembers.kt index 557b9a913ba..3b9ae59b162 100644 --- a/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/TopLevelMembersKt/TopLevelMembers.kt +++ b/analysis/decompiled/decompiler-to-file-stubs/testData/clsFileStubBuilder/TopLevelMembersKt/TopLevelMembers.kt @@ -1,6 +1,3 @@ -// Issue: KTIJ-26761 -// KNM_K2_IGNORE - package foo.TopLevelMembers fun funWithBlockBody() { diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt index 0f820b865ae..a4013d17ce6 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt @@ -25,9 +25,9 @@ object KotlinStubVersions { // BuiltIn stub version should be increased if changes are made to builtIn stub building subsystem (org.jetbrains.kotlin.idea.decompiler.builtIns) // Increasing this version will lead to reindexing of all builtIn files (see KotlinBuiltInFileType). - const val BUILTIN_STUB_VERSION = BINARY_STUB_VERSION + 4 + const val BUILTIN_STUB_VERSION = BINARY_STUB_VERSION + 5 // JS stub version should be increased if changes are made to js stub building subsystem (org.jetbrains.kotlin.idea.decompiler.js) // Increasing this version will lead to reindexing of js binary files (see KotlinJavaScriptMetaFileType). - const val JS_STUB_VERSION = BINARY_STUB_VERSION + 3 + const val JS_STUB_VERSION = BINARY_STUB_VERSION + 4 }