8000 How to identify if a `KSTypeReference` is an aliased imported type? · Issue #2391 · google/ksp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

How to identify if a KSTypeReference is an aliased imported type? #2391

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

Open
mcarleio opened this issue Mar 27, 2025 · 4 comments
Open

How to identify if a KSTypeReference is an aliased imported type? #2391

mcarleio opened this issue Mar 27, 2025 · 4 comments
Milestone

Comments

@mcarleio
Copy link

TLDR: How to (consistently) identify that a type is actually an aliased import?


Given the following code block

import kotlin.Double as FP

interface Mapper {
    fun fromFP(fp: FP): Int
    fun fromString(str: String?): Int?
}

And given the following simplified KSP code

ksClassDeclaration
            .getAllFunctions()
            .map {
                 isAlias(it.parameters.first().type)
            }

My current (frankly ugly and not optimal) way to identify if it is an alias:

fun isAlias(typeReference: KSTypeReference): Boolean {
    val resolvedType = typeReference.resolve()
    return typeReference.toString().takeWhile { it != '<' } != resolvedType.makeNotNullable().toString().takeWhile { it != '<' }
}

Sadly this is not working anymore since 2.1.20-1.0.32, as the typeReference is not anymore an instance of KSTypeReferenceImpl (1.0.31) but of KSTypeReferenceResolvedImpl (1.0.32), which has a different result for toString().

For fromFP:

  • With 1.0.31 the type is an instance of KSTypeReferenceImpl with toString() == "FP"
  • With 1.0.32 the type is an instance of KSTypeReferenceResolvedImpl with toString() == "Double"

I could amend the already ugly isAlias logic, but then I directly stumble upon similar problems with nullable types (see fromString) or even worse: a combination of nullable aliased import types with generics 😵

For fromString:

  • With 1.0.31 the type is an instance of KSTypeReferenceImpl with toString() == "String"
  • With 1.0.32 the type is an instance of KSTypeReferenceResolvedImpl with toString() == "String?"
@mcarleio
Copy link
Author

Background is, that I am writing a mapping library which will generate the implementation of those methods and I want to use the same names/aliases as the original code and not switch to the resolved types - although this will also work, but it can look very ugly when the alias e.g. was used due to colliding names.

override fun fromFP(fp: FP): Int = fp.toInt()
// vs
override fun fromFP(fp: Double): Int = fp.toInt()

@ting-yuan
Copy link
Collaborator
ting-yuan commented Mar 29, 2025

Although KSTypeReferenceResolvedImpl.toString() should render the alias rather than the underlying type (I consider it a minor bug), have you tried typeRef.resolve().declaration is KSTypeAlias which is more reliable than toString?

@mcarleio
Copy link
Author

I think this will only work when those are typealiases. But in my case it is just an import alias (e.g. import kotlin.Double as FS), so typeRef.resolve().declaration is directly a KSClassDeclaration and not a KSTypeAlias.

Had a look with debugger on the KSTypeReferenceResolvedImpl with 1.0.32 and could not find any way to identify the import alias name.

@mcarleio
Copy link
Author
mcarleio commented Apr 5, 2025

@ting-yuan any further ideas or hints?

@ting-yuan ting-yuan added this to the 2.0.1 milestone Apr 9, 2025
@ting-yuan ting-yuan modified the milestones: 2.0.1, 2.0, 2.0.2 Apr 30, 2025
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
0