square / kotlinpoet

A Kotlin API for generating .kt source files.
https://square.github.io/kotlinpoet/
Apache License 2.0
3.9k stars 288 forks source link

KSType.toTypeName() is inaccurate when typealias are mixed. #1369

Closed nazoking closed 2 years ago

nazoking commented 2 years ago

When a type aliased as a generic type has a type parameter, the result of method KSType.toTypeName() outputs an incorrect type name and type parameter.

The following pattern is as expected.

// on code,
typealias Alias2<X> = Map<X, Int>
lateinit var n3: Alias2<String>

// and in ksp processor,
resolver.getPropertyDeclarationByName("n2", true)!!.type.toTypeName().toString()
// expected:  `Alias2<String>`  , equals actual: `Alias2<String>` 

However, the next two patterns are incorrect

// on code,
typealias Alias = List<Int>
val n: List<Alias>

// and in ksp processor,
resolver.getPropertyDeclarationByName("n", true)!!.type.toTypeName().toString()
// expected:  `List<Alias>`   , but actual: `List<Alias<Int>>` 
// on code,
typealias Alias2<X> = Map<X, Int>
lateinit var n2: List<Alias2<String>>

// and in ksp processor,
resolver.getPropertyDeclarationByName("n2", true)!!.type.toTypeName().toString()
// expected:  `List<Alias2<String>>`   , but actual: `List<Alias2<String, Int>>` 
ZacSweers commented 2 years ago

What version are you using?

nazoking commented 2 years ago

i'm using

nazoking commented 2 years ago

oh..?

I just ran your test file with my test code and run it, I get different results than what you report above. ( The results were output as expected. ) I will look into this a little more. Please let me know if you can give me any hints. My test environment is jdk version is 14 gradle is 7.2.

nazoking commented 2 years ago

ok.

The cause was found. The problem I faced was one that was fixed by the pull request #1321 . This fix has not yet been released and has not been incorporated in the version 1.20.0 I am testing. I close this Issue. I look forward to a release in the near future. Thanks for a great library.