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

Kotlin 2.0.0 - Typealias declaration is target type when typealias is a Composable function in a different module #1921

Closed
eygraber opened this issue May 23, 2024 · 2 comments

Comments

@eygraber
Copy link

eygraber commented May 23, 2024

There are 3 commits in this sample project.

The first commit (f451dc8) has a typealias that is a Composable function in foo/src/main/java/com/example/foo/Foo.kt

class FooHolder(
    val foo: Foo,
)

typealias Foo = @Composable ((id: Long) -> Unit) -> Unit

FooHolder is referenced in app/src/main/java/com/example/myapplication.MainActivity.kt

@ExampleAnnotation
abstract class Example {
    abstract val fooHolder: FooHolder
}

The KSP processor looks at the primary constructor of FooHolder and prints out whether it is a typealias, and what the type is:

val symbols = resolver.getSymbolsWithAnnotation(ExampleAnnotation::class.qualifiedName!!)

for (symbol in symbols) {
    if (symbol is KSClassDeclaration) {
        val exampleClass =
            symbol.getAllProperties()
                .first().type.resolve().declaration as KSClassDeclaration

        exampleClass
            .primaryConstructor
            ?.parameters
            ?.find { it.name?.getShortName() == "foo" }
            ?.let { param ->
                logger.error("isTypeAlias(${param.type.resolve().declaration is KSTypeAlias}) - ${param.type}")
            }
    }
}

In this case running ./gradlew assembleDebug prints:

isTypeAlias(false) - Function1<Function1<[@kotlin.ParameterName] Long, Unit>, Unit>

The second commit (cedd6ef) removes the Composable annotation from Foo. Running prints:

isTypeAlias(true) - Foo

The third commit (63e6de2) adds back the Composable annotation to Foo, but moves Foo and FooHolder into the app module. Running prints:

isTypeAlias(true) - Foo

This happens with both KSP1 and KSP2.

There is a separate branch called 1.9.24 (which branches off of the first "broken" commit) that uses Kotlin 1.9.24. Running prints:

isTypeAlias(true) - Foo

It's unclear to me if that's because of something that was introduced in KSP 2.0.0-1.0.21 or because the Compose compiler changed to org.jetbrains.kotlin.plugin.compose (or both).

@julioromano
Copy link

julioromano commented Jun 6, 2024

FYI: Problem persists with KSP 2.0.0-1.0.22

@eygraber
Copy link
Author

Looks like this is fixed in newer versions of KSP / Kotlin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants