Hi, here is the latest KSP <release>. Please note ...
# ksp
j
Hi, here is the latest KSP release. Please note that we are pretty close to beta stage. In beta stage we want to avoid making API changes, if you have any changes you want to see in terms APIs, now is still a good time. This release is a pretty long one, please refer to the release note on Github for full list of changes in this release. Major breaking changes: • switch to 
Sequence
 for a bunch of APIs ◦ Some of the API return type has been changed from 
Collection
 or 
List
 to 
Sequence
, which will allow performance improvement if you use case involved early termination. ◦ Note that because 
Sequence
 is lazily evaluated, it requires a terminal operation to make actual compute happen. Please check you uses around the impacted APIs to see if they have a terminal operation. Most common use case is 
Collection.map{}
 which will iterate through the collection by itself, but is non-terminal for 
Sequence
 and should be companied with 
toList()
 or replaced with 
forEach
◦ For the whole list of APIs impacted by this change, please refer to this commit. • Origin for KS symbols from binaries is now split into 
KOTLIN_LIB
 and 
JAVA_LIB
.
👍 5
🎉 2
👍🏼 1
y
One api i have on my mind if the ability to check if a property has a backing field. I've been meaning to add it but didn't have much time these days
👍 1
j
I can add an issue for that
y
i started working on this
so this turned out a lot more complicated then a function call to an existing descriptor API 😕 https://github.com/yigit/ksp/commit/9f7d348a76fc6e2ddae8695ecfddff6a0e50cb0d resolveUtil in compioler has a weird logic with a TODO. https://github.com/JetBrains/kotlin/blame/master/compiler/frontend/src/org/jetbrains/kotlin/resolve/resolveUtil.kt Then there are workarounds i found in the source code but they are still not detecting properties w/o backing fields properly when the property is in a descriptor. https://github.com/yigit/ksp/commit/9f7d348a76fc6e2ddae8695ecfddff6a0e50cb0d#diff-675bc4d99ebd3438e286a65973c3b[…]31d6ad272d42b3181a916a0655c347R431 will need to debug further.
t
In
resolveUtil.kt
,
getter != null -> false
looks weird to me; A backing field can be used in a getter, right?
for example,
Copy code
fun foo() = 1
val x: Int = foo()
    get() = field
fails the
hasBackingField
For source code, I think a property has a backing field if and only if it has a initializer. For library,
DeserializedPropertyDescriptor
is created with backingField always set; The information is lost.
OTOH, in Kotlin, a backing field is kind of private implementation of a property. Accessing it outside of accessors might be a little bit weird.
y
For room, we need to know to decide whether that property should be saved into a database column or not. While searching usages of the internal api on kotline source code, i noticed kotline serialization does the same
maybe it works good enough for serialization of they only support src code and not binary dependencies
t
I'm trying to see if there's some way to infer the backing field but so far haven't found anything
It basically tries to see if there is a field with the same name to the property in the binary class.
y
That seems feasible to me. Just a minor change is needed to get the jvm name i guess but i can take it from your patch. Meanwhile, we should probably double check with JB on why that method does not really work
👍 1
ok that passes the test. i'll try to add more with jvm name, overrides etc
oh nice, JVM name doess not affect because the class visitor always gives kotlin names 🙂 i'll still keep the test cases
oh that is because i was using JvmField not jvm name 🤦‍♂️ also, jvm name is not applicable to fields it is only applicable to methods https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-name/
t
Not having to consider jvm name sounds a great relief :-) As for override / inheritance, since the API is on KSPropertyDeclaration, which is always the place where it is defined, we probably don't need to check overridden property?
y
thats what i'm trying to figure out but the API in kotlin compiler looks for overrides, though only when it is fake:
Copy code
fun PropertyDescriptor.hasBackingField(bindingContext: BindingContext?): Boolean = when {
    kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE -> overriddenDescriptors.any { it.hasBackingField(bindingContext) }
t
That doesn't seem to applicable in our case, or we have something to fix in getAllProperties
y
yea i think we don't return them
👍 1
sent an initial PR. One test case i could create is to create a java method pair that looks like a property. For some reason, it does not show up when traversing properties https://github.com/google/ksp/pull/449/files#diff-f8c01fc500f3df4198742e0508c930581723f430d30bb308aed2316a6c3c67f0R162 Need to check, i forgot if KSP converts them to properties or not