google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.9k stars 277 forks source link

[KSP2] KSClassDeclaration.getAllFunctions() doesn't return functions in the order they're declared #2135

Open BoD opened 1 month ago

BoD commented 1 month ago

With 2.0.20-1.0.25.

Given a class like this:

class MyClass(private val c: Int) {
  fun a(){}
  fun b(){}
  fun c(){}
}

KSClassDeclaration.getAllFunctions() will return c, a, b, instead of a, b, c.

Removing or renaming the c val makes the order correct again.

This only happens with ksp.useKSP2=true.

Repro steps

git clone https://github.com/BoD/repro-ksp-getAllFunctions-order
cd repro-ksp-getAllFunctions-order
./gradlew build

Expected: w: [ksp] all functions:[a, b, c, equals, hashCode, toString, <init>]

Actual: w: [ksp] all functions:[c, a, b, equals, hashCode, toString, <init>]

ting-yuan commented 1 month ago

Have you tried Resolver.getDeclarationsInSourceOrder?

BoD commented 1 month ago

This seemed promising, but surprisingly, I still have the wrong order with getDeclarationsInSourceOrder. I've updated my repro project here