Is there any idiomatic / recommended way to detect...
# ksp
d
Is there any idiomatic / recommended way to detect dynamic values in Kotlin/JS? Right now I'm getting
kotlin.Any
as a type while return true for
anotherType.isAssignableFrom(typeThatIsInThisCaseDynamic)
I'm going to filter out all
Any
types in my codebase as a workaround, but wondering if there's something cleaner (especially since
dynamic
is a JS only concept I believe)
I noticed there is a
KSDynamicReference
class but when I try to check
property.type.element
to see if it's dynamic, it is null instead. If I could write
if (property.type.element is KSDyanimicReference)
I would be happy with that 🙂
🤔 1
j
we have built logic for checking dynamic types, but that might be broken as you are not getting the dynamic type, can you also try KSP2 to see if the issue resolves in KSP2?
d
I just tried setting
kp.useKSP2=true
and I'm getting an NPE in KSP
Copy code
Caused by: java.lang.NullPointerException: null cannot be cast to non-null type org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
	at com.google.devtools.ksp.impl.symbol.kotlin.UtilKt.getDefaultValue(util.kt:430)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$defaultArguments$2.invoke(KSAnnotationImpl.kt:88)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$defaultArguments$2.invoke(KSAnnotationImpl.kt:71)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl.getDefaultArguments(KSAnnotationImpl.kt:71)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$arguments$2.invoke(KSAnnotationImpl.kt:65)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$arguments$2.invoke(KSAnnotationImpl.kt:62)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl.getArguments(KSAnnotationImpl.kt:62)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSPropertyDeclarationImpl$annotations$2$4.invoke(KSPropertyDeclarationImpl.kt:71)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSPropertyDeclarationImpl$annotations$2$4.invoke(KSPropertyDeclarationImpl.kt:67)
	at kotlin.sequences.FilteringSequence$iterator$1.calcNext(Sequences.kt:171)
	at kotlin.sequences.FilteringSequence$iterator$1.hasNext(Sequences.kt:194)
	at kotlin.sequences.FlatteningSequence$iterator$1.ensureItemIterator(Sequences.kt:316)
	at kotlin.sequences.FlatteningSequence$iterator$1.hasNext(Sequences.kt:303)
	at kotlin.sequences.SequencesKt___SequencesKt.any(_Sequences.kt:1236)
	at com.google.devtools.ksp.common.visitor.CollectAnnotatedSymbolsVisitor.visitAnnotated(CollectAnnotatedSymbolsVisitor.kt:37)
	at com.google.devtools.ksp.common.visitor.CollectAnnotatedSymbolsVisitor.visitPropertyDeclaration(CollectAnnotatedSymbolsVisitor.kt:82)
	at com.google.devtools.ksp.common.visitor.CollectAnnotatedSymbolsVisitor.visitPropertyDeclaration(CollectAnnotatedSymbolsVisitor.kt:33)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSPropertyDeclarationImpl.accept(KSPropertyDeclarationImpl.kt:168)
	at com.google.devtools.ksp.common.visitor.CollectAnnotatedSymbolsVisitor.visitFile(CollectAnnotatedSymbolsVisitor.kt:43)
	at com.google.devtools.ksp.common.visitor.CollectAnnotatedSymbolsVisitor.visitFile(CollectAnnotatedSymbolsVisitor.kt:33)
	at com.google.devtools.ksp.impl.symbol.kotlin.KSFileImpl.accept(KSFileImpl.kt:84)
	at com.google.devtools.ksp.impl.ResolverAAImpl.collectAnnotatedSymbols(ResolverAAImpl.kt:578)
	at com.google.devtools.ksp.impl.ResolverAAImpl.access$collectAnnotatedSymbols(ResolverAAImpl.kt:81)
	at com.google.devtools.ksp.impl.ResolverAAImpl$newAnnotatedSymbols$2.invoke(ResolverAAImpl.kt:589)
	at com.google.devtools.ksp.impl.ResolverAAImpl$newAnnotatedSymbols$2.invoke(ResolverAAImpl.kt:588)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at com.google.devtools.ksp.impl.ResolverAAImpl.getNewAnnotatedSymbols(ResolverAAImpl.kt:588)
	at com.google.devtools.ksp.impl.ResolverAAImpl.getSymbolsWithAnnotation(ResolverAAImpl.kt:570)
	at com.google.devtools.ksp.processing.Resolver.getSymbolsWithAnnotation$default(Resolver.kt:48)
	at com.varabyte.kobweb.ksp.frontend.FrontendProcessor.process(FrontendProcessor.kt:80)
where FrontendProcessor#80 in my code looks like:
Copy code
resolver.getSymbolsWithAnnotation(INIT_KOBWEB_FQN).map { annotatedFun ->
Don't have a lot of time to look into this right now unfortunately
(Using
ksp = "1.9.23-1.0.20"
in case that's the problem)
j
no worries, I was just wondering if KSP2 can solve this particular issue for dynamic types, thanks for testing out, I can address this KSP2 issue of yours as well.
d
Anyway, if you want to test, it should be very easy. In a Kotlin/JS project with KSP applied, just add the top level property
val jsTest = js("2 + 2")
and then check yourself if
override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: Unit)
gives you a property with a dynamic type element.
👍 1
Not sure if the crash I'm getting is user error, so apologies if it's something with my project
j
No it is likely KSP2 issues, we are currently working on addressing these issues.
d
Good luck. Feel free to reach out to me by DM if you want a moderately complex project to test with.
thank you color 1
t
From the type system's point of view, can you try to see if a type 1) can be assigned to an arbitrary type (this is what you did. not sure if Nothing would work), and 2) can be assigned from Any. If both work, then it is "likely" a dynamic
Otherwise currently there is no way to tell whether a
KSType
is dynamic or not.
KSDyanimicReference
is a thing at the reference site. After resolving to a
KSType
, the reference site information is gone.
In your test case,
property.type.element
is null because there is no explicit type mentioned in the source.
d
Ok so if it's WAI that's fine. My code is fine because I just short circuit on the
Any
type, I don't ever want to support it anyway.
In your test case,
property.type.element
is null because there is no explicit type mentioned in the source.
Ah got it!
t
When a type is implicit, only a full body analysis can tell the underlying type. So type checking is probably the most efficient way already.
d
For context, basically I am searching for a bunch of top level properties to see if they are instances of one of several classes in my framework. They are all implicitly typed but it seems to be working (note: I am resolving them)
👍 1
r
we are getting same error when using
Copy code
#ksp
ksp.useKSP2=true
id("com.google.devtools.ksp") version "2.0.0-1.0.21"
Copy code
* What went wrong:
Execution failed for task ':pitaraShared:kspKotlinIosSimulatorArm64'.
> A failure occurred while executing com.google.devtools.ksp.gradle.KspAAWorkerAction
   > null cannot be cast to non-null type org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl