Skip to content

Commit 74eb23b

Browse files
committed
WIP: 尝试支持对 CoroutineScope 的安全类型转化支持
1 parent 868006e commit 74eb23b

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
2222
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
2323
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
2424
import org.jetbrains.kotlin.ir.types.defaultType
25+
import org.jetbrains.kotlin.ir.types.isNullable
2526
import org.jetbrains.kotlin.ir.types.isSubtypeOfClass
2627
import org.jetbrains.kotlin.ir.types.typeWith
2728
import org.jetbrains.kotlin.ir.util.isAnnotationWithEqualFqName
@@ -229,18 +230,22 @@ private fun generateTransformBodyForFunction(
229230
} else {
230231
// TODO scope safe cast
231232
val scopeType = coroutineScopeRef.defaultType
232-
// irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
233233

234-
val irSafeAs = IrTypeOperatorCallImpl(
235-
startOffset,
236-
endOffset,
237-
scopeType,
238-
IrTypeOperator.SAFE_CAST,
239-
scopeType,
240-
irGet(dispatchReceiverParameter)
241-
)
234+
if (getValueArgument(1)?.type?.isNullable() == true) {
235+
val irSafeAs = IrTypeOperatorCallImpl(
236+
startOffset,
237+
endOffset,
238+
scopeType,
239+
IrTypeOperator.SAFE_CAST,
240+
scopeType,
241+
irGet(dispatchReceiverParameter)
242+
)
243+
244+
putValueArgument(1, irSafeAs)
245+
}
246+
247+
// irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
242248

243-
putValueArgument(1, irSafeAs)
244249
}
245250
}
246251
}

0 commit comments

Comments
 (0)