google / google/ksp

Resolver#getDeclarationsInSourceOrder() gives incorrect order for nested annotation's values.

Open
#2,617 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
3.5k
Forks
415
Avg merge
1d 2h
Merged PRs (30d)
53

Description

Consider a class annotated with two annotations that have the exact same values except one is nested:

```kotlin
@MyAnnotation(a = "a", b = "b", c = "c")
@MyObject.MyNestedAnnotation(a = "a", b = "b", c = "c")
class MyClass {}

annotation class MyAnnotation(
val a: String,
val b: String,
val c: String,
)

object MyObject {
annotation class MyNestedAnnotation(
val a: String,
val b: String,
val c: String,
)
}
```

And a KSP processor that outputs the enclosed declarations of the annotations with and without `Resolver#getDeclarationsInSourceOrder()`:

```kotlin
override fun process(resolver: Resolver): List {
fun printAnnotationInfo(name: String, inSourceOrder: Boolean) {
val declaration = resolver.getClassDeclarationByName(resolver.getKSNameFromString(name))!!
val declarations =
if (inSourceOrder) {
resolver.getDeclarationsInSourceOrder(declaration)
} else {
declaration.declarations
}.map { it.simpleName.asString() }.toList()

println("${declaration.simpleName.asString()} (inSourceOrder=$inSourceOrder): $declarations")
}

printAnnotationInfo("MyAnnotation", inSourceOrder = false)
printAnnotationInfo("MyAnnotation", inSourceOrder = true)
printAnnotationInfo("MyObject.MyNestedAnnotation", inSourceOrder = false)
printAnnotationInfo("MyObject.MyNestedAnnotation", inSourceOrder = true)
return listOf()
}
```

Which gives the following output:

```
MyAnnotation (inSourceOrder=false): [a, b, c, ]
MyAnnotation (inSourceOrder=true): [a, b, c, ]
MyNestedAnnotation (inSourceOrder=false): [a, b, c, ]
MyNestedAnnotation (inSourceOrder=true): [b, a, c, ]
```

Notice that `MyNestedAnnotation (inSourceOrder=true)` gives the incorrect result.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the provided nested-annotation example with a KSP processor and compare Resolver#getDeclarationsInSourceOrder() with declaration.declarations. Trace the implementation of that Resolver method and its handling of nested declarations; done means the nested annotation's values are returned in source order, matching the non-nested annotation output.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.