// KJS_WITH_FULL_RUNTIME // EXPECTED_REACHABLE_NODES: 1545 package foo // CHECK_CALLED: doFilter // CHECK_NOT_CALLED: filterIsInstance data class A(val x: Int) data class B(val x: Int) // filter from stdlib is not used, because it's important, // that filter function is not inline. When lambda is // not inlined and captures some local variable, // the test crashes on runtime (it's expected behaviour). fun Array.doFilter(fn: (T)->Boolean): List { val filtered = arrayListOf() for (i in 0..lastIndex) { val element = this[i] if (fn(element)) { filtered.add(element) } } return filtered } inline fun filterIsInstance(arrayOfAnys: Array): List { return arrayOfAnys.doFilter { it is T }.map { it as T } } fun box(): String { val src: Array = arrayOf(A(1), B(2), A(3), B(4)) assertEquals(listOf(A(1), A(3)), filterIsInstance(src)) assertEquals(listOf(B(2), B(4)), filterIsInstance(src)) return "OK" }