Hi, I have a `KtTypeReference` for `kotlin.Unit` a...
# compiler
r
Hi, I have a
KtTypeReference
for
kotlin.Unit
and it could be any other fqName for a type and I wish to resolve the type for. This is happening inside a function declared inside a class where I’m using the class member declaration scope to resolve the types. Currently I’m using:
Copy code
val returnType = typeResolver.resolveType(containingDeclaration.scopeForMemberDeclarationResolution, function.typeReference!!, trace, true)
but this returns:
Copy code
[ERROR : kotlin.Unit]
Is there a better way to go from
KtTypeReference -> KotlinType
than using the type resolver? Is there a different type of scope I should be constructing to resolve the type? Any help is appreciated, thanks.
s
There is a slice in
BindingContext
for
TYPE
, but it probably not populated before resolution. I am usually resolving the whole function using
ResolveSession.resolveDescriptor
and using it
returnType
. Not sure about efficiency of this, but it works most of the time 🙂
r
thanks I’ll try that. The
BindingContext.TYPE
is not populated yet so I need to resolve it, but I will try with the resolve session and report back
With the
ResolveSession
I get:
Copy code
org.jetbrains.kotlin.resolve.lazy.NoDescriptorForDeclarationException: Descriptor wasn't found for declaration CLASS
	at org.jetbrains.kotlin.idea.project.IdeaAbsentDescriptorHandler.diagnoseDescriptorNotFound(IdeaLocalDescriptorResolver.kt:44)
	at org.jetbrains.kotlin.resolve.lazy.LazyDeclarationResolver.findClassDescriptor(LazyDeclarationResolver.kt:88)
	at org.jetbrains.kotlin.resolve.lazy.LazyDeclarationResolver.getClassDescriptor(LazyDeclarationResolver.kt:62)
	at org.jetbrains.kotlin.resolve.lazy.LazyDeclarationResolver.getMemberScopeDeclaredIn$frontend(LazyDeclarationResolver.kt:227)
	at org.jetbrains.kotlin.resolve.lazy.LazyDeclarationResolver$resolveToDescriptor$1.visitNamedFunction(LazyDeclarationResolver.kt:124)
	at org.jetbrains.kotlin.resolve.lazy.LazyDeclarationResolver$resolveToDescriptor$1.visitNamedFunction(LazyDeclarationResolver.kt:94)
	at org.jetbrains.kotlin.psi.KtNamedFunction.accept(KtNamedFunction.java:50)
	at org.jetbrains.kotlin.resolve.lazy.LazyDeclarationResolver.resolveToDescriptor(LazyDeclarationResolver.kt:94)
	at org.jetbrains.kotlin.resolve.lazy.LazyDeclarationResolver.resolveToDescriptor(LazyDeclarationResolver.kt:91)
	at org.jetbrains.kotlin.resolve.lazy.ResolveSession.resolveToDescriptor(ResolveSession.java:324)
	at arrow.meta.qq.QuoteKt$classOrObject$$inlined$quote$2$1.invoke(Quote.kt:221)
	at arrow.meta.qq.QuoteKt$classOrObject$$inlined$quote$2$1.invoke(Quote.kt)
	at arrow.meta.extensions.MetaComponentRegistrar$syntheticResolver$1.generateSyntheticMethods(MetaComponentRegistrar.kt:407)
	at arrow.meta.extensions.MetaComponentRegistrar$registerSyntheticResolver$1.generateSyntheticMethods(MetaComponentRegistrar.kt:596)
	at org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension$Companion$getInstance$1.generateSyntheticMethods(SyntheticResolveExtension.kt:86)
I’m calling this from the
SyntheticResolverExtension.generateSyntheticMethods
s
I checked for a brief minute, it looks like idea has its own set of extensions for resolution ¯\_(ツ)_/¯ I am not sure if it is suitable in your case , but you can try it in the idea environment. https://github.com/JetBrains/kotlin/blob/master/idea/ide-common/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt#L65
k
Well you can try something like this to resolve a general class
thisDescriptor.module.findClassAcrossModuleDependencies(ClassId.topLevel(fqName))
And for BuiltIns
thisDescriptor.module.builtIns.unitType
(this would be from within
SyntheticResolveExtension.generateSyntheticMethods
)
r
Thanks @kralli, will try that out.