Possible bug in companion objects (or compiler)


#1

Not entirely sure this is a bug, but if you create a property inside a companion object, and use a "private set", you can use it in the class and the compiler won't complain, but then you get a runtime crash.

The example (I’m using Android, but I think it can be easily replicated in a plain Kotlin project):

 
class App : Application() {

  companion object {
  var instance: App by Delegates.notNull()
           private set
  }

  override fun onCreate() {
  super.onCreate()
  instance = this
  }
}


The error:

java.lang.NoSuchMethodError: No static method access$setInstance$1(Lcom/antonioleiva/weatherapp/ui/App$Companion;Lcom/antonioleiva/weatherapp/ui/App;)V in class Lcom/antonioleiva/weatherapp/ui/App$Companion; or its super classes (declaration of ‘com.antonioleiva.weatherapp.ui.App$Companion’ appears in /data/app/com.antonioleiva.weatherapp-1/base.apk)
           at com.antonioleiva.weatherapp.ui.App.onCreate(App.kt:15)


#2

This is a bug. I've reported an issue: https://youtrack.jetbrains.com/issue/KT-8928, please star/vote it. Thank you!