Kotlin recursive type inference bug

Consider this piece of code:

class Node(children: List<Node>) {
    val sum = children.sumBy { it.sum }
}

Because it can't tell what type sum is supposed to be, rather than produce a meaningful error, it breaks IDEs and compilers:

  • IntelliJ complains with org.jetbrains.kotlin.idea.caches.resolve.KotlinIdeaResolutionException: Kotlin resolution encountered a problem while analyzing KtCallExpression
  • Try Kotlin throws error 500s at you
  • If you manage to compile it, you'll get a org.jetbrains.kotlin.util.ReenteringLazyValueComputationException

However, if you slightly change the code and make it

class Node(children: List<Node>) {
    val sum: Int = children.sumBy { it.sum }
}

(note sum has a type explicitly defined), everything works as it's supposed to.

Just wanted to let you know about this, and maybe see if anyone else had a similar error happen in the past.

Posted Dec 8, 2018, 7:01 AM UTC

167
matejdro•

I recommend you search official Kotlin's issue tracker and if issue is not there yet, make one: https://youtrack.jetbrains.com/issues/KT

Jetbrains is surprisingly active in their issue tracker.

16
qrck13•

No surprise they are active. Jet means fast :)

6
Moercy•

I just wish they would use the github issue tracker :-(

1
matejdro•

Nah, I think at their scale better issue tracker is needed with categories and statuses and shit.

6
Moercy•

I don't know, as a user I have a lot of problems working with YouTrack, as the kotlin issue tracker is the only place I get in touch with it. It might be just me being lazy, but having to create an account and get informations within a to me unknown issue tracker is hard.

The scale argument doesn't do it for me, because there are way bigger orgs/repos organized on Github. But I'm okay with Jetbrains feeling at home with YouTrack and being more productive there ;-) It's just another burden for me to think twice about filing an issue

E: typo

1
hvidstue•

They not only feel more at home with YouTrack - they BUILD YouTrack ;)
https://www.jetbrains.com/youtrack/

1
mzarechenskiy•

Thanks for your example! I created an issue: KT-28726

3