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

Stack overflow when generating names if types are "curiously recurring" (recursively bound generics) #1914

Open
DanySK opened this issue May 22, 2024 · 0 comments

Comments

@DanySK
Copy link

DanySK commented May 22, 2024

Describe the bug
Building a type name for a type whose bound is recursively generic produces a stack overflow.
However, this pattern of use of generics is very common, and used also in the standard library (see example).

To Reproduce

Consider the following main :

import com.squareup.kotlinpoet.asTypeName
import kotlin.reflect.jvm.kotlinFunction

fun main() {
    Class.forName("kotlin.collections.ArraysKt").methods.first {
        // Selects fun <T : Comparable<T>> Array<T>.max(): T
        it.name == "max" && it.parameters.first().parameterizedType.typeName.contains("Comparable")
    }.kotlinFunction?.parameters?.first()?.type
        ?.asTypeName() // Stack overflow due to the recurring type.
}

Expected behavior
A TypeName for T : Comparable<T> is produced.

Additional context
We are writing a code generator that runs through the standard library, and we noticed we cannot use it on comparators.

@DanySK DanySK added the bug label May 22, 2024
@DanySK DanySK changed the title Stack overflow when a curiously recurring template pattern (recursively generic type) is used Stack overflow when a curiously recurring template pattern (recursively bound generic type) is used May 22, 2024
@DanySK DanySK changed the title Stack overflow when a curiously recurring template pattern (recursively bound generic type) is used Stack overflow when generating names if types are "curiously recurring" (recursively bound generics) May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants