2cf8f75a90
In FIR we desugar when with multiple conditions leading to same block
as tree of OR expressions
Given
```
when(some) {
"a", "b", "c" -> {}
else -> {}
}
```
actually desugared into
```
when(val <subj> = some) {
<subj> == "a" || <subj> == "b" || <subj> == "c" -> {}
else -> {}
}
```
There is a multiple ways of how we can organize such expressions in FIR
Previously it was just nesting-chain of OR expressions
While the most efficient way in terms of required stack depth is
a balanced tree
KT-53255
65 lines
1.6 KiB
Kotlin
Vendored
65 lines
1.6 KiB
Kotlin
Vendored
object A {
|
|
private constructor() /* primary */ {
|
|
super/*Any*/()
|
|
/* <init>() */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fun testWithSubject(x: Any?): String {
|
|
return { // BLOCK
|
|
val tmp0_subject: Any? = x
|
|
when {
|
|
EQEQ(arg0 = tmp0_subject, arg1 = null) -> "null"
|
|
EQEQ(arg0 = tmp0_subject, arg1 = A) -> "A"
|
|
tmp0_subject is String -> "String"
|
|
tmp0_subject !is Number -> "!Number"
|
|
setOf<Nothing>().contains<Number>(element = tmp0_subject /*as Number */) -> "nothingness?"
|
|
else -> "something"
|
|
}
|
|
}
|
|
}
|
|
|
|
fun test(x: Any?): String {
|
|
return when {
|
|
EQEQ(arg0 = x, arg1 = null) -> "null"
|
|
EQEQ(arg0 = x, arg1 = A) -> "A"
|
|
x is String -> "String"
|
|
x !is Number -> "!Number"
|
|
setOf<Nothing>().contains<Number>(element = x /*as Number */) -> "nothingness?"
|
|
else -> "something"
|
|
}
|
|
}
|
|
|
|
fun testComma(x: Int): String {
|
|
return { // BLOCK
|
|
val tmp1_subject: Int = x
|
|
when {
|
|
when {
|
|
when {
|
|
EQEQ(arg0 = tmp1_subject, arg1 = 1) -> true
|
|
else -> EQEQ(arg0 = tmp1_subject, arg1 = 2)
|
|
} -> true
|
|
else -> when {
|
|
EQEQ(arg0 = tmp1_subject, arg1 = 3) -> true
|
|
else -> EQEQ(arg0 = tmp1_subject, arg1 = 4)
|
|
}
|
|
} -> "1234"
|
|
when {
|
|
EQEQ(arg0 = tmp1_subject, arg1 = 5) -> true
|
|
else -> when {
|
|
EQEQ(arg0 = tmp1_subject, arg1 = 6) -> true
|
|
else -> EQEQ(arg0 = tmp1_subject, arg1 = 7)
|
|
}
|
|
} -> "567"
|
|
when {
|
|
EQEQ(arg0 = tmp1_subject, arg1 = 8) -> true
|
|
else -> EQEQ(arg0 = tmp1_subject, arg1 = 9)
|
|
} -> "89"
|
|
else -> "?"
|
|
}
|
|
}
|
|
}
|
|
|