openrewrite / rewrite-kotlin

Work-in-progress implementation of Kotlin language support for OpenRewrite.
Apache License 2.0
38 stars 12 forks source link

Error on type alias for function type #300

Closed knutwannheden closed 8 months ago

knutwannheden commented 10 months ago

The following test:

    @Test
    void typeAliasForFunctionType() {
        rewriteRun(
          kotlin(
            """
              typealias Action = (String) -> Unit
              """
          )
        );
    }

results in the following exception:

java.lang.ClassCastException: class org.openrewrite.kotlin.tree.K$FunctionType cannot be cast to class org.openrewrite.java.tree.Expression (org.openrewrite.kotlin.tree.K$FunctionType and org.openrewrite.java.tree.Expression are in unnamed module of loader 'app')
    at org.openrewrite.kotlin.internal.KotlinParserVisitor.visitTypeAlias(KotlinParserVisitor.kt:2831)
    at org.openrewrite.kotlin.internal.KotlinParserVisitor.visitElement0(KotlinParserVisitor.kt:4588)
    at org.openrewrite.kotlin.internal.KotlinParserVisitor.visitElement(KotlinParserVisitor.kt:4524)
    at org.openrewrite.kotlin.internal.KotlinParserVisitor.visitFile(KotlinParserVisitor.kt:166)
    at org.openrewrite.kotlin.KotlinParser.lambda$parseInputs$3(KotlinParser.java:176)
knutwannheden commented 10 months ago

We currently map a typealias to a variable declaration with an "empty" name, the alias as the type, and the aliased type as the initializer. The last bit is also where it goes wrong, because a K.FunctionType is a TypeTree, but not an Expression. Instead of using a variable declaration we should probably add a new K.TypeAlias statement type.

@traceyyoshima What are your thoughts on this?

knutwannheden commented 10 months ago

Please note that https://github.com/openrewrite/rewrite-kotlin/commit/a880c6da45e033ee2a120941279dbfb49fe80170 now fixes the parsing side, but I think the LST is still suboptimal here.

traceyyoshima commented 8 months ago

Fixed in PSI parser