[PSI2IR] Generate IR for functions and calls with context receivers
This commit is contained in:
committed by
TeamCityServer
parent
f4ddf66ac4
commit
307f318c9e
@@ -0,0 +1,18 @@
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// WITH_RUNTIME
|
||||
|
||||
class View {
|
||||
val coefficient = 42
|
||||
}
|
||||
|
||||
context(View) val Int.dp get() = coefficient * this
|
||||
|
||||
fun box(): String {
|
||||
with(View()) {
|
||||
if (listOf(1, 2, 10).map { it.dp } == listOf(42, 84, 420)) {
|
||||
return "OK"
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// WITH_RUNTIME
|
||||
|
||||
interface NumberOperations {
|
||||
operator fun Number.plus(other: Number): Number
|
||||
}
|
||||
|
||||
object DoubleOperations : NumberOperations {
|
||||
override operator fun Number.plus(other: Number) = this.toDouble() + other.toDouble()
|
||||
}
|
||||
|
||||
data class Matrix(val rows: Int, val columns: Int, val data: Array<out Number>) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as Matrix
|
||||
|
||||
if (rows != other.rows) return false
|
||||
if (columns != other.columns) return false
|
||||
if (!data.contentEquals(other.data)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = rows
|
||||
result = 31 * result + columns
|
||||
result = 31 * result + data.contentHashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun matrixOf(rows: Int, columns: Int, vararg data: Number): Matrix {
|
||||
assert(rows * columns == data.size) { "Wrong dimentions" }
|
||||
return Matrix(rows, columns, data)
|
||||
}
|
||||
|
||||
context(NumberOperations) operator fun Matrix.plus(other: Matrix): Matrix {
|
||||
assert(rows == other.rows && columns == other.columns) { "Matrices should have the same dimentions" }
|
||||
return matrixOf(rows, columns, *data.mapIndexed { i, element -> element + other.data[i] }.toTypedArray())
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val m1 = matrixOf(2, 2, 1, 2, 3, 4)
|
||||
val m2 = matrixOf(2, 2, .4, .3, .2, .1)
|
||||
with(DoubleOperations) {
|
||||
return if (m1 + m2 == matrixOf(2, 2, 1.4, 2.3, 3.2, 4.1)) "OK" else "fail"
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
|
||||
class A {
|
||||
val o = "O"
|
||||
}
|
||||
class B {
|
||||
val k = "K"
|
||||
}
|
||||
|
||||
context(B) fun A.f(a: Any, b: Any) = o + k
|
||||
|
||||
fun B.g(a: A): String {
|
||||
with (a) {
|
||||
return f(1, "2")
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return B().g(A())
|
||||
}
|
||||
Reference in New Issue
Block a user