-
Notifications
You must be signed in to change notification settings - Fork 285
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
KSType.toTypeName function returns ClassName for alias type instead of ParameterizedTypeName #1513
Comments
This happens to me too, but even without type aliases. Given this file: interface Repository<T>
class RealRepository @Inject constructor() : Repository<String> And you get the supertypes for |
Yeah, I haven't noticed that pull request I've mentioned was already released in 1.13.0. |
PRs are welcome if someone wants to take a look at one. I've been traveling the past week for kotlinconf and have a busy work week so not sure when I'll have time |
* Bump org.jetbrains.kotlin:kotlin-gradle-plugin from 1.8.10 to 1.8.20 Bumps [org.jetbrains.kotlin:kotlin-gradle-plugin](https://github.com/JetBrains/kotlin) from 1.8.10 to 1.8.20. - [Release notes](https://github.com/JetBrains/kotlin/releases) - [Changelog](https://github.com/JetBrains/kotlin/blob/master/ChangeLog.md) - [Commits](https://github.com/JetBrains/kotlin/commits) --- updated-dependencies: - dependency-name: org.jetbrains.kotlin:kotlin-gradle-plugin dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * Bump kotlinPoetVersion from 1.12.0 to 1.13.0 (#163) * Bump kotlinPoetVersion from 1.12.0 to 1.13.0 Bumps `kotlinPoetVersion` from 1.12.0 to 1.13.0. Updates `com.squareup:kotlinpoet` from 1.12.0 to 1.13.0 - [Release notes](https://github.com/square/kotlinpoet/releases) - [Changelog](https://github.com/square/kotlinpoet/blob/master/docs/changelog.md) - [Commits](square/kotlinpoet@1.12.0...1.13.0) Updates `com.squareup:kotlinpoet-ksp` from 1.12.0 to 1.13.0 - [Release notes](https://github.com/square/kotlinpoet/releases) - [Changelog](https://github.com/square/kotlinpoet/blob/master/docs/changelog.md) - [Commits](square/kotlinpoet@1.12.0...1.13.0) --- updated-dependencies: - dependency-name: com.squareup:kotlinpoet dependency-type: direct:production update-type: version-update:semver-minor - dependency-name: com.squareup:kotlinpoet-ksp dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump com.google.devtools.ksp from 1.8.10-1.0.9 to 1.8.20-1.0.10 (#164) * Bump com.google.devtools.ksp from 1.8.10-1.0.9 to 1.8.20-1.0.10 Bumps [com.google.devtools.ksp](https://github.com/google/ksp) from 1.8.10-1.0.9 to 1.8.20-1.0.10. - [Release notes](https://github.com/google/ksp/releases) - [Commits](google/ksp@1.8.10-1.0.9...1.8.20-1.0.10) --- updated-dependencies: - dependency-name: com.google.devtools.ksp dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * Bump com.google.devtools.ksp:symbol-processing-api (#165) Bumps [com.google.devtools.ksp:symbol-processing-api](https://github.com/google/ksp) from 1.8.10-1.0.9 to 1.8.20-1.0.10. - [Release notes](https://github.com/google/ksp/releases) - [Commits](google/ksp@1.8.10-1.0.9...1.8.20-1.0.10) --- updated-dependencies: - dependency-name: com.google.devtools.ksp:symbol-processing-api dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Selim Dinçer <wowselim@live.de> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Selim Dinçer <wowselim@live.de> * update gradle * revert kotlinPoet bump due to square/kotlinpoet#1513 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Selim Dinçer <wowselim@live.de>
@ansman I'm unable to reproduce your example with this test and modifying @Test
fun regression_1513() {
val compilation = prepareCompilation(
kotlin(
"Example.kt",
"""
package test
import com.squareup.kotlinpoet.ksp.test.processor.ExampleAnnotation
interface Repository<T>
@ExampleAnnotation
class RealRepository @Inject constructor() : Repository<String>
""",
),
)
val result = compilation.compile()
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
val generatedFileText = File(compilation.kspSourcesDir, "kotlin/test/TestRealRepository.kt")
.readText()
assertThat(generatedFileText).isEqualTo(
"""
package test
import kotlin.String
public class RealRepository : Repository<String>
""".trimIndent(),
)
} |
@Squiry your example is not very helpful as it is intentionally calling a different API and excluding type arguments. It would fail even if we revert the changes in the PR you mention. |
Is there any other API for converting
This bug is not about mentioned PR, I've just noted that after that PR test fails even on non alias types. |
The problem is still there, |
|
You have fixed the regression introduced in #1321, but my problem was on the version before it, I've just mentioned that #1321 have broken method even more. Reproducer I'm giving is still valid, I've checked it on master. Also you can notice, that |
The problem is that public fun KSType.toTypeName(
typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY,
): TypeName = toTypeName(typeParamResolver, arguments) |
If you have a fix, can you submit a fix + tests as a PR? |
* KSType.toTypeName fixed to work with aliased types resolves #1513 * test fixed * imports fixed * test case * import fix * imports fix * Simplify test + fix formatting/style issues --------- Co-authored-by: Zac Sweers <pandanomic@gmail.com>
Sorry for the late reply, but here is a repro. Given the code that @ZacSweers already posted, I can repro the issue with this code: // Add interfaces
decl.superTypes
.map { it.resolve() }
.filter { (it.declaration as KSClassDeclaration).classKind == ClassKind.INTERFACE }
.map { it.toTypeName() }
.forEach { classBuilder.addSuperinterface(it) } If I instead use the overload that uses a // Add interfaces
decl.superTypes
.filter { (it.resolve().declaration as KSClassDeclaration).classKind == ClassKind.INTERFACE }
.map { it.toTypeName() }
.forEach { classBuilder.addSuperinterface(it) } I believe the issue here is that public fun KSType.toTypeName(
typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY,
): TypeName = toTypeName(typeParamResolver, emptyList()) |
Changing it to this, fixes the issue and all tests pass: public fun KSType.toTypeName(
typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY,
): TypeName = toTypeName(typeParamResolver, arguments) |
This should be fixed already on the main branch if you could try snapshots and confirm |
Confirmed! |
I only pray for we haven't break some hidden implementation detail that relied on that emptyList behaviour. Other than that waiting for release! |
Yep, planning to publish another patch release this week! I'm in the process of setting up auto-releases from CI and trying to improve our workflow configuration, here's the latest attempt: #1537. Appreciate any 👀 ! |
1.13.2 is up! |
Describe the bug
I expect calling
KSType.toTypeName()
on a type likeArrayList<String>
to returnParameterizedTypeName
withrawType=kotlin.collections.List
andtypeArguments=[kotlin.String]
, but I get just rawClassName(kotlin.collections.List)
instead.To Reproduce
Just change this line to
val parameterType = parameter.type.resolve().toTypeName(functionTypeParams).let {
andsmokeTest
will fail with something like this:Additional context
Right now on a master branch
KSType.toTypeName
return non parameterizedClassName
even for non aliased types and reproducer from above will break the with much more errors. This behaviour was changed in a #1321 and I don't know if this was intended.The text was updated successfully, but these errors were encountered: