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

FIx findOverridee for properties, fixes #174 #176

Merged
merged 1 commit into from
Nov 30, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,30 @@ interface KSPropertyDeclaration : KSDeclaration {
fun isDelegated(): Boolean

/**
* Find the original overridee of this property, if overriding.
* @return [KSPropertyDeclaration] for the original property, if overriding, otherwise null.
* Find the closest overridee of this property, if overriding.
*
* For the following input:
* ```
* abstract class A {
* open val x:Int
* open val y:Int
* }
* abstract class B : A() {
* override val x:Int
* }
* abstract class C : B() {
* override val x:Int
* override val y:Int
* }
* ```
* Calling `findOverridee` on `C.x` will return `B.x`.
* Calling `findOverridee` on `C.y` will return `A.y`.
*
* When there are multiple super classes / interfaces with the property, the closest declaration
* to the current containing declaration is selected. If they are in the same level, the
* property of the first specified interface (in source) will be returned.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: specified super class or interface instead of specified interface

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overriding for properties usually get complicated when it involves Java, can you add a test case where Java code is overriding a property declared in Kotlin code? See this for example.

the findOverridee in the java implementation of property is not implemented (just returns null). I'm guessing that is because java fields do not override parents.

You mean there is a base kotlin class with val x:T and java overrides it with getX():T?
I'll give it a try but that makes me think, that would mean the findOverridee of KSFunction would need to return a KSPropertyAccessor that does not implement KSFunction :/.
I'll see what happens there.

There was also some work discussed before where KSP right now does not properly resolve property accessors in java source code. If we do, then we should also implement findOverridee in KSPropertyDeclaration.

Copy link
Collaborator Author

@yigit yigit Nov 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually trying to reproduce that hits this issue: (same exception)
#94

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm actually able to workaround that issue by making KSJavaFileImpl use descriptors for the class instead of Java impls.
Java impls are causing lots of inconsistencies hence created this issue:
#177

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this so fast.

Question: is the merge of this PR blocked by the progress on #177? Or can this be merged already? It would help me a lot: until this is fixed, I need to maintain my fork. I use Kotlin only. :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neetopia wdyt? I think this one is fine to merge as it should be an improvement over what we have (fixes kotlin properties, java is still broken).
#177 probably won't happen or if it happens it will take a long time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll merge this one.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the merge. Is it possible to create a release that contains this? I am using KSP as a dependency.
If not, what is a likely date for a new release, given your release schedule?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do a release this Friday PST.

*
* @return [KSPropertyDeclaration] for the overridden property, if overriding, otherwise null.
* Calling [findOverridee] is expensive and should be avoided if possible.
*/
fun findOverridee(): KSPropertyDeclaration?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration

@Suppress("unused") // used by tests
class OverrideeProcessor: AbstractTestProcessor() {
Expand Down Expand Up @@ -51,6 +52,12 @@ class OverrideeProcessor: AbstractTestProcessor() {
}

private fun logClass(subject: KSClassDeclaration) {
subject.declarations.filterIsInstance<KSPropertyDeclaration>()
.forEach {
val signature = it.toSignature()
val overrideeSignature = it.findOverridee()?.toSignature()
results.add("$signature -> $overrideeSignature")
}
subject.declarations.filterIsInstance<KSFunctionDeclaration>()
.filterNot { it.simpleName.asString() in IGNORED_METHOD_NAMES }
.forEach {
Expand All @@ -74,6 +81,15 @@ class OverrideeProcessor: AbstractTestProcessor() {
}
}

private fun KSPropertyDeclaration.toSignature(): String {
val self = this
return buildString {
append(self.closestClassDeclaration()?.simpleName?.asString())
append(".")
append(self.simpleName.asString())
}
}

companion object {
// ignore these methods as we receive syntetics of it from compiled code
private val IGNORED_METHOD_NAMES = listOf("equals", "hashCode", "toString")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class KSPropertyDeclarationDescriptorImpl private constructor(val descriptor: Pr
}

override fun findOverridee(): KSPropertyDeclaration? {
return ResolverImpl.instance.resolvePropertyDeclaration(this)?.original?.overriddenDescriptors?.single { it.overriddenDescriptors.isEmpty() }
?.toKSPropertyDeclaration()
val propertyDescriptor = ResolverImpl.instance.resolvePropertyDeclaration(this)
return propertyDescriptor?.findClosestOverridee()?.toKSPropertyDeclaration()
}

override fun isDelegated(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class KSPropertyDeclarationImpl private constructor(val ktProperty: KtProperty)
override fun isDelegated(): Boolean = ktProperty.hasDelegate()

override fun findOverridee(): KSPropertyDeclaration? {
return ResolverImpl.instance.resolvePropertyDeclaration(this)?.original?.overriddenDescriptors?.single { it.overriddenDescriptors.isEmpty() }
?.toKSPropertyDeclaration()
val propertyDescriptor = ResolverImpl.instance.resolvePropertyDeclaration(this)
return propertyDescriptor?.findClosestOverridee()?.toKSPropertyDeclaration()
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ internal fun DeclarationDescriptor.findPsi(): PsiElement? {
}

/**
* @see KSFunctionDeclaration.findOverridee for docs.
* @see KSFunctionDeclaration.findOverridee / [KSPropertyDeclaration.findOverridee] for docs.
*/
internal fun FunctionDescriptor.findClosestOverridee(): FunctionDescriptor? {
internal inline fun <reified T : CallableMemberDescriptor> T.findClosestOverridee(): T? {
// When there is an intermediate class between the overridden and our function, we might receive
// a FAKE_OVERRIDE function which is not desired as we are trying to find the actual
// declared method.
Expand All @@ -296,16 +296,16 @@ internal fun FunctionDescriptor.findClosestOverridee(): FunctionDescriptor? {
// class / interface method OR in case of equal distance (e.g. diamon dinheritance), pick the
// one declared first in the code.

val queue = ArrayDeque<FunctionDescriptor>()
val queue = ArrayDeque<T>()
queue.add(this)

while (queue.isNotEmpty()) {
val current = queue.removeFirst()
val overriddenDescriptors = current.original.overriddenDescriptors
val overriddenDescriptors: Collection<T> = current.original.overriddenDescriptors.filterIsInstance<T>()
overriddenDescriptors.firstOrNull {
it.kind != CallableMemberDescriptor.Kind.FAKE_OVERRIDE
}?.let {
return it.original
return it.original as T?
}
// if all methods are fake, add them to the queue
queue.addAll(overriddenDescriptors)
Expand Down
44 changes: 44 additions & 0 deletions compiler-plugin/testData/api/overridee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
// EXPECTED:
// Subject:
// Companion.companionMethod() -> null
// Subject.notOverridingProp -> null
// Subject.overriddenBaseProp -> Base.overriddenBaseProp
// Subject.overriddenAbstractBaseProp -> Base.overriddenAbstractBaseProp
// Subject.openGrandBaseProp -> GrandBase.openGrandBaseProp
// Subject.abstractGrandBaseProp -> GrandBase.abstractGrandBaseProp
// Subject.overriddenGrandBaseProp -> Base.overriddenGrandBaseProp
// Subject.overriddenAbstractGrandBaseProp -> Base.overriddenAbstractGrandBaseProp
// Subject.openFun() -> Base.openFun()
// Subject.abstractFun() -> Base.abstractFun()
// Subject.openFunWithGenericArg(t:String) -> Base.openFunWithGenericArg(t:T)
Expand All @@ -42,6 +49,13 @@
// Subject.staticMethod() -> null
// lib.Subject:
// Companion.companionMethod() -> null
// Subject.abstractGrandBaseProp -> GrandBase.abstractGrandBaseProp
// Subject.notOverridingProp -> null
// Subject.openGrandBaseProp -> GrandBase.openGrandBaseProp
// Subject.overriddenAbstractBaseProp -> Base.overriddenAbstractBaseProp
// Subject.overriddenAbstractGrandBaseProp -> Base.overriddenAbstractGrandBaseProp
// Subject.overriddenBaseProp -> Base.overriddenBaseProp
// Subject.overriddenGrandBaseProp -> Base.overriddenGrandBaseProp
// Subject.abstractFun() -> Base.abstractFun()
// Subject.abstractFunWithGenericArg(t:String) -> Base.abstractFunWithGenericArg(t:T)
// Subject.abstractGrandBaseFun() -> GrandBase.abstractGrandBaseFun()
Expand All @@ -68,12 +82,20 @@
// FILE: lib.kt
package lib;
abstract class GrandBase {
open var openGrandBaseProp: Int = 0
abstract var abstractGrandBaseProp: Int
open var overriddenGrandBaseProp: Int = 0
abstract var overriddenAbstractGrandBaseProp: Int
open fun openGrandBaseFun() {}
abstract fun abstractGrandBaseFun()
open fun overriddenGrandBaseFun() {}
abstract fun overriddenAbstractGrandBaseFun()
}
abstract class Base<T> : GrandBase() {
open var overriddenBaseProp: Int = 0
abstract var overriddenAbstractBaseProp: Int
override var overriddenGrandBaseProp:Int = 0
override var overriddenAbstractGrandBaseProp: Int = 0
open fun openFun() {}
abstract fun abstractFun():Unit
open fun openFunWithGenericArg(t:T):T = TODO()
Expand All @@ -83,6 +105,13 @@ abstract class Base<T> : GrandBase() {
}

abstract class Subject: Base<String>() {
var notOverridingProp: Int = 0
override open var overriddenBaseProp: Int = 0
override var overriddenAbstractBaseProp: Int = 0
override open var openGrandBaseProp: Int = 0
override var abstractGrandBaseProp: Int = 0
override var overriddenGrandBaseProp:Int = 0
override var overriddenAbstractGrandBaseProp: Int = 0
override fun openFun() {}
override fun abstractFun() {}
override fun openFunWithGenericArg(t:String):String = TODO()
Expand All @@ -99,12 +128,20 @@ abstract class Subject: Base<String>() {
// MODULE: main(lib)
// FILE: a.kt
abstract class GrandBase {
open var openGrandBaseProp: Int = 0
abstract var abstractGrandBaseProp: Int = 0
open var overriddenGrandBaseProp: Int = 0
abstract var overriddenAbstractGrandBaseProp: Int = 0
open fun openGrandBaseFun() {}
abstract fun abstractGrandBaseFun()
open fun overriddenGrandBaseFun() {}
abstract fun overriddenAbstractGrandBaseFun()
}
abstract class Base<T> : GrandBase() {
open var overriddenBaseProp: Int = 0
var overriddenAbstractBaseProp: Int = 0
override var overriddenGrandBaseProp:Int = 0
override var overriddenAbstractGrandBaseProp: Int = 0
open fun openFun() {}
abstract fun abstractFun():Unit
open fun openFunWithGenericArg(t:T):T = TODO()
Expand All @@ -114,6 +151,13 @@ abstract class Base<T> : GrandBase() {
}

abstract class Subject: Base<String>() {
var notOverridingProp: Int = 0
override open var overriddenBaseProp: Int = 0
override var overriddenAbstractBaseProp: Int = 0
override open var openGrandBaseProp: Int = 0
override var abstractGrandBaseProp: Int = 0
override var overriddenGrandBaseProp:Int = 0
override var overriddenAbstractGrandBaseProp: Int = 0
override fun openFun() {}
override fun abstractFun() {}
override fun openFunWithGenericArg(t:String):String = TODO()
Expand Down