https://kotlinlang.org logo
Title
s

Sebastian Krajewski

04/19/2019, 6:57 PM
Hey, what's wrong here? I don't get any syntax error but compilator crashes with this message:
Cause: Label wasn't found during iterating through instructions
Element is unknownThe root cause was thrown at: LabelNormalizationMethodTransformer.kt:148
Oh, this also causes a compilator crash:
class foo(var bar : String)

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

streetsofboston

04/19/2019, 7:03 PM
Is it because may be trying to assign a value to a
null
property…?
baz?.bar
could be null. Then you may wind up with
null += test
. This should not compile. But instead of a compiler error you get a compiler crash…
I get the same error
s

Sebastian Krajewski

04/19/2019, 7:07 PM
But ?. operator ensures that you work on an instance
s

streetsofboston

04/19/2019, 7:08 PM
What you’d want is
baz?.let { it.bar += test }
This is a simplified version of the bug you found:
class Container(
    var value : String = ""
)

fun bug() {
    val container : Container? = null
    container?.value += "2"
}
It seems to come from the
+=
operator with an optional/nullable left-hand side expression
s

Sebastian Krajewski

04/19/2019, 7:19 PM
Yep, because
?.plus()
seems to work
But if value is non nullable and property is called safely, why it needs safe call?
s

streetsofboston

04/19/2019, 7:21 PM
I think since a
String
is a immutable class, the
+=
is not a
plusAssign
. It probably rewrites it a
object = object + param
, which may fail….
Rewriting it manually works….
class Container(
    var value : String = ""
)

fun bug() {
    val container : Container? = null
    container?.value = container?.value + "2"
}
You should file a bug.
s

Sebastian Krajewski

04/19/2019, 7:24 PM
It is already filled
💯 1
s

streetsofboston

04/19/2019, 7:26 PM
Do you have the link to the bug? I’d love to share it with my teams here. Thank you!
s

Sebastian Krajewski

04/19/2019, 7:26 PM