https://kotlinlang.org logo
a

Ahmed Mourad

04/18/2021, 3:26 AM
Hi, can someone point out the problem with this function?
Copy code
fun KtClassOrObject.findSuperType(
    bindingContext: BindingContext,
    fqName: FqName
): KtSuperTypeListEntry? {
    return this.superTypeListEntries.firstOrNull { entry ->
        bindingContext.get(
            BindingContext.TYPE,
            entry.typeReference
        )?.constructor?.declarationDescriptor?.fqNameSafe == fqName
    }
}
It's called inside an AnalysisHandler's
analysisCompleted
. The problem is that
bindingContext#get
always returns null, despite
entry.typeReference
not being null, but why?
I'm sure this should work, it looks like it might be a Kotlin bug to me .. I'm using Kotlin 1.4.32
r

raulraja

04/18/2021, 9:27 AM
Hi @Ahmed Mourad, If this happens in the first analysis cycle those symbols are not bound yet in the binding context. To do a second pass in which the binding context has the info and association between ktElements and analysis descriptors you have to return
RetryWithAdditionalRoots
in the first analysis result in order to rewind and do a second pass over analysis.
not sure if that is the reason but that is a common error when trying to access bound symbols while in analysis, your code may work depending on which phase step is being called.
a

Ahmed Mourad

04/19/2021, 4:04 AM
Hey @raulraja! I've been through multiple implementations of the handler, a problem with them was frequently or sometimes always getting a
GC Overhead limit exceeded
failure. Looking at different implementations on Github I came across this reoccurring pattern of keeping a local
didRecompile
flag which seems to ensure retrying is only triggered once: https://github.com/square/anvil/blob/9f05fa9714cdb238bb81960045d81b2e1d12e9bb/comp[…]/com/squareup/anvil/compiler/codegen/CodeGenerationExtension.kt https://github.com/ShikaSD/kotlin-compiler-plugin-retry-with-additional-roots/blob/master/compiler-plugin/src/main/kotlin/Plugin.kt This seems to be the reason for the given problem, but I'm now unsure what a prober
AnalysisHandlerExtension
should look like.
r

raulraja

04/19/2021, 1:11 PM
Not sure if it’ll help but this is how we encoded it in Arrow Meta for typed quotes which couples KtElements in the quotes phase to the resolved descriptors and rewinds analysis. https://github.com/arrow-kt/arrow-meta/blob/7f83adf7726d43fba4a4cbeedd70289f3b19266e/compiler-plugin/src/main/kotlin/arrow/meta/quotes/TypedQuote.kt
7 Views