Compiler silently captures private fields in inline fun


#1

 

class Abc

class Obj{
  private val props = HashMap<String, Any>()

  inline fun <reified T> invoke():T = props.values().first { it is T } as T
}

fun main(args: Array<String>) {
  val obj = Obj()
  println(obj<Abc>())
}

Output:

Exception in thread "main" java.lang.IllegalAccessError: tried to access field rule2.Obj.props from class rule2.Rule2Package$Main$c9203aaa

This is runtime err. I suggest compiletime err.


#2

Yes, this is a known issue: https://youtrack.jetbrains.com/issue/KT-6453. You can star or vote for it. Thanks!