Skip to content

The get / set index access operator overloads are incorrectly escaped #1869

Open
@daniel-rusu

Description

Describe the bug
Using KotlinPoet 1.16.0 to generate the index-access operator overloads for get / set escapes the function name producing:

public operator fun `get`(index: Int): Int = values[index]

instead of:

public operator fun get(index: Int): Int = values[index]

To Reproduce
This code generates a half-baked collection to reproduce the issue:

val file = FileSpec.builder("", "MyCollectionExample")
        .addType(
            TypeSpec.classBuilder(ClassName("", "MyCollectionExample"))
                .primaryConstructor(
                    FunSpec.constructorBuilder()
                        .addParameter("values", typeNameOf<IntArray>())
                        .build()
                ).addProperty(
                    PropertySpec.builder("values", typeNameOf<IntArray>(), KModifier.PRIVATE)
                        .initializer("values")
                        .build()
                )
                .addFunction(
                    FunSpec.builder("get")
                        .addModifiers(KModifier.OPERATOR)
                        .returns(Int::class)
                        .addParameter("index", Int::class)
                        .addStatement("return values[index]")
                        .build()
                )
                .build()
        )
        .build()

    file.writeTo(System.out)

produces:

public class MyCollectionExample(
  private val values: IntArray,
) {
  public operator fun `get`(index: Int): Int = values[index]
}

Notice the function name.

Expected behavior
I would expect the function name not to be escaped when overloading the index access operator.

Additional context
This was probably introduced by:
#994

Skimming through the code, it's interesting that MemberName.emit tries to account for this scenario by only escaping the name when the operator is null. I wonder if we can remove the get / set soft-keywords from Util.KEYWORDS or if that would introduce any other side-effects.

Activity

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

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions