From 88f04ca2adfa08692769bb98ef7835d8c56fc229 Mon Sep 17 00:00:00 2001 From: Jiaxiang Chen Date: Fri, 23 Sep 2022 10:37:55 -0700 Subject: [PATCH] Use abbreviated type for creating reference elements from inherited declarations. fixes #1011 --- .../binary/KSTypeReferenceDescriptorImpl.kt | 6 +- .../ksp/test/KSPCompilerPluginTest.kt | 6 ++ .../devtools/ksp/impl/test/KSPAATest.kt | 7 +++ .../processor/InheritedTypeAliasProcessor.kt | 60 +++++++++++++++++++ test-utils/testData/api/inheritedTypeAlias.kt | 36 +++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 test-utils/src/main/kotlin/com/google/devtools/ksp/processor/InheritedTypeAliasProcessor.kt create mode 100644 test-utils/testData/api/inheritedTypeAlias.kt diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSTypeReferenceDescriptorImpl.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSTypeReferenceDescriptorImpl.kt index 8938077805..b2260cf95b 100644 --- a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSTypeReferenceDescriptorImpl.kt +++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSTypeReferenceDescriptorImpl.kt @@ -32,6 +32,7 @@ import com.google.devtools.ksp.symbol.Origin import com.google.devtools.ksp.symbol.impl.kotlin.IdKeyTriple import com.google.devtools.ksp.symbol.impl.kotlin.getKSTypeCached import org.jetbrains.kotlin.builtins.isSuspendFunctionTypeOrSubtype +import org.jetbrains.kotlin.types.AbbreviatedType import org.jetbrains.kotlin.types.KotlinType class KSTypeReferenceDescriptorImpl private constructor( @@ -46,10 +47,13 @@ class KSTypeReferenceDescriptorImpl private constructor( } } + private fun KotlinType.findAlias(): KotlinType = + (kotlinType as? AbbreviatedType)?.abbreviation ?: this + override val location: Location = NonExistLocation override val element: KSReferenceElement by lazy { - KSClassifierReferenceDescriptorImpl.getCached(kotlinType, origin, this) + KSClassifierReferenceDescriptorImpl.getCached(kotlinType.findAlias(), origin, this) } override val annotations: Sequence by lazy { diff --git a/compiler-plugin/src/test/kotlin/com/google/devtools/ksp/test/KSPCompilerPluginTest.kt b/compiler-plugin/src/test/kotlin/com/google/devtools/ksp/test/KSPCompilerPluginTest.kt index 377005e7d5..1ba0990bca 100644 --- a/compiler-plugin/src/test/kotlin/com/google/devtools/ksp/test/KSPCompilerPluginTest.kt +++ b/compiler-plugin/src/test/kotlin/com/google/devtools/ksp/test/KSPCompilerPluginTest.kt @@ -279,6 +279,12 @@ class KSPCompilerPluginTest : AbstractKSPCompilerPluginTest() { runTest("../test-utils/testData/api/implicitPropertyAccessors.kt") } + @TestMetadata("inheritedTypeAlias.kt") + @Test + fun testInheritedTypeAlias() { + runTest("../test-utils/testData/api/inheritedTypeAlias.kt") + } + @TestMetadata("innerTypes.kt") @Test fun testInnerTypes() { diff --git a/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/impl/test/KSPAATest.kt b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/impl/test/KSPAATest.kt index 528975f4d7..f74c0df9c2 100644 --- a/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/impl/test/KSPAATest.kt +++ b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/impl/test/KSPAATest.kt @@ -307,6 +307,13 @@ class KSPAATest : AbstractKSPAATest() { runTest("../test-utils/testData/api/implicitPropertyAccessors.kt") } + @Disabled + @TestMetadata("inheritedTypeAlias.kt") + @Test + fun testInheritedTypeAlias() { + runTest("../test-utils/testData/api/inheritedTypeAlias.kt") + } + @Disabled @TestMetadata("innerTypes.kt") @Test diff --git a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/InheritedTypeAliasProcessor.kt b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/InheritedTypeAliasProcessor.kt new file mode 100644 index 0000000000..9b1704bf7d --- /dev/null +++ b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/InheritedTypeAliasProcessor.kt @@ -0,0 +1,60 @@ +/* + * Copyright 2022 Google LLC + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.devtools.ksp.processor + +import com.google.devtools.ksp.getClassDeclarationByName +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.symbol.KSAnnotated + +class InheritedTypeAliasProcessor : AbstractTestProcessor() { + val results = mutableListOf() + + override fun toResult(): List { + return results + } + + override fun process(resolver: Resolver): List { + val sub = resolver.getClassDeclarationByName("Sub")!! + val sup = resolver.getClassDeclarationByName("Super")!! + sub.getAllFunctions().single { it.simpleName.asString() == "foo" }.let { func -> + func.parameters.forEach { + it.type.element?.typeArguments?.joinToString(prefix = "sub: ${it.name?.asString()} :") { + it.toString() + }?.let { results.add(it) } + } + } + sub.getAllProperties().forEach { prop -> + prop.type.element?.typeArguments?.joinToString(prefix = "sub: ${prop.simpleName.asString()} :") { + it.toString() + }?.let { results.add(it) } + } + sup.getAllFunctions().single { it.simpleName.asString() == "foo" }.let { func -> + func.parameters.forEach { + it.type.element?.typeArguments?.joinToString(prefix = "super: ${it.name?.asString()} :") { + it.toString() + }?.let { results.add(it) } + } + } + sup.getAllProperties().forEach { prop -> + prop.type.element?.typeArguments?.joinToString(prefix = "super: ${prop.simpleName.asString()} :") { + it.toString() + }?.let { results.add(it) } + } + return emptyList() + } +} diff --git a/test-utils/testData/api/inheritedTypeAlias.kt b/test-utils/testData/api/inheritedTypeAlias.kt new file mode 100644 index 0000000000..9fa622c3b9 --- /dev/null +++ b/test-utils/testData/api/inheritedTypeAlias.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2022 Google LLC + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// TEST PROCESSOR: InheritedTypeAliasProcessor +// EXPECTED: +// sub: arg :INVARIANT Float +// sub: prop :INVARIANT Int +// super: arg :INVARIANT Float +// super: prop :INVARIANT Int +// END + +typealias AliasMap = Map +typealias AliasFun = (T) -> Double + + +interface Sub : Super + + +interface Super { + val prop: AliasMap + fun foo(arg: AliasFun) +}