Skip to content

Commit 3a4a9c1

Browse files
authored
Merge pull request #869 from Kotlin/lower-implicit-receivers
[Compiler plugin] Lower frontend generated implicit receivers
2 parents 0621169 + 358fcd9 commit 3a4a9c1

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/aggregation/AggregateDsl.kt

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.aggregation
22

33
import org.jetbrains.kotlinx.dataframe.DataFrame
4+
import org.jetbrains.kotlinx.dataframe.annotations.HasSchema
45
import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
56
import org.jetbrains.kotlinx.dataframe.api.ColumnSelectionDsl
67
import org.jetbrains.kotlinx.dataframe.api.pathOf
@@ -11,6 +12,7 @@ import org.jetbrains.kotlinx.dataframe.impl.columnName
1112
import kotlin.reflect.KProperty
1213
import kotlin.reflect.typeOf
1314

15+
@HasSchema(schemaArg = 0)
1416
public abstract class AggregateDsl<out T> :
1517
DataFrame<T>,
1618
ColumnSelectionDsl<T> {

plugins/kotlin-dataframe/src/org/jetbrains/kotlinx/dataframe/plugin/extensions/IrBodyFiller.kt

+25-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.ir.expressions.IrConst
3030
import org.jetbrains.kotlin.ir.expressions.IrErrorCallExpression
3131
import org.jetbrains.kotlin.ir.expressions.IrExpression
3232
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
33+
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
3334
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
3435
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
3536
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
@@ -41,6 +42,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
4142
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
4243
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
4344
import org.jetbrains.kotlin.ir.types.IrSimpleType
45+
import org.jetbrains.kotlin.ir.types.IrType
4446
import org.jetbrains.kotlin.ir.types.classFqName
4547
import org.jetbrains.kotlin.ir.types.classOrFail
4648
import org.jetbrains.kotlin.ir.types.classifierOrNull
@@ -235,16 +237,32 @@ private class DataFrameFileLowering(val context: IrPluginContext) : FileLowering
235237
return true
236238
}
237239

238-
@OptIn(UnsafeDuringIrConstructionAPI::class)
240+
// org.jetbrains.kotlin.fir.backend.generators.CallAndReferenceGenerator#applyReceivers
241+
override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression {
242+
if (isScope(expression.typeOperand)) {
243+
return expression.replaceWithConstructorCall()
244+
}
245+
return super.visitTypeOperator(expression)
246+
}
247+
239248
override fun visitErrorCallExpression(expression: IrErrorCallExpression): IrExpression {
240-
val origin = (expression.type.classifierOrNull?.owner as? IrClass)?.origin ?: return expression
241-
val fromPlugin = origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey is DataFramePlugin
242-
val scopeReference = expression.type.classFqName?.shortName()?.asString()?.startsWith("Scope") ?: false
243-
if (!(fromPlugin || scopeReference)) {
249+
if (!isScope(expression.type)) {
244250
return expression
245251
}
246-
val constructor = expression.type.getClass()!!.constructors.toList().single()
247-
val type = expression.type
252+
return expression.replaceWithConstructorCall()
253+
}
254+
255+
@OptIn(UnsafeDuringIrConstructionAPI::class)
256+
private fun isScope(type: IrType): Boolean {
257+
val origin = (type.classifierOrNull?.owner as? IrClass)?.origin ?: return false
258+
val fromPlugin = origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey is DataFramePlugin
259+
val scopeReference = type.classFqName?.shortName()?.asString()?.startsWith("Scope") ?: false
260+
return fromPlugin || scopeReference
261+
}
262+
263+
@OptIn(UnsafeDuringIrConstructionAPI::class)
264+
private fun IrExpression.replaceWithConstructorCall(): IrConstructorCallImpl {
265+
val constructor = type.getClass()!!.constructors.toList().single()
248266
return IrConstructorCallImpl(-1, -1, type, constructor.symbol, 0, 0, 0)
249267
}
250268
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
2+
import org.jetbrains.kotlinx.dataframe.api.*
3+
4+
5+
@DataSchema
6+
data class Record(val a: String, val b: Int)
7+
8+
fun box(): String {
9+
val df = List(10) { Record(it.toString(), it) }.let { dataFrameOf(*it.toTypedArray()) }
10+
val aggregate = df.pivot { b }.aggregate {
11+
this.add("c") { 123 }.c
12+
}
13+
return "OK"
14+
}

plugins/kotlin-dataframe/tests-gen/org/jetbrains/kotlin/fir/dataframe/DataFrameBlackBoxCodegenTestGenerated.java

+6
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,12 @@ public void testUpdate() {
442442
runTest("testData/box/update.kt");
443443
}
444444

445+
@Test
446+
@TestMetadata("wrongReceiver.kt")
447+
public void testWrongReceiver() {
448+
runTest("testData/box/wrongReceiver.kt");
449+
}
450+
445451
@Nested
446452
@TestMetadata("testData/box/colKinds")
447453
@TestDataPath("$PROJECT_ROOT")

0 commit comments

Comments
 (0)