Kotlin 2.0.20 bug found

I ran into a problem in 2.0.20 that I can not reproduce in 1.9.0, 1.9.10, 1.9.20 or 2.0.0

The following code won’t compile due to accessing the var “list”

class GenericClass {
@PublishedApi internal var list = mutableListOf()

companion object {
inline fun GenericClass.init() {
list = mutableListOf() // compiler error accessing list, only fails for setting, not getting
}
}
}

This appears to be specific to generic classes, this same code without the generic type compiles.

Thoughts? Am reporting to the right place?

The correct place to report bugs is https://kotl.in/issue

Also, I can’t reproduce your problem. How do you call this init function? Could you write it in https://play.kotlinlang.org/?

I can post a project, I don’t call it at all, this doesn’t compile when building with 2.0.20, but it builds with 1.9 and 2.0.0

It doesn’t look like your sample code would ever compile, because you’re not providing a type parameter to mutableListOf. How does the compiler know what the type of internal var list is? Right now it’d just be List with no type.

There seems to be a formatting problem due to angle brackets <>. I was able to get the original text of the post using Discourse’s raw API, but OP, please use code formatting using triple backticks ``` next time

class GenericClass<T> {
  @PublishedApi internal var list = mutableListOf<String>()

  companion object {
    inline fun <reified T : Any> GenericClass<T>.init() {
      list = mutableListOf() // compiler error accessing list, only fails for setting, not getting
    }
  }
}

I can’t seem to reproduce the compiler error though, at least on 2.0.21

1 Like

I just verified that is doesn’t occur in 2.0.21, just in 2.0.20 so you can consider this fixed.