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

Do not use compiler hard-coded use-site target #2058

Merged
merged 1 commit into from
Aug 27, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class KSAnnotationResolvedImpl private constructor(
}

override val useSiteTarget: AnnotationUseSiteTarget? by lazy {
// Do not use compiler hard-coded use-site target.
// FIXME: use origin after it is fixed.
if (parent?.origin == Origin.KOTLIN_LIB || parent?.origin == Origin.JAVA_LIB)
return@lazy null

when (annotationApplication.useSiteTarget) {
null -> null
FILE -> AnnotationUseSiteTarget.FILE
Expand All @@ -128,6 +133,7 @@ class KSAnnotationResolvedImpl private constructor(
}
}

// FIXME: use parent.origin
override val origin: Origin = Origin.KOTLIN_LIB

override val location: Location by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class KSTypeReferenceResolvedImpl private constructor(
}

override val element: KSReferenceElement? by lazy {
// FIXME: synthetic elements can have non-synthetic annotations via use-site targets
if (parent == null || parent.origin == Origin.SYNTHETIC) {
null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.google.devtools.ksp.processor
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.getConstructors
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSDeclaration
Expand Down Expand Up @@ -89,6 +90,18 @@ class ImplicitElementProcessor : AbstractTestProcessor() {
)
val ImplictConstructorJava = resolver.getClassDeclarationByName("ImplictConstructorJava")!!
result.add(ImplictConstructorJava.getConstructors().map { it.toString() }.joinToString(","))

listOf("Test", "lib.Test").forEach { clsName ->
resolver.getClassDeclarationByName(clsName)!!.let { cls ->
cls.getDeclaredProperties().single().let { annotated ->
result.add(
"$clsName, $annotated: ${annotated.annotations.toList().map {
"${it.shortName.asString()}: ${ it.useSiteTarget }"
}}"
)
}
}
}
return emptyList()
}
}
43 changes: 42 additions & 1 deletion test-utils/testData/api/implicitElements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,48 @@
// GetAnno
// <init>
// synthetic constructor for ImplictConstructorJava
// Test, p: [MyKotlinAnnotation: null, MyJavaAnnotation: null]
// lib.Test, p: [MyKotlinAnnotation: null, MyJavaAnnotation: null]
// END
// MODULE: lib
// FILE: lib/MyJavaAnnotation.java
package lib;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.MODULE;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({FIELD})
public @interface MyJavaAnnotation {}

// FILE: lib/MyKotlinAnnotation.kt
package lib
@Target(AnnotationTarget.PROPERTY)
annotation class MyKotlinAnnotation

// FILE: lib/Test.kt
package lib
class Test(
@MyKotlinAnnotation
@MyJavaAnnotation
val p: Int
)

// MODULE: main(lib)
// FILE: Test.kt
import lib.*
class Test(
@MyKotlinAnnotation
@MyJavaAnnotation
val p: Int
)
// FILE: a.kt
annotation class GetAnno
annotation class SetAnno
Expand Down Expand Up @@ -66,4 +107,4 @@ public class JavaClass {

public class ImplictConstructorJava {

}
}
Loading