From a3830b26115257bbc81d2f46ba89cd09c43b0d03 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 13 Nov 2020 19:54:30 +0100 Subject: [PATCH] JVM IR: copy corresponding property+annotations for DefaultImpls methods Instead of using methodSignatureMapper which was an obsolete hack to make accessor names "getFoo" instead of "". In particular, this keeps the `@Deprecated` annotation on the corresponding property, which results in ACC_DEPRECATED in codegen. #KT-43326 Fixed --- .../kotlin/backend/jvm/JvmBackendContext.kt | 3 +-- .../kotlin/backend/jvm/JvmCachedDeclarations.kt | 6 ++---- .../jetbrains/kotlin/backend/jvm/ir/IrUtils.kt | 3 ++- .../deprecated/deprecatedProperty.kt | 5 +++++ .../deprecated/deprecatedProperty.txt | 15 +++++++++++++++ 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt index 552557361e4..5fc528ef78e 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt @@ -35,7 +35,6 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrFunctionReference import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.util.IdSignature import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi2ir.PsiErrorBuilder @@ -72,7 +71,7 @@ class JvmBackendContext( val methodSignatureMapper = MethodSignatureMapper(this) internal val innerClassesSupport = JvmInnerClassesSupport(irFactory) - internal val cachedDeclarations = JvmCachedDeclarations(this, methodSignatureMapper, state.languageVersionSettings) + internal val cachedDeclarations = JvmCachedDeclarations(this, state.languageVersionSettings) override val mapping: Mapping = DefaultMapping() diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmCachedDeclarations.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmCachedDeclarations.kt index 2b14698e19f..8c9a7ec941b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmCachedDeclarations.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmCachedDeclarations.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.backend.jvm import org.jetbrains.kotlin.backend.common.ir.copyParameterDeclarationsFrom import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor import org.jetbrains.kotlin.backend.common.ir.createStaticFunctionWithReceivers -import org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface import org.jetbrains.kotlin.backend.jvm.ir.copyCorrespondingPropertyFrom import org.jetbrains.kotlin.backend.jvm.ir.createJvmIrBuilder @@ -34,7 +33,6 @@ import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver class JvmCachedDeclarations( private val context: JvmBackendContext, - private val methodSignatureMapper: MethodSignatureMapper, private val languageVersionSettings: LanguageVersionSettings ) { private val singletonFieldDeclarations = HashMap() @@ -152,9 +150,8 @@ class JvmCachedDeclarations( return defaultImplsMethods.getOrPut(interfaceFun) { val defaultImpls = getDefaultImplsClass(interfaceFun.parentAsClass) - val name = Name.identifier(methodSignatureMapper.mapFunctionName(interfaceFun)) context.irFactory.createStaticFunctionWithReceivers( - defaultImpls, name, interfaceFun, + defaultImpls, interfaceFun.name, interfaceFun, dispatchReceiverType = parent.defaultType, // If `interfaceFun` is not a real implementation, then we're generating stubs in a descendant // interface's DefaultImpls. For example, @@ -189,6 +186,7 @@ class JvmCachedDeclarations( isFakeOverride = false, typeParametersFromContext = parent.typeParameters ).also { + it.copyCorrespondingPropertyFrom(interfaceFun) if (it.origin == JvmLoweredDeclarationOrigin.DEFAULT_IMPLS_BRIDGE_FOR_COMPATIBILITY && !it.annotations.hasAnnotation(DeprecationResolver.JAVA_DEPRECATED) ) { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt index 16b0eeaace4..4cb77019a44 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt @@ -319,11 +319,12 @@ fun IrSimpleFunction.copyCorrespondingPropertyFrom(source: IrSimpleFunction) { val property = source.correspondingPropertySymbol?.owner ?: return val target = this - correspondingPropertySymbol = factory.buildProperty() { + correspondingPropertySymbol = factory.buildProperty { name = property.name updateFrom(property) }.apply { parent = target.parent + annotations = property.annotations when { source.isGetter -> getter = target source.isSetter -> setter = target diff --git a/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.kt b/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.kt index 921f1172114..1167e2ab082 100644 --- a/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.kt +++ b/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.kt @@ -51,3 +51,8 @@ var List.textGenExtVar get() = 1 set(v) {} +interface I { + @Deprecated("") + val T.id: T + get() = this +} diff --git a/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.txt b/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.txt index b9bc3f1e940..739b7115338 100644 --- a/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.txt +++ b/compiler/testData/codegen/bytecodeListing/deprecated/deprecatedProperty.txt @@ -44,3 +44,18 @@ public final class DeprecatedPropertyKt { public deprecated final static method setTextExtVar(@org.jetbrains.annotations.NotNull p0: java.lang.Object, p1: int): void public deprecated final static method setTextGenExtVar(@org.jetbrains.annotations.NotNull p0: java.util.List, p1: int): void } + +@kotlin.Metadata +public final class I$DefaultImpls { + // source: 'deprecatedProperty.kt' + public synthetic deprecated static @kotlin.Deprecated method getId$annotations(p0: java.lang.Object): void + public deprecated static method getId(@org.jetbrains.annotations.NotNull p0: I, p1: java.lang.Object): java.lang.Object + public final inner class I$DefaultImpls +} + +@kotlin.Metadata +public interface I { + // source: 'deprecatedProperty.kt' + public deprecated abstract method getId(p0: java.lang.Object): java.lang.Object + public final inner class I$DefaultImpls +}