From 5cd96831dc292e43d0e8872067150f8f3944c01a Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Wed, 16 Aug 2023 11:17:03 +0200 Subject: [PATCH] [Native] Move `isRestrictedSuspendFunction` from `IrUtils2` This function is also helpful for JVM backend. --- .../src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt | 5 ++--- .../kotlin/resolve/calls/checkers/coroutineCallChecker.kt | 3 +++ .../backend/konan/lower/NativeSuspendFunctionLowering.kt | 8 ++++++-- .../src/org/jetbrains/kotlin/ir/util/IrUtils2.kt | 4 ---- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt index 105a147bd13..62e8dd746a5 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.descriptors.impl.MutableClassDescriptor import org.jetbrains.kotlin.descriptors.impl.MutablePackageFragmentDescriptor import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.calls.checkers.isRestrictedSuspendFunction import org.jetbrains.kotlin.resolve.calls.checkers.isRestrictsSuspensionReceiver import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.storage.LockBasedStorageManager @@ -108,9 +109,7 @@ class JvmRuntimeTypes( if (descriptor.isSuspend) { return mutableListOf().apply { - if (actualFunctionDescriptor.extensionReceiverParameter?.type - ?.isRestrictsSuspensionReceiver() == true - ) { + if (actualFunctionDescriptor.isRestrictedSuspendFunction()) { if (descriptor.isSuspendLambdaOrLocalFunction()) { add(restrictedSuspendLambda.defaultType) } else { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt index 1a82280a537..ff7750cda19 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt @@ -144,6 +144,9 @@ fun KotlinType.isRestrictsSuspensionReceiver() = (listOf(this) + this.supertypes ) == true } +fun FunctionDescriptor.isRestrictedSuspendFunction(): Boolean = + extensionReceiverParameter?.type?.isRestrictsSuspensionReceiver() == true + private fun checkRestrictsSuspension( enclosingSuspendCallableDescriptor: CallableDescriptor, resolvedCall: ResolvedCall<*>, diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt index edf9a940a5d..11de26d0d97 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrStatement +import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.builders.irCall import org.jetbrains.kotlin.ir.declarations.* @@ -26,6 +27,8 @@ import org.jetbrains.kotlin.ir.types.makeNotNull import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.* import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.calls.checkers.isRestrictedSuspendFunction +import org.jetbrains.kotlin.resolve.calls.checkers.isRestrictsSuspensionReceiver internal class NativeSuspendFunctionsLowering( generationState: NativeGenerationState @@ -35,12 +38,13 @@ internal class NativeSuspendFunctionsLowering( override val stateMachineMethodName = Name.identifier("invokeSuspend") + @OptIn(ObsoleteDescriptorBasedAPI::class) override fun getCoroutineBaseClass(function: IrFunction): IrClassSymbol = - (if (function.isRestrictedSuspendFunction()) { + if (function.descriptor.isRestrictedSuspendFunction()) { symbols.restrictedContinuationImpl } else { symbols.continuationImpl - }) + } override fun nameForCoroutineClass(function: IrFunction) = fileLowerState.getCoroutineImplUniqueName(function).synthesizedName diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt index 873ea3941dd..f1815aab8b6 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt @@ -75,10 +75,6 @@ fun IrBuilderWithScope.irCatch() = ) ) -@OptIn(ObsoleteDescriptorBasedAPI::class) -fun IrFunction.isRestrictedSuspendFunction(): Boolean = - this.descriptor.extensionReceiverParameter?.type?.isRestrictsSuspensionReceiver() == true - fun IrBuilderWithScope.irByte(value: Byte) = IrConstImpl.byte(startOffset, endOffset, context.irBuiltIns.byteType, value)