Skip to content

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

Open
@DanySK

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions