Skip to content

Commit

Permalink
copy type annotations when replacing arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Jun 22, 2023
1 parent 1c588f6 commit 4dfe59e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ class ResolverImpl(
val typeSubstitutor = containing.kotlinType.createTypeSubstitutor()
val substituted = declaration.substitute(typeSubstitutor) as? ValueDescriptor
substituted?.let {
return getKSTypeCached(substituted.type)
return getKSTypeCached(substituted.type, annotations = property.type.resolve().annotations)
}
}
// if substitution fails, fallback to the type from the property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ class KSTypeImpl private constructor(

override fun replace(arguments: List<KSTypeArgument>): KSType {
return kotlinType.replaceTypeArguments(arguments)?.let {
getKSTypeCached(it, arguments)
getKSTypeCached(it, arguments, annotations)
} ?: KSErrorType
}

override fun starProjection(): KSType {
return getKSTypeCached(kotlinType.replaceArgumentsWithStarProjections())
return getKSTypeCached(kotlinType.replaceArgumentsWithStarProjections(), annotations = annotations)
}

private val meNullable: KSType by lazy { getKSTypeCached(kotlinType.makeNullable()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ class KSPCompilerPluginTest : AbstractKSPCompilerPluginTest() {
runTest("../test-utils/testData/api/typeAliasComparison.kt")
}

@TestMetadata("typeAnnotation.kt")
@Test
fun testTypeAnnotation() {
runTest("../test-utils/testData/api/typeAnnotation.kt")
}

@TestMetadata("typeComposure.kt")
@Test
fun testTypeComposure() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.google.devtools.ksp.processor

import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated

class TypeAnnotationProcessor : AbstractTestProcessor() {
val result = mutableListOf<String>()

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

override fun process(resolver: Resolver): List<KSAnnotated> {
val myList = resolver.getClassDeclarationByName("MyClass")!!.getDeclaredProperties().single()
val myStringClass = resolver.getClassDeclarationByName("MyStringClass")!!.asStarProjectedType()
result.add(myList.type.resolve().annotations.joinToString())
result.add(myList.asMemberOf(myStringClass).annotations.joinToString())
result.add(myList.type.resolve().let { it.replace(it.arguments) }.annotations.joinToString())
result.add(myList.type.resolve().starProjection().annotations.joinToString())
return emptyList()
}
}
32 changes: 32 additions & 0 deletions test-utils/testData/api/typeAnnotation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2023 Google LLC
* Copyright 2010-2023 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: TypeAnnotationProcessor
// EXPECTED:
// @JvmSuppressWildcards
// @JvmSuppressWildcards
// @JvmSuppressWildcards
// @JvmSuppressWildcards
// END

class MyClass<T> {
var myList: @JvmSuppressWildcards List<Foo> = TODO()
}

class MyStringClass: MyClass<String> {}

0 comments on commit 4dfe59e

Please sign in to comment.