diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSTypeImpl.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSTypeImpl.kt index 4a9aef7c76..bbb41ddda8 100644 --- a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSTypeImpl.kt +++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSTypeImpl.kt @@ -25,6 +25,7 @@ import com.google.devtools.ksp.symbol.KSType import com.google.devtools.ksp.symbol.KSTypeArgument import com.google.devtools.ksp.symbol.Nullability import com.google.devtools.ksp.symbol.Origin +import com.google.devtools.ksp.symbol.impl.binary.KSAnnotationDescriptorImpl import com.google.devtools.ksp.symbol.impl.binary.KSTypeArgumentDescriptorImpl import com.google.devtools.ksp.symbol.impl.convertKotlinType import com.google.devtools.ksp.symbol.impl.replaceTypeArguments @@ -156,6 +157,10 @@ fun getKSTypeCached( ) { KSErrorType } else { - KSTypeImpl.getCached(kotlinType, ksTypeArguments, annotations) + KSTypeImpl.getCached( + kotlinType, + ksTypeArguments, + kotlinType.annotations.map { KSAnnotationDescriptorImpl.getCached(it, null) }.asSequence() + ) } } 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 58ebfee61b..8ef81a1f2f 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 @@ -224,6 +224,12 @@ class KSPCompilerPluginTest : AbstractKSPCompilerPluginTest() { runTest("testData/api/functionTypeAlias.kt") } + @TestMetadata("functionTypeAnnotation.kt") + @Test + fun testFunctionTypeAnnotation() { + runTest("testData/api/functionTypeAnnotation.kt") + } + @TestMetadata("functionTypes.kt") @Test fun testFunctionTypes() { diff --git a/compiler-plugin/testData/api/functionTypeAnnotation.kt b/compiler-plugin/testData/api/functionTypeAnnotation.kt new file mode 100644 index 0000000000..900a700d93 --- /dev/null +++ b/compiler-plugin/testData/api/functionTypeAnnotation.kt @@ -0,0 +1,33 @@ +/* + * 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. + */ + + +// WITH_RUNTIME +// TEST PROCESSOR: FunctionTypeAnnotationProcessor +// EXPECTED: +// strExt0: Function1 @ExtensionFunctionType +// strExt1: Function2 @ExtensionFunctionType +// strWithAnnoExt0: Function1 @A, @ExtensionFunctionType +// strWithAnnoExt1: Function2 @A, @ExtensionFunctionType +// END +// FILE: a.kt +annotation class A + +val strExt0: String.() -> Unit = TODO() +val strExt1: String.(Int) -> Unit = TODO() +val strWithAnnoExt0: @A String.() -> Unit = TODO() +val strWithAnnoExt1: @A String.(@A Int) -> Unit = TODO() 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 7f79611a83..f5e4c2c8ea 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 @@ -240,6 +240,13 @@ class KSPAATest : AbstractKSPAATest() { runTest("../compiler-plugin/testData/api/functionTypeAlias.kt") } + @Disabled + @TestMetadata("functionTypeAnnotation.kt") + @Test + fun testFunctionTypeAnnotation() { + runTest("testData/api/functionTypeAnnotation.kt") + } + @Disabled @TestMetadata("functionTypes.kt") @Test diff --git a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/FunctionTypeAnnotationProcessor.kt b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/FunctionTypeAnnotationProcessor.kt new file mode 100644 index 0000000000..c35d156e3f --- /dev/null +++ b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/FunctionTypeAnnotationProcessor.kt @@ -0,0 +1,36 @@ +package com.google.devtools.ksp.processor + +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSNode +import com.google.devtools.ksp.symbol.KSPropertyDeclaration +import com.google.devtools.ksp.visitor.KSTopDownVisitor + +class FunctionTypeAnnotationProcessor : AbstractTestProcessor() { + val results = mutableListOf() + + override fun toResult(): List { + return results + } + + override fun process(resolver: Resolver): List { + val files = resolver.getNewFiles() + + files.forEach { + it.accept( + object : KSTopDownVisitor() { + override fun defaultHandler(node: KSNode, data: Unit) = Unit + + override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: Unit) { + val type = property.type.resolve() + val propertyName = property.simpleName.asString() + val typeName = type.declaration.simpleName.asString() + results.add("$propertyName: $typeName ${type.annotations.joinToString { it.toString() }}") + } + }, + Unit + ) + } + return emptyList() + } +} diff --git a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/FunctionTypeProcessor.kt b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/FunctionTypeProcessor.kt index 4e5f4e07d0..f361599b5a 100644 --- a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/FunctionTypeProcessor.kt +++ b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/FunctionTypeProcessor.kt @@ -23,12 +23,10 @@ import com.google.devtools.ksp.visitor.KSTopDownVisitor open class FunctionTypeProcessor : AbstractTestProcessor() { val results = mutableListOf() - val typeCollector = TypeCollector() val types = mutableSetOf() override fun process(resolver: Resolver): List { val files = resolver.getNewFiles() - val ignoredNames = mutableSetOf() files.forEach { it.accept(