Skip to content

Commit

Permalink
Use abbreviated type for creating reference elements from inherited d…
Browse files Browse the repository at this point in the history
…eclarations.

fixes google#1011
  • Loading branch information
neetopia committed Sep 23, 2022
1 parent 0e5a63d commit 2c92cdb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
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,43 @@
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>)
}

0 comments on commit 2c92cdb

Please sign in to comment.