Skip to content

Commit 15b78c3

Browse files
authored
Merge pull request #39 from JSMonk/fix-lambda-creating-in-plugin
Remove duplicate of IR nodes inside the body
2 parents 8f70566 + 5a53e36 commit 15b78c3

File tree

2 files changed

+12
-39
lines changed

2 files changed

+12
-39
lines changed

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/ir/SuspendTransformTransformer.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
1111
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
1212
import org.jetbrains.kotlin.ir.IrStatement
1313
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
14+
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
1415
import org.jetbrains.kotlin.ir.builders.*
1516
import org.jetbrains.kotlin.ir.declarations.*
1617
import org.jetbrains.kotlin.ir.expressions.IrBody
@@ -241,21 +242,17 @@ private fun generateTransformBodyForFunctionLambda(
241242
parameter.defaultValue = originFunctionValueParameter.defaultValue
242243
}
243244

244-
245245
return context.createIrBuilder(function.symbol).irBlockBody {
246246
val suspendLambdaFunc = context.createSuspendLambdaFunctionWithCoroutineScope(
247247
originFunction = originFunction,
248248
function = function,
249249
this
250-
).also { +it }
250+
)
251251

252252
val lambdaType = context.symbols.suspendFunctionN(0).typeWith(suspendLambdaFunc.returnType)
253253

254-
val ex = IrFunctionExpressionImpl(-1, -1, lambdaType, suspendLambdaFunc, IrStatementOrigin.LAMBDA)
255-
+ex
256-
257254
+irReturn(irCall(transformTargetFunctionCall).apply {
258-
putValueArgument(0, ex)
255+
putValueArgument(0, IrFunctionExpressionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, lambdaType, suspendLambdaFunc, IrStatementOrigin.LAMBDA))
259256
// argument: 1, if is CoroutineScope, and this is CoroutineScope.
260257
val owner = transformTargetFunctionCall.owner
261258

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/IrFunctionUtils.kt

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import org.jetbrains.kotlin.ir.builders.*
1010
import org.jetbrains.kotlin.ir.builders.declarations.*
1111
import org.jetbrains.kotlin.ir.declarations.*
1212
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
13-
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
1413
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
15-
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
1614
import org.jetbrains.kotlin.ir.types.IrSimpleType
1715
import org.jetbrains.kotlin.ir.types.getClass
1816
import org.jetbrains.kotlin.ir.types.typeOrNull
@@ -59,7 +57,7 @@ fun IrPluginContext.createSuspendLambdaWithCoroutineScope(
5957
lambdaType: IrSimpleType,
6058
originFunction: IrFunction,
6159
): IrClass {
62-
return IrFactoryImpl.buildClass {
60+
return irFactory.buildClass {
6361
name = SpecialNames.NO_NAME_PROVIDED
6462
kind = ClassKind.CLASS
6563
/*
@@ -149,35 +147,15 @@ fun IrPluginContext.createSuspendLambdaFunctionWithCoroutineScope(
149147
function: IrFunction,
150148
blockBodyBuilder: IrBlockBodyBuilder
151149
): IrSimpleFunction {
152-
val func = IrFunctionImpl(
153-
startOffset = -1,
154-
endOffset = -1,
155-
origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA,
156-
name = SpecialNames.NO_NAME_PROVIDED,
157-
visibility = DescriptorVisibilities.LOCAL,
158-
isInline = false,
159-
isExpect = false,
160-
returnType = function.returnType,
161-
modality = Modality.FINAL,
162-
symbol = IrSimpleFunctionSymbolImpl(),
163-
isSuspend = true,
164-
isTailrec = false,
165-
isOperator = false,
166-
isInfix = false,
167-
isExternal = false,
168-
)
169-
170-
// val func1 = IrFactoryImpl.buildFun {
171-
// name = SpecialNames.NO_NAME_PROVIDED
172-
// visibility = DescriptorVisibilities.LOCAL
173-
// isSuspend = true
174-
// returnType = function.returnType
175-
// }
176-
177-
with(func) {
150+
return irFactory.buildFun {
151+
origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
152+
name = SpecialNames.NO_NAME_PROVIDED
153+
visibility = DescriptorVisibilities.LOCAL
154+
returnType = function.returnType
155+
modality = Modality.FINAL
156+
isSuspend = true
157+
}.apply {
178158
parent = function
179-
// origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
180-
181159
body = createIrBuilder(symbol).run {
182160
// don't use expr body, coroutine codegen can't generate for it.
183161
irBlockBody {
@@ -198,8 +176,6 @@ fun IrPluginContext.createSuspendLambdaFunctionWithCoroutineScope(
198176
}
199177
}
200178
}
201-
202-
return func
203179
}
204180

205181
fun IrFunction.paramsAndReceiversAsParamsList(): List<IrValueParameter> {

0 commit comments

Comments
 (0)