From bcd221f714abc76336b82c599dcfb9204bf986a1 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 8 Dec 2021 23:06:06 +0100 Subject: [PATCH] Kapt: use descriptorBasedTypeMapping for type mapping Since all anonymous types are approximated to a supertype in kapt, type mapping doesn't require backend information (mapping of anonymous types to class names) and can be performed statically, independently from particular JVM backend internals, via descriptorBasedTypeMapping. #KT-49682 --- .../stubs/ClassFileToSourceStubConverter.kt | 13 +++---- .../kotlin/kapt3/stubs/ErrorTypeCorrector.kt | 8 ++--- .../kotlin/kapt3/stubs/KaptTypeMapper.kt | 34 +++++++++++++++++++ .../testData/converter/defaultPackage.kt | 1 - .../defaultPackageCorrectErrorTypes.kt | 1 - .../testData/converter/fileFacadeJvmName.kt | 1 - .../testData/converter/kt34569.kt | 2 -- .../testData/converter/recentlyNullable.kt | 1 - .../kotlinRunner/DefaultParameterValues.kt | 1 - .../kotlinRunner/ErrorLocationMapping.kt | 2 -- .../testData/kotlinRunner/NestedClasses.kt | 2 -- 11 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KaptTypeMapper.kt 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 34cb7fc0ae3..b320d5c3b88 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 @@ -56,12 +56,8 @@ import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument import org.jetbrains.kotlin.resolve.constants.* import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull -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.descriptorUtil.* import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin -import org.jetbrains.kotlin.resolve.source.PsiSourceElement import org.jetbrains.kotlin.resolve.source.getPsi import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.KotlinType @@ -123,8 +119,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati val bindings: Map get() = mutableBindings - private val typeMapper - get() = kaptContext.generationState.typeMapper + private val typeMapper = KaptTypeMapper val treeMaker = TreeMaker.instance(kaptContext.context) as KaptTreeMaker @@ -510,7 +505,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati val declaration = kaptContext.origins[clazz]?.element as? KtClassOrObject ?: return defaultSuperTypes val declarationDescriptor = kaptContext.bindingContext[BindingContext.CLASS, declaration] ?: return defaultSuperTypes - if (typeMapper.mapType(declarationDescriptor) != Type.getObjectType(clazz.name)) { + if (typeMapper.mapType(declarationDescriptor.defaultType) != Type.getObjectType(clazz.name)) { return defaultSuperTypes } @@ -1494,7 +1489,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati } } - private fun convertKotlinType(type: KotlinType): Type = typeMapper.mapType(type, null, TypeMappingMode.GENERIC_ARGUMENT) + private fun convertKotlinType(type: KotlinType): Type = typeMapper.mapType(type, TypeMappingMode.GENERIC_ARGUMENT) private fun getFileForClass(c: ClassNode): KtFile? = kaptContext.origins[c]?.element?.containingFile as? KtFile diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ErrorTypeCorrector.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ErrorTypeCorrector.kt index 70a48342061..2a31e9e3b2c 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ErrorTypeCorrector.kt +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ErrorTypeCorrector.kt @@ -83,7 +83,7 @@ class ErrorTypeCorrector( private fun convert(type: SimpleType): JCTree.JCExpression { // TODO now the raw Java type is returned. In future we need to properly convert all type parameters - return treeMaker.Type(converter.kaptContext.generationState.typeMapper.mapType(type)) + return treeMaker.Type(KaptTypeMapper.mapType(type)) } private fun convertUserType(type: KtUserType, substitutions: SubstitutionMap): JCTree.JCExpression { @@ -98,16 +98,14 @@ class ErrorTypeCorrector( return convert(actualType, typeAlias.getSubstitutions(type)) } is ClassConstructorDescriptor -> { - val asmType = converter.kaptContext.generationState.typeMapper - .mapType(target.constructedClass.defaultType, null, TypeMappingMode.GENERIC_ARGUMENT) + val asmType = KaptTypeMapper.mapType(target.constructedClass.defaultType, TypeMappingMode.GENERIC_ARGUMENT) baseExpression = converter.treeMaker.Type(asmType) } is ClassDescriptor -> { // We only get here if some type were an error type. In other words, 'type' is either an error type or its argument, // so it's impossible it to be unboxed primitive. - val asmType = converter.kaptContext.generationState.typeMapper - .mapType(target.defaultType, null, TypeMappingMode.GENERIC_ARGUMENT) + val asmType = KaptTypeMapper.mapType(target.defaultType, TypeMappingMode.GENERIC_ARGUMENT) baseExpression = converter.treeMaker.Type(asmType) } diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KaptTypeMapper.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KaptTypeMapper.kt new file mode 100644 index 00000000000..fa36a8b4866 --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KaptTypeMapper.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2021 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.stubs + +import org.jetbrains.kotlin.codegen.signature.AsmTypeFactory +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.load.kotlin.TypeMappingConfiguration +import org.jetbrains.kotlin.load.kotlin.TypeMappingMode +import org.jetbrains.kotlin.load.kotlin.mapType +import org.jetbrains.kotlin.types.CommonSupertypes +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.org.objectweb.asm.Type + +internal object KaptTypeMapper { + private val configuration = object : TypeMappingConfiguration { + override fun commonSupertype(types: Collection): KotlinType = + CommonSupertypes.commonSupertype(types) + + override fun getPredefinedTypeForClass(classDescriptor: ClassDescriptor): Type? = null + + override fun getPredefinedInternalNameForClass(classDescriptor: ClassDescriptor): String? = null + + override fun processErrorType(kotlinType: KotlinType, descriptor: ClassDescriptor) { + } + + override fun preprocessType(kotlinType: KotlinType): KotlinType? = null + } + + fun mapType(type: KotlinType, mode: TypeMappingMode = TypeMappingMode.DEFAULT): Type = + mapType(type, AsmTypeFactory, mode, configuration, null) +} diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackage.kt b/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackage.kt index fd24bb2ff2b..6d3ab608bb6 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackage.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackage.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // STRICT //FILE: test/ClassRefAnnotation.java diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackageCorrectErrorTypes.kt b/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackageCorrectErrorTypes.kt index 1970c9265ed..cf6f0c15663 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackageCorrectErrorTypes.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/defaultPackageCorrectErrorTypes.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // CORRECT_ERROR_TYPES // STRICT diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/fileFacadeJvmName.kt b/plugins/kapt3/kapt3-compiler/testData/converter/fileFacadeJvmName.kt index c86f421db56..91b4daecd05 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/fileFacadeJvmName.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/fileFacadeJvmName.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // WITH_STDLIB @file:JvmName("FacadeName") diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/kt34569.kt b/plugins/kapt3/kapt3-compiler/testData/converter/kt34569.kt index 5cc46fefec0..8adecf4fec6 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/kt34569.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/kt34569.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR - class T : Runnable { @Override public override fun run() {} diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/recentlyNullable.kt b/plugins/kapt3/kapt3-compiler/testData/converter/recentlyNullable.kt index f535d7fc55f..d8b189db4d7 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/recentlyNullable.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/recentlyNullable.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // FILE: androidx/annotation/RecentlyNullable.java package androidx.annotation; diff --git a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/DefaultParameterValues.kt b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/DefaultParameterValues.kt index 73f1d47aecf..b86da2b6e43 100644 --- a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/DefaultParameterValues.kt +++ b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/DefaultParameterValues.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // DUMP_DEFAULT_PARAMETER_VALUES package test diff --git a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/ErrorLocationMapping.kt b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/ErrorLocationMapping.kt index 6390174e10f..ab84d50e947 100644 --- a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/ErrorLocationMapping.kt +++ b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/ErrorLocationMapping.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR - class Subject { diff --git a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/NestedClasses.kt b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/NestedClasses.kt index 2328189bfcc..88ad510b403 100644 --- a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/NestedClasses.kt +++ b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/NestedClasses.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR - package test internal class Simple {