From c5f44486a94a61bd32b246c084d68625e8d61330 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 9 Jun 2023 00:44:13 +0200 Subject: [PATCH] Rename KaptAnonymousTypeTransformer and move to frontend.java This is needed in order to run light analysis mode tests for JVM IR backend. In the subsequent commit, this extension is added to light analysis mode tests. Kapt stub generation uses this extension to transform local types to non-local: private val x = object {} With this extension, x's type will be `Any`. Without it, it will be an anonymous type. This anonymous type was not a problem for the old JVM backend, but it's difficult to translate it in the IR infrastructure in the light analysis mode where bodies are not resolved. When kapt stub generation works with JVM IR enabled, KaptAnonymousTypeTransformer ensured that backend would not crash and stubs would contain something useful. However, this is not happening in light analysis mode tests, which are supposed to check how compiler behaves in the light analysis mode which is used in kapt. --- ...ceWithSupertypeAnonymousTypeTransformer.kt | 13 +++++++-- .../org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt | 16 +++++++--- .../kapt3/KaptAnonymousTypeTransformer.kt | 29 ------------------- .../stubs/ClassFileToSourceStubConverter.kt | 1 + 4 files changed, 24 insertions(+), 35 deletions(-) rename plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/util/typeUtils.kt => compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/ReplaceWithSupertypeAnonymousTypeTransformer.kt (76%) delete mode 100644 plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/KaptAnonymousTypeTransformer.kt diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/util/typeUtils.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/ReplaceWithSupertypeAnonymousTypeTransformer.kt similarity index 76% rename from plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/util/typeUtils.kt rename to compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/ReplaceWithSupertypeAnonymousTypeTransformer.kt index d0af6ec3930..cecf41860ea 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/util/typeUtils.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/ReplaceWithSupertypeAnonymousTypeTransformer.kt @@ -1,18 +1,27 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 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.kapt3.util +package org.jetbrains.kotlin.resolve.jvm import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility +import org.jetbrains.kotlin.resolve.DeclarationSignatureAnonymousTypeTransformer import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeProjectionImpl import org.jetbrains.kotlin.types.replace import org.jetbrains.kotlin.types.typeUtil.builtIns +open class ReplaceWithSupertypeAnonymousTypeTransformer : DeclarationSignatureAnonymousTypeTransformer { + override fun transformAnonymousType(descriptor: DeclarationDescriptorWithVisibility, type: KotlinType): KotlinType? = + if (!DescriptorUtils.isLocal(descriptor)) + replaceAnonymousTypeWithSuperType(type) + else type +} + fun replaceAnonymousTypeWithSuperType(type: KotlinType): KotlinType { val declaration = type.constructor.declarationDescriptor as? ClassDescriptor ?: return type diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt index b78fc3638fa..e816c8ed4e9 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt @@ -35,21 +35,24 @@ import org.jetbrains.kotlin.container.ComponentProvider import org.jetbrains.kotlin.container.StorageComponentContainer import org.jetbrains.kotlin.container.useInstance import org.jetbrains.kotlin.context.ProjectContext +import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor import org.jetbrains.kotlin.kapt.cli.KaptCliOption -import org.jetbrains.kotlin.kapt.cli.KaptCliOption.Companion.ANNOTATION_PROCESSING_COMPILER_PLUGIN_ID import org.jetbrains.kotlin.kapt.cli.KaptCliOption.* +import org.jetbrains.kotlin.kapt.cli.KaptCliOption.Companion.ANNOTATION_PROCESSING_COMPILER_PLUGIN_ID import org.jetbrains.kotlin.kapt3.base.Kapt import org.jetbrains.kotlin.kapt3.base.util.KaptLogger import org.jetbrains.kotlin.kapt3.util.MessageCollectorBackedKaptLogger import org.jetbrains.kotlin.kapt3.util.doOpenInternalPackagesIfRequired -import org.jetbrains.kotlin.psi.KtFile -import org.jetbrains.kotlin.resolve.BindingTrace import org.jetbrains.kotlin.platform.TargetPlatform import org.jetbrains.kotlin.platform.jvm.isJvm +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.BindingTrace +import org.jetbrains.kotlin.resolve.jvm.ReplaceWithSupertypeAnonymousTypeTransformer import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension import org.jetbrains.kotlin.resolve.jvm.extensions.PartialAnalysisHandlerExtension +import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.utils.decodePluginOptions import java.io.ByteArrayInputStream import java.io.File @@ -261,7 +264,12 @@ class Kapt3ComponentRegistrar : ComponentRegistrar { moduleDescriptor: ModuleDescriptor ) { if (!platform.isJvm()) return - container.useInstance(KaptAnonymousTypeTransformer(analysisExtension)) + container.useInstance(object : ReplaceWithSupertypeAnonymousTypeTransformer() { + override fun transformAnonymousType(descriptor: DeclarationDescriptorWithVisibility, type: KotlinType): KotlinType? { + if (!analysisExtension.analyzePartially) return null + return super.transformAnonymousType(descriptor, type) + } + }) } } diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/KaptAnonymousTypeTransformer.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/KaptAnonymousTypeTransformer.kt deleted file mode 100644 index 91140fbc707..00000000000 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/KaptAnonymousTypeTransformer.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2010-2018 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.kapt3 - -import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility -import org.jetbrains.kotlin.kapt3.util.replaceAnonymousTypeWithSuperType -import org.jetbrains.kotlin.resolve.DeclarationSignatureAnonymousTypeTransformer -import org.jetbrains.kotlin.resolve.DescriptorUtils.isLocal -import org.jetbrains.kotlin.resolve.jvm.extensions.PartialAnalysisHandlerExtension -import org.jetbrains.kotlin.types.KotlinType - -class KaptAnonymousTypeTransformer( - private val analysisExtension: PartialAnalysisHandlerExtension -) : DeclarationSignatureAnonymousTypeTransformer { - override fun transformAnonymousType(descriptor: DeclarationDescriptorWithVisibility, type: KotlinType): KotlinType? { - if (!analysisExtension.analyzePartially) { - return null - } - - if (isLocal(descriptor)) { - return type - } - - return replaceAnonymousTypeWithSuperType(type) - } -} \ No newline at end of file diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt index 99c6d7a8b01..14aaf57d389 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt @@ -65,6 +65,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin +import org.jetbrains.kotlin.resolve.jvm.replaceAnonymousTypeWithSuperType import org.jetbrains.kotlin.resolve.source.getPsi import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.error.ErrorTypeKind