angryziber / kotlin-puzzlers

A collection of Kotlin Puzzlers
422 stars 58 forks source link

Puzzler: Compiler crash on `+=` after a `?.` expression #54

Closed streetsofboston closed 4 years ago

streetsofboston commented 5 years ago

This code makes the compiler crash. The bug is know and referenced here: https://youtrack.jetbrains.com/issue/KT-31135

class foo(var bar : String)

fun main() {    
    var baz : foo? = foo("test")
    baz?.bar += "test"
}

It seems to be relate to the fact that

  1. the += is operated on an immutable class (String) and the statement is re-written as baz?.bar = baz?.bar + "test" and
  2. The plus extension function on a String takes a nullable receiver: operator fun String?.plus(other: Any?): String

Rewriting it by hand, this bug goes away. It looks like the compiler messes up the re-write.

If the plus's receiver were non-nullable, a compiler error would have been generated instead of it crashing.