Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use abbreviated type for creating reference elements from inherited declarations. #1118

Merged
merged 1 commit into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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(
Expand All @@ -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<KSAnnotation> by lazy {
Expand Down
Expand Up @@ -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() {
Expand Down
Expand Up @@ -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
Expand Down
@@ -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<String>()

override fun toResult(): List<String> {
return results
}

override fun process(resolver: Resolver): List<KSAnnotated> {
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()
}
}
36 changes: 36 additions & 0 deletions 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<T> = Map<String, T>
typealias AliasFun<T> = (T) -> Double


interface Sub : Super


interface Super {
val prop: AliasMap<Int>
fun foo(arg: AliasFun<Float>)
}