Files
kotlin-fork/compiler/testData/codegen/box/when/enumOptimization/enumInsideClassObject.kt
T
Sergej Jaskiewicz 55ae6d1f3e [JS IR] Optimize pattern-matching of enums into comparing their ordinals
For this code:
```
enum class Season {
    WINTER,
    SPRING,
    SUMMER,
    AUTUMN
}

fun bar1(x : Season) : String {
    return when (x) {
        Season.WINTER, Season.SPRING -> "winter_spring"
        Season.SUMMER -> "summer"
        else -> "autumn"
    }
}
```

previously we generated this:
```
function foo(x) {
    var tmp0_subject = x;
    return (tmp0_subject.equals(Season_WINTER_getInstance())
        ? true
        : tmp0_subject.equals(Season_SPRING_getInstance()))
            ? 'winter_spring'
            : tmp0_subject.equals(Season_SUMMER_getInstance())
                ? 'summer'
                : 'autumn';
}
```

And now we generated this:
```
function bar2(x) {
  var tmp0_subject = x;
  var tmp0 = tmp0_subject._get_ordinal__0_k$();
  var tmp;
  switch (tmp0) {
    case 0:
    case 1:
      tmp = 'winter_spring';
      break;
    case 2:
      tmp = 'summer';
      break;
    case 3:
      tmp = 'autumn';
      break;
    default:
      noWhenBranchMatchedException();
      break;
  }
  return tmp;
}
```
2021-10-14 11:44:01 +00:00

34 lines
802 B
Kotlin
Vendored

// WITH_RUNTIME
// CHECK_CASES_COUNT: function=foo count=3
// CHECK_IF_COUNT: function=foo count=0
import kotlin.test.assertEquals
class A {
companion object {
enum class Season {
WINTER,
SPRING,
SUMMER,
AUTUMN
}
}
}
fun foo(x : A.Companion.Season) : String {
return when (x) {
A.Companion.Season.WINTER -> "winter"
A.Companion.Season.SPRING -> "spring"
A.Companion.Season.SUMMER -> "summer"
else -> "other"
}
}
fun box() : String {
assertEquals("winter", foo(A.Companion.Season.WINTER))
assertEquals("spring", foo(A.Companion.Season.SPRING))
assertEquals("summer", foo(A.Companion.Season.SUMMER))
assertEquals("other", foo(A.Companion.Season.AUTUMN))
return "OK"
}