From bc3b791cd833c83a51c0b09723acf6f8521007ac Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 26 Jan 2021 17:21:44 +0300 Subject: [PATCH] [TMP] Remove :kotlin-coroutines-experimental-compat module --- build.gradle.kts | 6 +- .../forTestCompile/ForTestCompileRuntime.java | 5 - ...bstractCompileKotlinAgainstKotlinTest.java | 5 +- .../kotlin/codegen/CodegenTestCase.java | 16 - .../jetbrains/kotlin/test/KotlinBaseTest.kt | 7 - idea/idea-gradle/build.gradle.kts | 3 +- ...thJdkAndRuntimeLightProjectDescriptor.java | 3 +- .../coroutines-experimental/build.gradle.kts | 101 ------ .../experimental/SafeContinuationJvm.kt | 81 ----- .../experimental/intrinsics/IntrinsicsJvm.kt | 119 ------- .../jvm/internal/CoroutineImpl.kt | 61 ---- .../jvm/internal/CoroutineIntrinsics.kt | 23 -- .../migration/CoroutinesMigration.kt | 144 --------- .../jvm/test/CoroutineContextTest.kt | 163 ---------- .../jvm/test/CoroutinesReferenceValuesTest.kt | 60 ---- .../jvm/test/SequenceBuilderTest.kt | 295 ------------------ .../coroutines-experimental/jvm/test/utils.kt | 15 - .../experimental/ContinuationInterceptor.kt | 30 -- .../experimental/CoroutineContext.kt | 79 ----- .../experimental/CoroutineContextImpl.kt | 88 ------ .../coroutines/experimental/Coroutines.kt | 38 --- .../experimental/CoroutinesExperimentalH.kt | 21 -- .../CoroutinesIntrinsicsExperimentalH.kt | 44 --- .../experimental/CoroutinesLibrary.kt | 117 ------- .../experimental/SequenceBuilder.kt | 177 ----------- .../experimental/intrinsics/Intrinsics.kt | 61 ---- libraries/stdlib/jdk7/build.gradle | 1 - libraries/stdlib/jdk8/build.gradle | 1 - libraries/stdlib/jvm/build.gradle | 1 - .../coroutines/CoroutinesMigrationTest.kt | 54 ---- .../build.gradle | 1 - .../RuntimePublicAPITest.kt | 5 - prepare/compiler/build.gradle.kts | 4 +- prepare/idea-plugin/build.gradle.kts | 1 - settings.gradle | 2 - 35 files changed, 6 insertions(+), 1826 deletions(-) delete mode 100644 libraries/stdlib/coroutines-experimental/build.gradle.kts delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/SafeContinuationJvm.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/intrinsics/IntrinsicsJvm.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineImpl.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineIntrinsics.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/migration/CoroutinesMigration.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/test/CoroutineContextTest.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/test/CoroutinesReferenceValuesTest.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/test/SequenceBuilderTest.kt delete mode 100644 libraries/stdlib/coroutines-experimental/jvm/test/utils.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/ContinuationInterceptor.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContext.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContextImpl.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/Coroutines.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesExperimentalH.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesIntrinsicsExperimentalH.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesLibrary.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/SequenceBuilder.kt delete mode 100644 libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/intrinsics/Intrinsics.kt delete mode 100644 libraries/stdlib/jvm/test/coroutines/CoroutinesMigrationTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index b7e4f0a0c5e..bd6351e925b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -333,8 +333,7 @@ val coreLibProjects = listOfNotNull( ":kotlin-test:kotlin-test-junit5", ":kotlin-test:kotlin-test-testng", ":kotlin-test:kotlin-test-js".takeIf { !kotlinBuildProperties.isInJpsBuildIdeaSync }, - ":kotlin-reflect", - ":kotlin-coroutines-experimental-compat" + ":kotlin-reflect" ) val gradlePluginProjects = listOf( @@ -944,8 +943,7 @@ tasks { ":kotlin-reflect:publish", ":kotlin-main-kts:publish", ":kotlin-stdlib-js:publish", - ":kotlin-test:kotlin-test-js:publish", - ":kotlin-coroutines-experimental-compat:publish" + ":kotlin-test:kotlin-test-js:publish" ) } } diff --git a/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java b/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java index 9aacf91e1b5..13d69085b66 100644 --- a/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java +++ b/compiler/test-infrastructure-utils/tests/org/jetbrains/kotlin/codegen/forTestCompile/ForTestCompileRuntime.java @@ -100,11 +100,6 @@ public class ForTestCompileRuntime { return assertExists(new File("dist/kotlinc/lib/kotlin-annotations-android.jar")); } - @NotNull - public static File coroutinesCompatForTests() { - return assertExists(new File("dist/kotlinc/lib/kotlin-coroutines-experimental-compat.jar")); - } - @NotNull private static File assertExists(@NotNull File file) { if (!file.exists()) { diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractCompileKotlinAgainstKotlinTest.java b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractCompileKotlinAgainstKotlinTest.java index d52ba5ffc9f..3ab66e720f3 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractCompileKotlinAgainstKotlinTest.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/AbstractCompileKotlinAgainstKotlinTest.java @@ -93,10 +93,7 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends CodegenTest @NotNull private URLClassLoader createGeneratedClassLoader() throws Exception { return new URLClassLoader( - new URL[]{ - bDir.toURI().toURL(), aDir.toURI().toURL(), - ForTestCompileRuntime.coroutinesCompatForTests().toURI().toURL() - }, + new URL[]{bDir.toURI().toURL(), aDir.toURI().toURL()}, ForTestCompileRuntime.runtimeAndReflectJarClassLoader() ); } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java index 6417bfe459e..383b6e5e941 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java @@ -235,7 +235,6 @@ public abstract class CodegenTestCase extends KotlinBaseTest getExtraDependenciesFromKotlinCompileClasspath() { - List includeFromCompileClasspath = CollectionsKt.listOf( - ForTestCompileRuntime.coroutinesCompatForTests() - ); - List compileClasspath = - CollectionsKt.map( - CollectionsKt.filterIsInstance( - myEnvironment.getConfiguration().get(CLIConfigurationKeys.CONTENT_ROOTS), - JvmClasspathRoot.class), - JvmClasspathRoot::getFile); - return CollectionsKt.intersect(compileClasspath, includeFromCompileClasspath); - } - - @NotNull protected String generateToText() { return generateToText(null); @@ -483,7 +468,6 @@ public abstract class CodegenTestCase extends KotlinBaseTest : KtUsefulTestCase() usePreparsedDirectives: Boolean ) { var explicitLanguageVersionSettings: LanguageVersionSettings? = null - var includeCompatExperimentalCoroutines = false val kotlinConfigurationFlags: MutableList = ArrayList(0) for (testFile in testFilesWithConfigurationDirectives) { val content = testFile.content @@ -171,18 +170,12 @@ abstract class KotlinBaseTest : KtUsefulTestCase() """.trimIndent() ) } - if (content.contains(StandardNames.COROUTINES_PACKAGE_FQ_NAME_EXPERIMENTAL.asString())) { - includeCompatExperimentalCoroutines = true - } val fileLanguageVersionSettings: LanguageVersionSettings? = parseLanguageVersionSettings(directives) if (fileLanguageVersionSettings != null) { assert(explicitLanguageVersionSettings == null) { "Should not specify !LANGUAGE directive twice" } explicitLanguageVersionSettings = fileLanguageVersionSettings } } - if (includeCompatExperimentalCoroutines) { - configuration.addJvmClasspathRoot(ForTestCompileRuntime.coroutinesCompatForTests()) - } if (explicitLanguageVersionSettings != null) { configuration.languageVersionSettings = explicitLanguageVersionSettings } diff --git a/idea/idea-gradle/build.gradle.kts b/idea/idea-gradle/build.gradle.kts index 5220535504b..0a54813ac45 100644 --- a/idea/idea-gradle/build.gradle.kts +++ b/idea/idea-gradle/build.gradle.kts @@ -24,7 +24,6 @@ dependencies { compileOnly(intellijPluginDep("Groovy")) compileOnly(intellijPluginDep("junit")) compileOnly(intellijPluginDep("testng")) - runtimeOnly(project(":kotlin-coroutines-experimental-compat")) compileOnly(project(":kotlin-gradle-statistics")) @@ -125,4 +124,4 @@ if (Ide.AS41.orHigher()) { getOrCreateTask("test") { setExcludes(listOf("**")) } -} \ No newline at end of file +} diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinWithJdkAndRuntimeLightProjectDescriptor.java b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinWithJdkAndRuntimeLightProjectDescriptor.java index 16a1fc22ded..f2a6dc345e8 100644 --- a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinWithJdkAndRuntimeLightProjectDescriptor.java +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinWithJdkAndRuntimeLightProjectDescriptor.java @@ -40,8 +40,7 @@ public class KotlinWithJdkAndRuntimeLightProjectDescriptor extends KotlinJdkAndL @NotNull public static final KotlinWithJdkAndRuntimeLightProjectDescriptor INSTANCE = new KotlinWithJdkAndRuntimeLightProjectDescriptor( - Arrays.asList(ForTestCompileRuntime.runtimeJarForTests(), - ForTestCompileRuntime.coroutinesCompatForTests()) + Arrays.asList(ForTestCompileRuntime.runtimeJarForTests()) ); public static KotlinWithJdkAndRuntimeLightProjectDescriptor getInstance(LanguageLevel level) { diff --git a/libraries/stdlib/coroutines-experimental/build.gradle.kts b/libraries/stdlib/coroutines-experimental/build.gradle.kts deleted file mode 100644 index 58cbeb5a486..00000000000 --- a/libraries/stdlib/coroutines-experimental/build.gradle.kts +++ /dev/null @@ -1,101 +0,0 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - -plugins { - kotlin("jvm") -} - -publish() - -description = "Kotlin pre 1.3 experimental coroutines compatibility library" - -jvmTarget = "1.6" -javaHome = rootProject.extra["JDK_16"] as String - - -sourceSets { - "main" { - java.srcDirs("src", "jvm/src") - } - "test" { - java.srcDirs("jvm/test") - } - "migrationTest" { - if(!kotlinBuildProperties.isInIdeaSync) - java.srcDirs("jvm/test") - } -} -val migrationTestSourceSet = sourceSets["migrationTest"] - -configurations { - "migrationTestImplementation" { - extendsFrom(testImplementation.get()) - } -} - -dependencies { - api(kotlinStdlib()) - testImplementation(project(":kotlin-test:kotlin-test-junit")) - "migrationTestImplementation"(sourceSets.main.get().output) -} - -tasks { - val compileKotlin by existing(KotlinCompile::class) { - kotlinOptions { - languageVersion = "1.3" - apiVersion = "1.3" - freeCompilerArgs = listOf( - "-Xmulti-platform", - "-Xallow-kotlin-package", - "-Xmultifile-parts-inherit", - "-Xopt-in=kotlin.RequiresOptIn", - "-Xopt-in=kotlin.contracts.ExperimentalContracts", - "-Xcoroutines=enable", - "-XXLanguage:-ReleaseCoroutines", - "-Xno-use-ir", - "-Xuse-old-backend" - ) - moduleName = "kotlin-coroutines-experimental-compat" - } - } - val compileTestKotlin by existing(KotlinCompile::class) { - kotlinOptions { - languageVersion = "1.2" - apiVersion = "1.2" - freeCompilerArgs = listOf( - "-Xcoroutines=enable", - "-Xno-use-ir", - "-Xuse-old-backend" - ) - } - } - val compileMigrationTestKotlin by existing(KotlinCompile::class) { - kotlinOptions { - languageVersion = "1.3" - apiVersion = "1.3" - freeCompilerArgs = listOf( - "-Xno-use-ir", - "-Xuse-old-backend" - ) - } - } - - val migrationTestClasses by existing - val migrationTest by creating(Test::class) { - dependsOn(migrationTestClasses) - group = "verification" - testClassesDirs = migrationTestSourceSet.output.classesDirs - classpath = migrationTestSourceSet.runtimeClasspath - } - - val check by existing { - dependsOn(migrationTest) - } - - - val jar by existing(Jar::class) { - callGroovy("manifestAttributes", manifest, project, "Main") - } -} - -sourcesJar() -javadocJar() diff --git a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/SafeContinuationJvm.kt b/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/SafeContinuationJvm.kt deleted file mode 100644 index 2a6cfb915d8..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/SafeContinuationJvm.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental - -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater -import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED - -@PublishedApi -internal actual class SafeContinuation -internal actual constructor( - private val delegate: Continuation, - initialResult: Any? -) : Continuation { - - @PublishedApi - internal actual constructor(delegate: Continuation) : this(delegate, UNDECIDED) - - public actual override val context: CoroutineContext - get() = delegate.context - - @Volatile - private var result: Any? = initialResult - - companion object { - private val UNDECIDED: Any? = Any() - private val RESUMED: Any? = Any() - - @Suppress("UNCHECKED_CAST") - @JvmStatic - private val RESULT = AtomicReferenceFieldUpdater.newUpdater, Any?>( - SafeContinuation::class.java, Any::class.java as Class, "result" - ) - } - - private class Fail(val exception: Throwable) - - actual override fun resume(value: T) { - while (true) { // lock-free loop - val result = this.result // atomic read - when { - result === UNDECIDED -> if (RESULT.compareAndSet(this, UNDECIDED, value)) return - result === COROUTINE_SUSPENDED -> if (RESULT.compareAndSet(this, COROUTINE_SUSPENDED, RESUMED)) { - delegate.resume(value) - return - } - else -> throw IllegalStateException("Already resumed") - } - } - } - - actual override fun resumeWithException(exception: Throwable) { - while (true) { // lock-free loop - val result = this.result // atomic read - when { - result === UNDECIDED -> if (RESULT.compareAndSet(this, UNDECIDED, Fail(exception))) return - result === COROUTINE_SUSPENDED -> if (RESULT.compareAndSet(this, COROUTINE_SUSPENDED, RESUMED)) { - delegate.resumeWithException(exception) - return - } - else -> throw IllegalStateException("Already resumed") - } - } - } - - @PublishedApi - internal actual fun getResult(): Any? { - var result = this.result // atomic read - if (result === UNDECIDED) { - if (RESULT.compareAndSet(this, UNDECIDED, COROUTINE_SUSPENDED)) return COROUTINE_SUSPENDED - result = this.result // reread volatile var - } - when { - result === RESUMED -> return COROUTINE_SUSPENDED // already called continuation, indicate COROUTINE_SUSPENDED upstream - result is Fail -> throw result.exception - else -> return result // either COROUTINE_SUSPENDED or data - } - } -} diff --git a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/intrinsics/IntrinsicsJvm.kt b/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/intrinsics/IntrinsicsJvm.kt deleted file mode 100644 index e9a5d9296ed..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/intrinsics/IntrinsicsJvm.kt +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -@file:kotlin.jvm.JvmName("IntrinsicsKt") -@file:kotlin.jvm.JvmMultifileClass -@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") - -package kotlin.coroutines.experimental.intrinsics - -import kotlin.coroutines.experimental.* - -/** - * Starts unintercepted coroutine without receiver and with result type [T] and executes it until its first suspension. - * Returns the result of the coroutine or throws its exception if it does not suspend or [COROUTINE_SUSPENDED] if it suspends. - * In the latter case, the [completion] continuation is invoked when coroutine completes with result or exception. - * This function is designed to be used from inside of [suspendCoroutineOrReturn] to resume the execution of a suspended - * coroutine using a reference to the suspending function. - */ -@SinceKotlin("1.1") -@Suppress("UNCHECKED_CAST") -@kotlin.internal.InlineOnly -public actual inline fun (suspend () -> T).startCoroutineUninterceptedOrReturn( - completion: Continuation -): Any? = (this as Function1, Any?>).invoke(completion) - -/** - * Starts unintercepted coroutine with receiver type [R] and result type [T] and executes it until its first suspension. - * Returns the result of the coroutine or throws its exception if it does not suspend or [COROUTINE_SUSPENDED] if it suspends. - * In the latter case, the [completion] continuation is invoked when coroutine completes with result or exception. - * This function is designed to be used from inside of [suspendCoroutineOrReturn] to resume the execution of a suspended - * coroutine using a reference to the suspending function. - */ -@SinceKotlin("1.1") -@Suppress("UNCHECKED_CAST") -@kotlin.internal.InlineOnly -public actual inline fun (suspend R.() -> T).startCoroutineUninterceptedOrReturn( - receiver: R, - completion: Continuation -): Any? = (this as Function2, Any?>).invoke(receiver, completion) - - -// JVM declarations - -/** - * Creates a coroutine without receiver and with result type [T]. - * This function creates a new, fresh instance of suspendable computation every time it is invoked. - * - * To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance. - * The [completion] continuation is invoked when coroutine completes with result or exception. - * - * This function is _unchecked_. Repeated invocation of any resume function on the resulting continuation corrupts the - * state machine of the coroutine and may result in arbitrary behaviour or exception. - */ -@SinceKotlin("1.1") -public actual fun (suspend () -> T).createCoroutineUnchecked( - completion: Continuation -): Continuation = - if (this !is kotlin.coroutines.experimental.jvm.internal.CoroutineImpl) - buildContinuationByInvokeCall(completion) { - @Suppress("UNCHECKED_CAST") - (this as Function1, Any?>).invoke(completion) - } - else - (this.create(completion) as kotlin.coroutines.experimental.jvm.internal.CoroutineImpl).facade - -/** - * Creates a coroutine with receiver type [R] and result type [T]. - * This function creates a new, fresh instance of suspendable computation every time it is invoked. - * - * To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance. - * The [completion] continuation is invoked when coroutine completes with result or exception. - * - * This function is _unchecked_. Repeated invocation of any resume function on the resulting continuation corrupts the - * state machine of the coroutine and may result in arbitrary behaviour or exception. - */ -@SinceKotlin("1.1") -public actual fun (suspend R.() -> T).createCoroutineUnchecked( - receiver: R, - completion: Continuation -): Continuation = - if (this !is kotlin.coroutines.experimental.jvm.internal.CoroutineImpl) - buildContinuationByInvokeCall(completion) { - @Suppress("UNCHECKED_CAST") - (this as Function2, Any?>).invoke(receiver, completion) - } - else - (this.create(receiver, completion) as kotlin.coroutines.experimental.jvm.internal.CoroutineImpl).facade - -// INTERNAL DEFINITIONS - -private inline fun buildContinuationByInvokeCall( - completion: Continuation, - crossinline block: () -> Any? -): Continuation { - val continuation = - object : Continuation { - override val context: CoroutineContext - get() = completion.context - - override fun resume(value: Unit) { - processBareContinuationResume(completion, block) - } - - override fun resumeWithException(exception: Throwable) { - completion.resumeWithException(exception) - } - } - - return kotlin.coroutines.experimental.jvm.internal.interceptContinuationIfNeeded(completion.context, continuation) -} - -/** - * This value is used as a return value of [suspendCoroutineOrReturn] `block` argument to state that - * the execution was suspended and will not return any result immediately. - */ -@SinceKotlin("1.1") -public actual val COROUTINE_SUSPENDED: Any get() = kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED diff --git a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineImpl.kt b/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineImpl.kt deleted file mode 100644 index 4c4d01fc82b..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineImpl.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental.jvm.internal - -import java.lang.IllegalStateException -import kotlin.coroutines.experimental.Continuation -import kotlin.coroutines.experimental.CoroutineContext -import kotlin.coroutines.experimental.processBareContinuationResume -import kotlin.jvm.internal.Lambda - -/** - * @suppress - */ -abstract class CoroutineImpl( - arity: Int, - @JvmField - protected var completion: Continuation? -) : Lambda(arity), Continuation { - - // label == -1 when coroutine cannot be started (it is just a factory object) or has already finished execution - // label == 0 in initial part of the coroutine - @JvmField - protected var label: Int = if (completion != null) 0 else -1 - - private val _context: CoroutineContext? = completion?.context - - override val context: CoroutineContext - get() = _context!! - - private var _facade: Continuation? = null - - val facade: Continuation get() { - if (_facade == null) _facade = interceptContinuationIfNeeded(_context!!, this) - return _facade!! - } - - override fun resume(value: Any?) { - processBareContinuationResume(completion!!) { - doResume(value, null) - } - } - - override fun resumeWithException(exception: Throwable) { - processBareContinuationResume(completion!!) { - doResume(null, exception) - } - } - - protected abstract fun doResume(data: Any?, exception: Throwable?): Any? - - open fun create(completion: Continuation<*>): Continuation { - throw IllegalStateException("create(Continuation) has not been overridden") - } - - open fun create(value: Any?, completion: Continuation<*>): Continuation { - throw IllegalStateException("create(Any?;Continuation) has not been overridden") - } -} diff --git a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineIntrinsics.kt b/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineIntrinsics.kt deleted file mode 100644 index b33c822c318..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/jvm/internal/CoroutineIntrinsics.kt +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -@file:JvmName("CoroutineIntrinsics") - -package kotlin.coroutines.experimental.jvm.internal - -import kotlin.coroutines.experimental.Continuation -import kotlin.coroutines.experimental.ContinuationInterceptor -import kotlin.coroutines.experimental.CoroutineContext - -/** - * @suppress - */ -fun normalizeContinuation(continuation: Continuation): Continuation = - (continuation as? CoroutineImpl)?.facade ?: continuation - -internal fun interceptContinuationIfNeeded( - context: CoroutineContext, - continuation: Continuation -) = context[ContinuationInterceptor]?.interceptContinuation(continuation) ?: continuation diff --git a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/migration/CoroutinesMigration.kt b/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/migration/CoroutinesMigration.kt deleted file mode 100644 index e719df918b6..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/src/kotlin/coroutines/experimental/migration/CoroutinesMigration.kt +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental.migration - -import kotlin.coroutines.experimental.Continuation as ExperimentalContinuation -import kotlin.coroutines.experimental.CoroutineContext as ExperimentalCoroutineContext -import kotlin.coroutines.experimental.AbstractCoroutineContextElement as ExperimentalAbstractCoroutineContextElement -import kotlin.coroutines.experimental.EmptyCoroutineContext as ExperimentalEmptyCoroutineContext -import kotlin.coroutines.experimental.ContinuationInterceptor as ExperimentalContinuationInterceptor -import kotlin.coroutines.* -import kotlin.coroutines.intrinsics.* - - -/** - * Converts [Continuation] to [ExperimentalContinuation]. - */ -@SinceKotlin("1.3") -public fun Continuation.toExperimentalContinuation(): ExperimentalContinuation = - (this as? ContinuationMigration)?.continuation ?: ExperimentalContinuationMigration(this) - -/** - * Converts [ExperimentalContinuation] to [Continuation]. - */ -@SinceKotlin("1.3") -public fun ExperimentalContinuation.toContinuation(): Continuation = - (this as? ExperimentalContinuationMigration)?.continuation ?: ContinuationMigration(this) - -/** - * Converts [CoroutineContext] to [ExperimentalCoroutineContext]. - */ -@SinceKotlin("1.3") -public fun CoroutineContext.toExperimentalCoroutineContext(): ExperimentalCoroutineContext { - val interceptor = get(ContinuationInterceptor.Key) - val migration = get(ContextMigration.Key) - val remainder = minusKey(ContinuationInterceptor.Key).minusKey(ContextMigration.Key) - val original = migration?.context ?: ExperimentalEmptyCoroutineContext - val result = if (remainder === EmptyCoroutineContext) original else original + ExperimentalContextMigration(remainder) - return if (interceptor == null) result else result + interceptor.toExperimentalContinuationInterceptor() -} - -/** - * Converts [ExperimentalCoroutineContext] to [CoroutineContext]. - */ -@SinceKotlin("1.3") -public fun ExperimentalCoroutineContext.toCoroutineContext(): CoroutineContext { - val interceptor = get(ExperimentalContinuationInterceptor.Key) - val migration = get(ExperimentalContextMigration.Key) - val remainder = minusKey(ExperimentalContinuationInterceptor.Key).minusKey(ExperimentalContextMigration.Key) - val original = migration?.context ?: EmptyCoroutineContext - val result = if (remainder === ExperimentalEmptyCoroutineContext) original else original + ContextMigration(remainder) - return if (interceptor == null) result else result + interceptor.toContinuationInterceptor() -} - -/** - * Converts [ContinuationInterceptor] to [ExperimentalContinuationInterceptor]. - */ -@SinceKotlin("1.3") -public fun ContinuationInterceptor.toExperimentalContinuationInterceptor(): ExperimentalContinuationInterceptor = - (this as? ContinuationInterceptorMigration)?.interceptor ?: ExperimentalContinuationInterceptorMigration(this) - -/** - * Converts [ExperimentalContinuationInterceptor] to [ContinuationInterceptor]. - */ -@SinceKotlin("1.3") -public fun ExperimentalContinuationInterceptor.toContinuationInterceptor(): ContinuationInterceptor = - (this as? ExperimentalContinuationInterceptorMigration)?.interceptor ?: ContinuationInterceptorMigration(this) - -// ------------------ converter classes ------------------ -// Their name starts with "Experimental" if they implement the corresponding Experimental interfaces - -private class ExperimentalContinuationMigration(val continuation: Continuation): ExperimentalContinuation { - override val context = continuation.context.toExperimentalCoroutineContext() - override fun resume(value: T) = continuation.resume(value) - override fun resumeWithException(exception: Throwable) = continuation.resumeWithException(exception) -} - -private class ContinuationMigration(val continuation: ExperimentalContinuation): Continuation { - override val context: CoroutineContext = continuation.context.toCoroutineContext() - override fun resumeWith(result: Result) { - result - .onSuccess { continuation.resume(it) } - .onFailure { continuation.resumeWithException(it) } - } -} - -private class ExperimentalContextMigration(val context: CoroutineContext): ExperimentalAbstractCoroutineContextElement(Key) { - companion object Key : ExperimentalCoroutineContext.Key -} - -private class ContextMigration(val context: ExperimentalCoroutineContext): AbstractCoroutineContextElement(Key) { - companion object Key : CoroutineContext.Key -} - -private class ExperimentalContinuationInterceptorMigration(val interceptor: ContinuationInterceptor) : ExperimentalContinuationInterceptor { - override val key: ExperimentalCoroutineContext.Key<*> - get() = ExperimentalContinuationInterceptor.Key - - override fun interceptContinuation(continuation: ExperimentalContinuation): ExperimentalContinuation = - interceptor.interceptContinuation(continuation.toContinuation()).toExperimentalContinuation() -} - -private class ContinuationInterceptorMigration(val interceptor: ExperimentalContinuationInterceptor) : ContinuationInterceptor { - override val key: CoroutineContext.Key<*> - get() = ContinuationInterceptor.Key - - override fun interceptContinuation(continuation: Continuation): Continuation = - interceptor.interceptContinuation(continuation.toExperimentalContinuation()).toContinuation() -} - -internal fun ((Continuation) -> Any?).toExperimentalSuspendFunction(): (ExperimentalContinuation) -> Any? = - ExperimentalSuspendFunction0Migration(this) - -internal fun ((T1, Continuation) -> Any?).toExperimentalSuspendFunction(): (T1, ExperimentalContinuation) -> Any? = - ExperimentalSuspendFunction1Migration(this) - -internal fun ((T1, T2, Continuation) -> Any?).toExperimentalSuspendFunction(): (T1, T2, ExperimentalContinuation) -> Any? = - ExperimentalSuspendFunction2Migration(this) - -private class ExperimentalSuspendFunction0Migration( - val function: (Continuation) -> Any? -) : (ExperimentalContinuation) -> Any? { - override fun invoke(continuation: ExperimentalContinuation): Any? { - return function(continuation.toContinuation()) - } -} - -private class ExperimentalSuspendFunction1Migration( - val function: (T1, Continuation) -> Any? -) : (T1, ExperimentalContinuation) -> Any? { - override fun invoke(t1: T1, continuation: ExperimentalContinuation): Any? { - return function(t1, continuation.toContinuation()) - } -} - -private class ExperimentalSuspendFunction2Migration( - val function: (T1, T2, Continuation) -> Any? -) : (T1, T2, ExperimentalContinuation) -> Any? { - override fun invoke(t1: T1, t2: T2, continuation: ExperimentalContinuation): Any? { - return function(t1, t2, continuation.toContinuation()) - } -} diff --git a/libraries/stdlib/coroutines-experimental/jvm/test/CoroutineContextTest.kt b/libraries/stdlib/coroutines-experimental/jvm/test/CoroutineContextTest.kt deleted file mode 100644 index f441dbbff2f..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/test/CoroutineContextTest.kt +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package test.coroutines - -import kotlin.test.* -import kotlin.coroutines.experimental.* - -class CoroutineContextTest { - data class CtxA(val i: Int) : AbstractCoroutineContextElement(CtxA) { - companion object Key : CoroutineContext.Key - } - - data class CtxB(val i: Int) : AbstractCoroutineContextElement(CtxB) { - companion object Key : CoroutineContext.Key - } - - data class CtxC(val i: Int) : AbstractCoroutineContextElement(CtxC) { - companion object Key : CoroutineContext.Key - } - - object Disp1 : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor { - override fun interceptContinuation(continuation: Continuation): Continuation = continuation - override fun toString(): String = "Disp1" - } - - object Disp2 : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor { - override fun interceptContinuation(continuation: Continuation): Continuation = continuation - override fun toString(): String = "Disp2" - } - - @Test - fun testGetPlusFold() { - var ctx: CoroutineContext = EmptyCoroutineContext - assertContents(ctx) - assertEquals("EmptyCoroutineContext", ctx.toString()) - - ctx += CtxA(1) - assertContents(ctx, CtxA(1)) - assertEquals("CtxA(i=1)", ctx.toString()) - assertEquals(CtxA(1), ctx[CtxA]) - assertEquals(null, ctx[CtxB]) - assertEquals(null, ctx[CtxC]) - - ctx += CtxB(2) - assertContents(ctx, CtxA(1), CtxB(2)) - assertEquals("[CtxA(i=1), CtxB(i=2)]", ctx.toString()) - assertEquals(CtxA(1), ctx[CtxA]) - assertEquals(CtxB(2), ctx[CtxB]) - assertEquals(null, ctx[CtxC]) - - ctx += CtxC(3) - assertContents(ctx, CtxA(1), CtxB(2), CtxC(3)) - assertEquals("[CtxA(i=1), CtxB(i=2), CtxC(i=3)]", ctx.toString()) - assertEquals(CtxA(1), ctx[CtxA]) - assertEquals(CtxB(2), ctx[CtxB]) - assertEquals(CtxC(3), ctx[CtxC]) - - ctx += CtxB(4) - assertContents(ctx, CtxA(1), CtxC(3), CtxB(4)) - assertEquals("[CtxA(i=1), CtxC(i=3), CtxB(i=4)]", ctx.toString()) - assertEquals(CtxA(1), ctx[CtxA]) - assertEquals(CtxB(4), ctx[CtxB]) - assertEquals(CtxC(3), ctx[CtxC]) - - ctx += CtxA(5) - assertContents(ctx, CtxC(3), CtxB(4), CtxA(5)) - assertEquals("[CtxC(i=3), CtxB(i=4), CtxA(i=5)]", ctx.toString()) - assertEquals(CtxA(5), ctx[CtxA]) - assertEquals(CtxB(4), ctx[CtxB]) - assertEquals(CtxC(3), ctx[CtxC]) - } - - @Test - fun testMinusKey() { - var ctx: CoroutineContext = CtxA(1) + CtxB(2) + CtxC(3) - assertContents(ctx, CtxA(1), CtxB(2), CtxC(3)) - assertEquals("[CtxA(i=1), CtxB(i=2), CtxC(i=3)]", ctx.toString()) - - ctx = ctx.minusKey(CtxA) - assertContents(ctx, CtxB(2), CtxC(3)) - assertEquals("[CtxB(i=2), CtxC(i=3)]", ctx.toString()) - assertEquals(null, ctx[CtxA]) - assertEquals(CtxB(2), ctx[CtxB]) - assertEquals(CtxC(3), ctx[CtxC]) - - ctx = ctx.minusKey(CtxC) - assertContents(ctx, CtxB(2)) - assertEquals("CtxB(i=2)", ctx.toString()) - assertEquals(null, ctx[CtxA]) - assertEquals(CtxB(2), ctx[CtxB]) - assertEquals(null, ctx[CtxC]) - - ctx = ctx.minusKey(CtxC) - assertContents(ctx, CtxB(2)) - assertEquals("CtxB(i=2)", ctx.toString()) - assertEquals(null, ctx[CtxA]) - assertEquals(CtxB(2), ctx[CtxB]) - assertEquals(null, ctx[CtxC]) - - ctx = ctx.minusKey(CtxB) - assertContents(ctx) - assertEquals("EmptyCoroutineContext", ctx.toString()) - assertEquals(null, ctx[CtxA]) - assertEquals(null, ctx[CtxB]) - assertEquals(null, ctx[CtxC]) - - assertEquals(EmptyCoroutineContext, ctx) - } - - @Test - fun testPlusCombined() { - val ctx1 = CtxA(1) + CtxB(2) - val ctx2 = CtxB(3) + CtxC(4) - val ctx = ctx1 + ctx2 - assertContents(ctx, CtxA(1), CtxB(3), CtxC(4)) - assertEquals("[CtxA(i=1), CtxB(i=3), CtxC(i=4)]", ctx.toString()) - assertEquals(CtxA(1), ctx[CtxA]) - assertEquals(CtxB(3), ctx[CtxB]) - assertEquals(CtxC(4), ctx[CtxC]) - } - - @Test - fun testLastDispatcher() { - var ctx: CoroutineContext = EmptyCoroutineContext - assertContents(ctx) - ctx += CtxA(1) - assertContents(ctx, CtxA(1)) - ctx += Disp1 - assertContents(ctx, CtxA(1), Disp1) - ctx += CtxA(2) - assertContents(ctx, CtxA(2), Disp1) - ctx += CtxB(3) - assertContents(ctx, CtxA(2), CtxB(3), Disp1) - ctx += Disp2 - assertContents(ctx, CtxA(2), CtxB(3), Disp2) - ctx += (CtxB(4) + CtxC(5)) - assertContents(ctx, CtxA(2), CtxB(4), CtxC(5), Disp2) - } - - @Test - fun testEquals() { - val ctx1 = CtxA(1) + CtxB(2) + CtxC(3) - val ctx2 = CtxB(2) + CtxC(3) + CtxA(1) // same - val ctx3 = CtxC(3) + CtxA(1) + CtxB(2) // same - val ctx4 = CtxA(1) + CtxB(2) + CtxC(4) // different - assertEquals(ctx1, ctx2) - assertEquals(ctx1, ctx3) - assertEquals(ctx2, ctx3) - assertNotEquals(ctx1, ctx4) - assertNotEquals(ctx2, ctx4) - assertNotEquals(ctx3, ctx4) - } - - private fun assertContents(ctx: CoroutineContext, vararg elements: CoroutineContext.Element) { - val set = ctx.fold(setOf()) { a, b -> a + b } - assertEquals(listOf(*elements), set.toList()) - for (elem in elements) - assertTrue(ctx[elem.key] == elem) - } -} diff --git a/libraries/stdlib/coroutines-experimental/jvm/test/CoroutinesReferenceValuesTest.kt b/libraries/stdlib/coroutines-experimental/jvm/test/CoroutinesReferenceValuesTest.kt deleted file mode 100644 index 2fecc0446fb..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/test/CoroutinesReferenceValuesTest.kt +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package test.coroutines - -import test.assertStaticAndRuntimeTypeIs -import kotlin.test.* -import kotlin.coroutines.experimental.* - -/** - * Test to ensure that coroutine machinery does not call equals/hashCode/toString anywhere. - */ -class CoroutinesReferenceValuesTest { - class BadClass { - override fun equals(other: Any?): Boolean = error("equals") - override fun hashCode(): Int = error("hashCode") - override fun toString(): String = error("toString") - } - - var counter = 0 - - // tail-suspend function via suspendCoroutine (test SafeContinuation) - suspend fun getBadClassViaSuspend(): BadClass = suspendCoroutine { cont -> - counter++ - cont.resume(BadClass()) - } - - // state machine - suspend fun checkBadClassTwice() { - assertStaticAndRuntimeTypeIs(getBadClassViaSuspend()) - assertStaticAndRuntimeTypeIs(getBadClassViaSuspend()) - } - - fun suspend(block: suspend () -> T) = block - - @Test - fun testBadClass() { - val bad = suspend { - checkBadClassTwice() - getBadClassViaSuspend() - } - var result: BadClass? = null - bad.startCoroutine(object : Continuation { - override val context: CoroutineContext = EmptyCoroutineContext - - override fun resume(value: BadClass) { - assertTrue(result == null) - result = value - } - - override fun resumeWithException(exception: Throwable) { - throw exception - } - }) - assertTrue(result is BadClass) - assertEquals(3, counter) - } -} \ No newline at end of file diff --git a/libraries/stdlib/coroutines-experimental/jvm/test/SequenceBuilderTest.kt b/libraries/stdlib/coroutines-experimental/jvm/test/SequenceBuilderTest.kt deleted file mode 100644 index e0ddceffc96..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/test/SequenceBuilderTest.kt +++ /dev/null @@ -1,295 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -@file:Suppress("ObsoleteExperimentalCoroutines", "DEPRECATION") - -package test.coroutines - -import kotlin.test.* -import kotlin.coroutines.experimental.buildSequence -import kotlin.coroutines.experimental.buildIterator - -class SequenceBuilderTest { - @Test - fun testSimple() { - val result = buildSequence { - for (i in 1..3) { - yield(2 * i) - } - } - - assertEquals(listOf(2, 4, 6), result.toList()) - // Repeated calls also work - assertEquals(listOf(2, 4, 6), result.toList()) - } - - @Test - fun testCallHasNextSeveralTimes() { - val result = buildSequence { - yield(1) - } - - val iterator = result.iterator() - - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasNext()) - - assertEquals(1, iterator.next()) - - assertFalse(iterator.hasNext()) - assertFalse(iterator.hasNext()) - assertFalse(iterator.hasNext()) - - assertFailsWith { iterator.next() } - } - - @Test - fun testManualIteration() { - val result = buildSequence { - yield(1) - yield(2) - yield(3) - } - - val iterator = result.iterator() - - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasNext()) - assertEquals(1, iterator.next()) - - assertTrue(iterator.hasNext()) - assertTrue(iterator.hasNext()) - assertEquals(2, iterator.next()) - - assertEquals(3, iterator.next()) - - assertFalse(iterator.hasNext()) - assertFalse(iterator.hasNext()) - - assertFailsWith { iterator.next() } - - assertEquals(1, result.iterator().next()) - } - - @Test - fun testEmptySequence() { - val result = buildSequence {} - val iterator = result.iterator() - - assertFalse(iterator.hasNext()) - assertFalse(iterator.hasNext()) - - assertFailsWith { iterator.next() } - } - - @Test - fun testLaziness() { - var sharedVar = -2 - val result = buildSequence { - while (true) { - when (sharedVar) { - -1 -> return@buildSequence - -2 -> error("Invalid state: -2") - else -> yield(sharedVar) - } - } - } - - val iterator = result.iterator() - - sharedVar = 1 - assertTrue(iterator.hasNext()) - assertEquals(1, iterator.next()) - - sharedVar = 2 - assertTrue(iterator.hasNext()) - assertEquals(2, iterator.next()) - - sharedVar = 3 - assertTrue(iterator.hasNext()) - assertEquals(3, iterator.next()) - - sharedVar = -1 - assertFalse(iterator.hasNext()) - assertFailsWith { iterator.next() } - } - - @Test - fun testExceptionInCoroutine() { - var sharedVar = -2 - val result = buildSequence { - while (true) { - when (sharedVar) { - -1 -> return@buildSequence - -2 -> throw UnsupportedOperationException("-2 is unsupported") - else -> yield(sharedVar) - } - } - } - - val iterator = result.iterator() - - sharedVar = 1 - assertEquals(1, iterator.next()) - - sharedVar = -2 - assertFailsWith { iterator.hasNext() } - assertFailsWith { iterator.hasNext() } - assertFailsWith { iterator.next() } - } - - @Test - fun testParallelIteration() { - var inc = 0 - val result = buildSequence { - for (i in 1..3) { - inc++ - yield(inc * i) - } - } - - assertEquals(listOf(Pair(1, 2), Pair(6, 8), Pair(15, 18)), result.zip(result).toList()) - } - - @Test - fun testYieldAllIterator() { - val result = buildSequence { - yieldAll(listOf(1, 2, 3).iterator()) - } - assertEquals(listOf(1, 2, 3), result.toList()) - } - - @Test - fun testYieldAllSequence() { - val result = buildSequence { - yieldAll(sequenceOf(1, 2, 3)) - } - assertEquals(listOf(1, 2, 3), result.toList()) - } - - @Test - fun testYieldAllCollection() { - val result = buildSequence { - yieldAll(listOf(1, 2, 3)) - } - assertEquals(listOf(1, 2, 3), result.toList()) - } - - @Test - fun testYieldAllCollectionMixedFirst() { - val result = buildSequence { - yield(0) - yieldAll(listOf(1, 2, 3)) - } - assertEquals(listOf(0, 1, 2, 3), result.toList()) - } - - @Test - fun testYieldAllCollectionMixedLast() { - val result = buildSequence { - yieldAll(listOf(1, 2, 3)) - yield(4) - } - assertEquals(listOf(1, 2, 3, 4), result.toList()) - } - - @Test - fun testYieldAllCollectionMixedBoth() { - val result = buildSequence { - yield(0) - yieldAll(listOf(1, 2, 3)) - yield(4) - } - assertEquals(listOf(0, 1, 2, 3, 4), result.toList()) - } - - @Test - fun testYieldAllCollectionMixedLong() { - val result = buildSequence { - yield(0) - yieldAll(listOf(1, 2, 3)) - yield(4) - yield(5) - yieldAll(listOf(6)) - yield(7) - yieldAll(listOf()) - yield(8) - } - assertEquals(listOf(0, 1, 2, 3, 4, 5, 6, 7, 8), result.toList()) - } - - @Test - fun testYieldAllCollectionOneEmpty() { - val result = buildSequence { - yieldAll(listOf()) - } - assertEquals(listOf(), result.toList()) - } - - @Test - fun testYieldAllCollectionManyEmpty() { - val result = buildSequence { - yieldAll(listOf()) - yieldAll(listOf()) - yieldAll(listOf()) - } - assertEquals(listOf(), result.toList()) - } - - @Test - fun testYieldAllSideEffects() { - val effects = arrayListOf() - val result = buildSequence { - effects.add("a") - yieldAll(listOf(1, 2)) - effects.add("b") - yieldAll(listOf()) - effects.add("c") - yieldAll(listOf(3)) - effects.add("d") - yield(4) - effects.add("e") - yieldAll(listOf()) - effects.add("f") - yield(5) - } - - for (res in result) { - effects.add("(") // marks step start - effects.add(res) - effects.add(")") // marks step end - } - assertEquals( - listOf( - "a", - "(", 1, ")", - "(", 2, ")", - "b", "c", - "(", 3, ")", - "d", - "(", 4, ")", - "e", "f", - "(", 5, ")" - ), - effects.toList() - ) - } - - @Test - fun testInfiniteYieldAll() { - val values = buildIterator { - while (true) { - yieldAll((1..5).map { it }) - } - } - - var sum = 0 - repeat(10) { - sum += values.next() //.also(::println) - } - assertEquals(30, sum) - } -} diff --git a/libraries/stdlib/coroutines-experimental/jvm/test/utils.kt b/libraries/stdlib/coroutines-experimental/jvm/test/utils.kt deleted file mode 100644 index d83a4ebe5d9..00000000000 --- a/libraries/stdlib/coroutines-experimental/jvm/test/utils.kt +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package test - -import kotlin.test.* - -inline fun assertStaticAndRuntimeTypeIs(value: T) { - @Suppress("USELESS_CAST") - if ((value as Any?) !is T) { - fail("Expected value $value to have ${T::class} type") - } -} \ No newline at end of file diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/ContinuationInterceptor.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/ContinuationInterceptor.kt deleted file mode 100644 index bf082e3072e..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/ContinuationInterceptor.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental - -/** - * Marks coroutine context element that intercepts coroutine continuations. - * The coroutines framework uses [ContinuationInterceptor.Key] to retrieve the interceptor and - * intercepts all coroutine continuations with [interceptContinuation] invocations. - */ -@SinceKotlin("1.1") -public interface ContinuationInterceptor : CoroutineContext.Element { - /** - * The key that defines *the* context interceptor. - */ - companion object Key : CoroutineContext.Key - - /** - * Returns continuation that wraps the original [continuation], thus intercepting all resumptions. - * This function is invoked by coroutines framework when needed and the resulting continuations are - * cached internally per each instance of the original [continuation]. - * - * By convention, implementations that install themselves as *the* interceptor in the context with - * the [Key] shall also scan the context for other element that implement [ContinuationInterceptor] interface - * and use their [interceptContinuation] functions, too. - */ - public fun interceptContinuation(continuation: Continuation): Continuation -} diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContext.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContext.kt deleted file mode 100644 index 2891e9cb03a..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContext.kt +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental - -/** - * Persistent context for the coroutine. It is an indexed set of [Element] instances. - * An indexed set is a mix between a set and a map. - * Every element in this set has a unique [Key]. Keys are compared _by reference_. - */ -@SinceKotlin("1.1") -public interface CoroutineContext { - /** - * Returns the element with the given [key] from this context or `null`. - * Keys are compared _by reference_, that is to get an element from the context the reference to its actual key - * object must be presented to this function. - */ - public operator fun get(key: Key): E? - - /** - * Accumulates entries of this context starting with [initial] value and applying [operation] - * from left to right to current accumulator value and each element of this context. - */ - public fun fold(initial: R, operation: (R, Element) -> R): R - - /** - * Returns a context containing elements from this context and elements from other [context]. - * The elements from this context with the same key as in the other one are dropped. - */ - public operator fun plus(context: CoroutineContext): CoroutineContext = - if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation - context.fold(this) { acc, element -> - val removed = acc.minusKey(element.key) - if (removed === EmptyCoroutineContext) element else { - // make sure interceptor is always last in the context (and thus is fast to get when present) - val interceptor = removed[ContinuationInterceptor] - if (interceptor == null) CombinedContext(removed, element) else { - val left = removed.minusKey(ContinuationInterceptor) - if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else - CombinedContext(CombinedContext(left, element), interceptor) - } - } - } - - /** - * Returns a context containing elements from this context, but without an element with - * the specified [key]. Keys are compared _by reference_, that is to remove an element from the context - * the reference to its actual key object must be presented to this function. - */ - public fun minusKey(key: Key<*>): CoroutineContext - - /** - * An element of the [CoroutineContext]. An element of the coroutine context is a singleton context by itself. - */ - public interface Element : CoroutineContext { - /** - * A key of this coroutine context element. - */ - public val key: Key<*> - - @Suppress("UNCHECKED_CAST") - public override operator fun get(key: Key): E? = - if (this.key === key) this as E else null - - public override fun fold(initial: R, operation: (R, Element) -> R): R = - operation(initial, this) - - public override fun minusKey(key: Key<*>): CoroutineContext = - if (this.key === key) EmptyCoroutineContext else this - } - - /** - * Key for the elements of [CoroutineContext]. [E] is a type of element with this key. - * Keys in the context are compared _by reference_. - */ - public interface Key -} diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContextImpl.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContextImpl.kt deleted file mode 100644 index ce34fd7437d..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutineContextImpl.kt +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental - -import kotlin.coroutines.experimental.CoroutineContext.* - -/** - * Base class for [CoroutineContext.Element] implementations. - */ -@SinceKotlin("1.1") -public abstract class AbstractCoroutineContextElement(public override val key: Key<*>) : Element - -/** - * An empty coroutine context. - */ -@SinceKotlin("1.1") -public object EmptyCoroutineContext : CoroutineContext { - public override fun get(key: Key): E? = null - public override fun fold(initial: R, operation: (R, Element) -> R): R = initial - public override fun plus(context: CoroutineContext): CoroutineContext = context - public override fun minusKey(key: Key<*>): CoroutineContext = this - public override fun hashCode(): Int = 0 - public override fun toString(): String = "EmptyCoroutineContext" -} - -//--------------------- internal impl --------------------- - -// this class is not exposed, but is hidden inside implementations -// this is a left-biased list, so that `plus` works naturally -internal class CombinedContext(val left: CoroutineContext, val element: Element) : CoroutineContext { - override fun get(key: Key): E? { - var cur = this - while (true) { - cur.element[key]?.let { return it } - val next = cur.left - if (next is CombinedContext) { - cur = next - } else { - return next[key] - } - } - } - - public override fun fold(initial: R, operation: (R, Element) -> R): R = - operation(left.fold(initial, operation), element) - - public override fun minusKey(key: Key<*>): CoroutineContext { - element[key]?.let { return left } - val newLeft = left.minusKey(key) - return when { - newLeft === left -> this - newLeft === EmptyCoroutineContext -> element - else -> CombinedContext(newLeft, element) - } - } - - private fun size(): Int = - if (left is CombinedContext) left.size() + 1 else 2 - - private fun contains(element: Element): Boolean = - get(element.key) == element - - private fun containsAll(context: CombinedContext): Boolean { - var cur = context - while (true) { - if (!contains(cur.element)) return false - val next = cur.left - if (next is CombinedContext) { - cur = next - } else { - return contains(next as Element) - } - } - } - - override fun equals(other: Any?): Boolean = - this === other || other is CombinedContext && other.size() == size() && other.containsAll(this) - - override fun hashCode(): Int = left.hashCode() + element.hashCode() - - override fun toString(): String = - "[" + fold("") { acc, element -> - if (acc.isEmpty()) element.toString() else acc + ", " + element - } + "]" -} diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/Coroutines.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/Coroutines.kt deleted file mode 100644 index 0238a465183..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/Coroutines.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental - -/** - * Interface representing a continuation after a suspension point that returns value of type `T`. - */ -@SinceKotlin("1.1") -public interface Continuation { - /** - * Context of the coroutine that corresponds to this continuation. - */ - public val context: CoroutineContext - - /** - * Resumes the execution of the corresponding coroutine passing [value] as the return value of the last suspension point. - */ - public fun resume(value: T) - - /** - * Resumes the execution of the corresponding coroutine so that the [exception] is re-thrown right after the - * last suspension point. - */ - public fun resumeWithException(exception: Throwable) -} - -/** - * Classes and interfaces marked with this annotation are restricted when used as receivers for extension - * `suspend` functions. These `suspend` extensions can only invoke other member or extension `suspend` functions on this particular - * receiver only and are restricted from calling arbitrary suspension functions. - */ -@SinceKotlin("1.1") -@Target(AnnotationTarget.CLASS) -@Retention(AnnotationRetention.BINARY) -public annotation class RestrictsSuspension diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesExperimentalH.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesExperimentalH.kt deleted file mode 100644 index ca614d7f794..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesExperimentalH.kt +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental - -@PublishedApi -internal expect class SafeContinuation : Continuation { - internal constructor(delegate: Continuation, initialResult: Any?) - - @PublishedApi - internal constructor(delegate: Continuation) - - @PublishedApi - internal fun getResult(): Any? - - override val context: CoroutineContext - override fun resume(value: T): Unit - override fun resumeWithException(exception: Throwable): Unit -} diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesIntrinsicsExperimentalH.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesIntrinsicsExperimentalH.kt deleted file mode 100644 index 99f319b1d72..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesIntrinsicsExperimentalH.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.coroutines.experimental.intrinsics - -import kotlin.coroutines.experimental.Continuation - -/** - * Starts unintercepted coroutine without receiver and with result type [T] and executes it until its first suspension. - * Returns the result of the coroutine or throws its exception if it does not suspend or [COROUTINE_SUSPENDED] if it suspends. - * In the latter case, the [completion] continuation is invoked when coroutine completes with result or exception. - * This function is designed to be used from inside of [suspendCoroutineOrReturn] to resume the execution of a suspended - * coroutine using a reference to the suspending function. - */ -@SinceKotlin("1.1") -public expect inline fun (suspend () -> T).startCoroutineUninterceptedOrReturn( - completion: Continuation -): Any? - -/** - * Starts unintercepted coroutine with receiver type [R] and result type [T] and executes it until its first suspension. - * Returns the result of the coroutine or throws its exception if it does not suspend or [COROUTINE_SUSPENDED] if it suspends. - * In the latter case, the [completion] continuation is invoked when coroutine completes with result or exception. - * This function is designed to be used from inside of [suspendCoroutineOrReturn] to resume the execution of a suspended - * coroutine using a reference to the suspending function. - */ -@SinceKotlin("1.1") -public expect inline fun (suspend R.() -> T).startCoroutineUninterceptedOrReturn( - receiver: R, - completion: Continuation -): Any? - -@SinceKotlin("1.1") -public expect fun (suspend () -> T).createCoroutineUnchecked( - completion: Continuation -): Continuation - -@SinceKotlin("1.1") -public expect fun (suspend R.() -> T).createCoroutineUnchecked( - receiver: R, - completion: Continuation -): Continuation diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesLibrary.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesLibrary.kt deleted file mode 100644 index 6a98802904b..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/CoroutinesLibrary.kt +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -@file:kotlin.jvm.JvmName("CoroutinesKt") -@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - -package kotlin.coroutines.experimental - -import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED -import kotlin.coroutines.experimental.intrinsics.createCoroutineUnchecked -import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn -import kotlin.internal.InlineOnly - -/** - * Starts coroutine with receiver type [R] and result type [T]. - * This function creates and start a new, fresh instance of suspendable computation every time it is invoked. - * The [completion] continuation is invoked when coroutine completes with result or exception. - */ -@SinceKotlin("1.1") -@Suppress("UNCHECKED_CAST") -public fun (suspend R.() -> T).startCoroutine( - receiver: R, - completion: Continuation -) { - createCoroutineUnchecked(receiver, completion).resume(Unit) -} - -/** - * Starts coroutine without receiver and with result type [T]. - * This function creates and start a new, fresh instance of suspendable computation every time it is invoked. - * The [completion] continuation is invoked when coroutine completes with result or exception. - */ -@SinceKotlin("1.1") -@Suppress("UNCHECKED_CAST") -public fun (suspend () -> T).startCoroutine( - completion: Continuation -) { - createCoroutineUnchecked(completion).resume(Unit) -} - -/** - * Creates a coroutine with receiver type [R] and result type [T]. - * This function creates a new, fresh instance of suspendable computation every time it is invoked. - * - * To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance. - * The [completion] continuation is invoked when coroutine completes with result or exception. - * Repeated invocation of any resume function on the resulting continuation produces [IllegalStateException]. - */ -@SinceKotlin("1.1") -@Suppress("UNCHECKED_CAST") -public fun (suspend R.() -> T).createCoroutine( - receiver: R, - completion: Continuation -): Continuation = SafeContinuation(createCoroutineUnchecked(receiver, completion), COROUTINE_SUSPENDED) - -/** - * Creates a coroutine without receiver and with result type [T]. - * This function creates a new, fresh instance of suspendable computation every time it is invoked. - * - * To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance. - * The [completion] continuation is invoked when coroutine completes with result or exception. - * Repeated invocation of any resume function on the resulting continuation produces [IllegalStateException]. - */ -@SinceKotlin("1.1") -@Suppress("UNCHECKED_CAST") -public fun (suspend () -> T).createCoroutine( - completion: Continuation -): Continuation = SafeContinuation(createCoroutineUnchecked(completion), COROUTINE_SUSPENDED) - -/** - * Obtains the current continuation instance inside suspend functions and suspends - * currently running coroutine. - * - * In this function both [Continuation.resume] and [Continuation.resumeWithException] can be used either synchronously in - * the same stack-frame where suspension function is run or asynchronously later in the same thread or - * from a different thread of execution. Repeated invocation of any resume function produces [IllegalStateException]. - */ -@SinceKotlin("1.1") -public suspend inline fun suspendCoroutine(crossinline block: (Continuation) -> Unit): T = - suspendCoroutineOrReturn { c: Continuation -> - val safe = SafeContinuation(c) - block(safe) - safe.getResult() - } - -/** - * Continuation context of current coroutine. - * - * This allows the user code to not pass an extra [CoroutineContext] parameter in basic coroutine builders - * like [launch](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html) - * and [async](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.html), - * but still provide easy access to coroutine context. - */ -@SinceKotlin("1.2") -@Suppress("WRONG_MODIFIER_TARGET") -@InlineOnly -public suspend inline val coroutineContext: CoroutineContext - get() { - throw NotImplementedError("Implemented as intrinsic") - } - -// INTERNAL DECLARATIONS - -@kotlin.internal.InlineOnly -internal inline fun processBareContinuationResume(completion: Continuation<*>, block: () -> Any?) { - try { - val result = block() - if (result !== COROUTINE_SUSPENDED) { - @Suppress("UNCHECKED_CAST") - (completion as Continuation).resume(result) - } - } catch (t: Throwable) { - completion.resumeWithException(t) - } -} diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/SequenceBuilder.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/SequenceBuilder.kt deleted file mode 100644 index 495e6137aec..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/SequenceBuilder.kt +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("SequenceBuilderKt") -@file:OptIn(ExperimentalTypeInference::class) - -package kotlin.coroutines.experimental - -import kotlin.coroutines.experimental.intrinsics.* -import kotlin.experimental.ExperimentalTypeInference - -/** - * Builds a [Sequence] lazily yielding values one by one. - * - * Since Kotlin 1.3 use [kotlin.sequences.sequence] instead. - * - * @see kotlin.sequences.generateSequence - * @see kotlin.sequences.sequence - */ -@SinceKotlin("1.1") -public fun buildSequence(@BuilderInference builderAction: suspend SequenceBuilder.() -> Unit): Sequence = Sequence { buildIterator(builderAction) } - -/** - * Builds an [Iterator] lazily yielding values one by one. - * - * Since Kotlin 1.3 use [kotlin.sequences.iterator] instead. - * - * @see kotlin.sequences.iterator - */ -@SinceKotlin("1.1") -public fun buildIterator(@BuilderInference builderAction: suspend SequenceBuilder.() -> Unit): Iterator { - val iterator = SequenceBuilderIterator() - iterator.nextStep = builderAction.createCoroutineUnchecked(receiver = iterator, completion = iterator) - return iterator -} - -/** - * Builder for a [Sequence] or an [Iterator], provides [yield] and [yieldAll] suspension functions. - * - * Since Kotlin 1.3 use [kotlin.sequences.SequenceScope] instead. - * - * @see buildSequence - * @see buildIterator - * @see kotlin.sequences.SequenceScope - */ -@RestrictsSuspension -@SinceKotlin("1.1") -public abstract class SequenceBuilder internal constructor() { - /** - * Yields a value to the [Iterator] being built. - */ - public abstract suspend fun yield(value: T) - - /** - * Yields all values from the `iterator` to the [Iterator] being built. - * - * The sequence of values returned by the given iterator can be potentially infinite. - */ - public abstract suspend fun yieldAll(iterator: Iterator) - - /** - * Yields a collections of values to the [Iterator] being built. - */ - public suspend fun yieldAll(elements: Iterable) { - if (elements is Collection && elements.isEmpty()) return - return yieldAll(elements.iterator()) - } - - /** - * Yields potentially infinite sequence of values to the [Iterator] being built. - * - * The sequence can be potentially infinite. - */ - public suspend fun yieldAll(sequence: Sequence) = yieldAll(sequence.iterator()) -} - -private typealias State = Int - -private const val State_NotReady: State = 0 -private const val State_ManyNotReady: State = 1 -private const val State_ManyReady: State = 2 -private const val State_Ready: State = 3 -private const val State_Done: State = 4 -private const val State_Failed: State = 5 - -private class SequenceBuilderIterator : SequenceBuilder(), Iterator, Continuation { - private var state = State_NotReady - private var nextValue: T? = null - private var nextIterator: Iterator? = null - var nextStep: Continuation? = null - - override fun hasNext(): Boolean { - while (true) { - when (state) { - State_NotReady -> {} - State_ManyNotReady -> - if (nextIterator!!.hasNext()) { - state = State_ManyReady - return true - } else { - nextIterator = null - } - State_Done -> return false - State_Ready, State_ManyReady -> return true - else -> throw exceptionalState() - } - - state = State_Failed - val step = nextStep!! - nextStep = null - step.resume(Unit) - } - } - - override fun next(): T { - when (state) { - State_NotReady, State_ManyNotReady -> return nextNotReady() - State_ManyReady -> { - state = State_ManyNotReady - return nextIterator!!.next() - } - State_Ready -> { - state = State_NotReady - @Suppress("UNCHECKED_CAST") - val result = nextValue as T - nextValue = null - return result - } - else -> throw exceptionalState() - } - } - - private fun nextNotReady(): T { - if (!hasNext()) throw NoSuchElementException() else return next() - } - - private fun exceptionalState(): Throwable = when (state) { - State_Done -> NoSuchElementException() - State_Failed -> IllegalStateException("Iterator has failed.") - else -> IllegalStateException("Unexpected state of the iterator: $state") - } - - - suspend override fun yield(value: T) { - nextValue = value - state = State_Ready - return suspendCoroutineOrReturn { c -> - nextStep = c - COROUTINE_SUSPENDED - } - } - - suspend override fun yieldAll(iterator: Iterator) { - if (!iterator.hasNext()) return - nextIterator = iterator - state = State_ManyReady - return suspendCoroutineOrReturn { c -> - nextStep = c - COROUTINE_SUSPENDED - } - } - - // Completion continuation implementation - override fun resume(value: Unit) { - state = State_Done - } - - override fun resumeWithException(exception: Throwable) { - throw exception // just rethrow - } - - override val context: CoroutineContext - get() = EmptyCoroutineContext -} diff --git a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/intrinsics/Intrinsics.kt b/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/intrinsics/Intrinsics.kt deleted file mode 100644 index 0e32b31549d..00000000000 --- a/libraries/stdlib/coroutines-experimental/src/kotlin/coroutines/experimental/intrinsics/Intrinsics.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -@file:kotlin.jvm.JvmName("IntrinsicsKt") -@file:kotlin.jvm.JvmMultifileClass -@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") -package kotlin.coroutines.experimental.intrinsics - -import kotlin.coroutines.experimental.* - -/** - * Obtains the current continuation instance inside suspend functions and either suspends - * currently running coroutine or returns result immediately without suspension. - * - * If the [block] returns the special [COROUTINE_SUSPENDED] value, it means that suspend function did suspend the execution and will - * not return any result immediately. In this case, the [Continuation] provided to the [block] shall be invoked at some moment in the - * future when the result becomes available to resume the computation. - * - * Otherwise, the return value of the [block] must have a type assignable to [T] and represents the result of this suspend function. - * It means that the execution was not suspended and the [Continuation] provided to the [block] shall not be invoked. - * As the result type of the [block] is declared as `Any?` and cannot be correctly type-checked, - * its proper return type remains on the conscience of the suspend function's author. - * - * Note that it is not recommended to call either [Continuation.resume] nor [Continuation.resumeWithException] functions synchronously - * in the same stackframe where suspension function is run. Use [suspendCoroutine] as a safer way to obtain current - * continuation instance. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -@Suppress("UNUSED_PARAMETER") -public suspend inline fun suspendCoroutineOrReturn(crossinline block: (Continuation) -> Any?): T = - suspendCoroutineUninterceptedOrReturn { cont -> block(cont.intercepted()) } - -/** - * Obtains the current continuation instance inside suspend functions and either suspends - * currently running coroutine or returns result immediately without suspension. - * - * Unlike [suspendCoroutineOrReturn] it does not intercept continuation. - */ -@SinceKotlin("1.2") -@kotlin.internal.InlineOnly -@Suppress("UNUSED_PARAMETER", "RedundantSuspendModifier") -public suspend inline fun suspendCoroutineUninterceptedOrReturn(crossinline block: (Continuation) -> Any?): T = - throw NotImplementedError("Implementation of suspendCoroutineUninterceptedOrReturn is intrinsic") - -/** - * Intercept continuation with [ContinuationInterceptor]. - */ -@SinceKotlin("1.2") -@kotlin.internal.InlineOnly -public inline fun Continuation.intercepted(): Continuation = - throw NotImplementedError("Implementation of intercepted is intrinsic") - -/** - * This value is used as a return value of [suspendCoroutineOrReturn] `block` argument to state that - * the execution was suspended and will not return any result immediately. - */ -@SinceKotlin("1.1") -public expect val COROUTINE_SUSPENDED: Any // get() = CoroutineSuspendedMarker diff --git a/libraries/stdlib/jdk7/build.gradle b/libraries/stdlib/jdk7/build.gradle index 361e0fda382..9828cfd9148 100644 --- a/libraries/stdlib/jdk7/build.gradle +++ b/libraries/stdlib/jdk7/build.gradle @@ -44,7 +44,6 @@ configurations { dependencies { compile project(':kotlin-stdlib') testCompile project(':kotlin-test:kotlin-test-junit') - testCompile project(':kotlin-coroutines-experimental-compat') } jar { diff --git a/libraries/stdlib/jdk8/build.gradle b/libraries/stdlib/jdk8/build.gradle index a5e45269b44..0d8f659fe60 100644 --- a/libraries/stdlib/jdk8/build.gradle +++ b/libraries/stdlib/jdk8/build.gradle @@ -14,7 +14,6 @@ dependencies { compile project(':kotlin-stdlib') compile project(':kotlin-stdlib-jdk7') testCompile project(':kotlin-test:kotlin-test-junit') - testCompile project(':kotlin-coroutines-experimental-compat') } sourceSets { diff --git a/libraries/stdlib/jvm/build.gradle b/libraries/stdlib/jvm/build.gradle index af14f044562..ac563951128 100644 --- a/libraries/stdlib/jvm/build.gradle +++ b/libraries/stdlib/jvm/build.gradle @@ -57,7 +57,6 @@ dependencies { compile group: 'org.jetbrains', name: 'annotations', version:'13.0' testCompile project(':kotlin-test:kotlin-test-junit') - testCompile project(':kotlin-coroutines-experimental-compat') builtins project(':core:builtins') } diff --git a/libraries/stdlib/jvm/test/coroutines/CoroutinesMigrationTest.kt b/libraries/stdlib/jvm/test/coroutines/CoroutinesMigrationTest.kt deleted file mode 100644 index 36408b32aea..00000000000 --- a/libraries/stdlib/jvm/test/coroutines/CoroutinesMigrationTest.kt +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -@file:Suppress("ObsoleteExperimentalCoroutines") - -package test.coroutines.experimental.migration - -import kotlin.coroutines.experimental.buildSequence as experimentalBuildSequence -import kotlin.coroutines.experimental.SequenceBuilder as ExperimentalSequenceBuilder -import kotlin.coroutines.experimental.Continuation as ExperimentalContinuation -import kotlin.coroutines.experimental.CoroutineContext as ExperimentalCoroutineContext -import kotlin.coroutines.experimental.AbstractCoroutineContextElement as ExperimentalAbstractCoroutineContextElement -import kotlin.coroutines.experimental.EmptyCoroutineContext as ExperimentalEmptyCoroutineContext -import kotlin.coroutines.experimental.ContinuationInterceptor as ExperimentalContinuationInterceptor -import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED as EXPERIMENTAL_COROUTINE_SUSPENDED -import kotlin.coroutines.experimental.migration.* -import kotlin.coroutines.* -import kotlin.test.* - -/** - * Tests on coroutines migrations utilities. - */ -class CoroutinesMigrationTest { - @Test - fun testContextMigration() { - assertTrue(EmptyCoroutineContext === ExperimentalEmptyCoroutineContext.toCoroutineContext()) - assertTrue(ExperimentalEmptyCoroutineContext === EmptyCoroutineContext.toExperimentalCoroutineContext()) - MyElement().let { e -> - assertTrue(e === e.toExperimentalCoroutineContext().toCoroutineContext()) - val ee = MyExperimentalElement() - val ctx = (e.toExperimentalCoroutineContext() + ee).toCoroutineContext() - assertTrue(e === ctx[MyElement.Key]) - assertTrue(ee === ctx.toExperimentalCoroutineContext()[MyExperimentalElement.Key]) - } - MyExperimentalElement().let { ee -> - assertTrue(ee === ee.toCoroutineContext().toExperimentalCoroutineContext()) - val e = MyElement() - val ctx = (ee.toCoroutineContext() + e).toExperimentalCoroutineContext() - assertTrue(ee === ctx[MyExperimentalElement.Key]) - assertTrue(e === ctx.toCoroutineContext()[MyElement.Key]) - } - } - - class MyElement : AbstractCoroutineContextElement(Key) { - companion object Key : CoroutineContext.Key - } - - class MyExperimentalElement : ExperimentalAbstractCoroutineContextElement(Key) { - companion object Key : ExperimentalCoroutineContext.Key - } -} - diff --git a/libraries/tools/binary-compatibility-validator/build.gradle b/libraries/tools/binary-compatibility-validator/build.gradle index 50d1fc8a2c3..63f44f4b56b 100644 --- a/libraries/tools/binary-compatibility-validator/build.gradle +++ b/libraries/tools/binary-compatibility-validator/build.gradle @@ -14,7 +14,6 @@ dependencies { testArtifacts project(':kotlin-stdlib-jdk7') testArtifacts project(':kotlin-stdlib-jdk8') testArtifacts project(':kotlin-reflect') - testArtifacts project(':kotlin-coroutines-experimental-compat') } pill { diff --git a/libraries/tools/binary-compatibility-validator/src/test/kotlin/org.jetbrains.kotlin.tools.tests/RuntimePublicAPITest.kt b/libraries/tools/binary-compatibility-validator/src/test/kotlin/org.jetbrains.kotlin.tools.tests/RuntimePublicAPITest.kt index 3b868370b32..16a433cf292 100644 --- a/libraries/tools/binary-compatibility-validator/src/test/kotlin/org.jetbrains.kotlin.tools.tests/RuntimePublicAPITest.kt +++ b/libraries/tools/binary-compatibility-validator/src/test/kotlin/org.jetbrains.kotlin.tools.tests/RuntimePublicAPITest.kt @@ -33,11 +33,6 @@ class RuntimePublicAPITest { snapshotAPIAndCompare("../../reflect/api/build/libs", "kotlin-reflect-api(?!-[-a-z]+)", nonPublicPackages = listOf("kotlin.reflect.jvm.internal")) } - @Test fun kotlinCoroutinesExperimentalCompat() { - snapshotAPIAndCompare("../../stdlib/coroutines-experimental/build/libs", "kotlin-coroutines-experimental-compat") - } - - private fun snapshotAPIAndCompare( basePath: String, jarPattern: String, diff --git a/prepare/compiler/build.gradle.kts b/prepare/compiler/build.gradle.kts index 2efc93714ba..d7d5ffab1ba 100644 --- a/prepare/compiler/build.gradle.kts +++ b/prepare/compiler/build.gradle.kts @@ -83,7 +83,6 @@ val distLibraryProjects = listOfNotNull( ":kotlin-annotations-android", ":kotlin-annotations-jvm", ":kotlin-ant", - ":kotlin-coroutines-experimental-compat", ":kotlin-daemon", ":kotlin-daemon-client", // TODO: uncomment when new daemon will be put back into dist @@ -121,7 +120,6 @@ val distCompilerPluginProjects = listOf( val distSourcesProjects = listOfNotNull( ":kotlin-annotations-jvm", - ":kotlin-coroutines-experimental-compat", ":kotlin-script-runtime", ":kotlin-test:kotlin-test-js".takeIf { !kotlinBuildProperties.isInJpsBuildIdeaSync }, ":kotlin-test:kotlin-test-junit", @@ -394,4 +392,4 @@ inline fun Project.distTask( rename(quote("-$version"), "") rename(quote("-$bootstrapKotlinVersion"), "") block() -} \ No newline at end of file +} diff --git a/prepare/idea-plugin/build.gradle.kts b/prepare/idea-plugin/build.gradle.kts index 5e5698f7526..2c71957e235 100644 --- a/prepare/idea-plugin/build.gradle.kts +++ b/prepare/idea-plugin/build.gradle.kts @@ -124,7 +124,6 @@ val projectsToShadow by extra(listOf( // Projects published to maven copied to the plugin as separate jars val libraryProjects = listOf( ":kotlin-reflect", - ":kotlin-coroutines-experimental-compat", ":kotlin-compiler-client-embeddable", ":kotlin-daemon-client", ":kotlin-daemon-client-new", diff --git a/settings.gradle b/settings.gradle index 65f5704ac74..3dfe1de7956 100644 --- a/settings.gradle +++ b/settings.gradle @@ -208,7 +208,6 @@ include ":benchmarks", ":plugins:uast-kotlin-idea", ":plugins:annotation-based-compiler-plugins-ide-support", ":kotlin-script-runtime", - ":kotlin-coroutines-experimental-compat", ":plugins:fir:fir-plugin-prototype", ":plugins:fir:fir-plugin-prototype:plugin-annotations", ":kotlin-test", @@ -442,7 +441,6 @@ if (buildProperties.inJpsBuildIdeaSync) { rootProject.name = "kotlin" project(':kotlin-script-runtime').projectDir = "$rootDir/libraries/tools/script-runtime" as File -project(":kotlin-coroutines-experimental-compat").projectDir = "$rootDir/libraries/stdlib/coroutines-experimental" as File project(':kotlin-test').projectDir = "$rootDir/libraries/kotlin.test" as File project(':kotlin-test:kotlin-test-common').projectDir = "$rootDir/libraries/kotlin.test/common" as File project(':kotlin-test:kotlin-test-annotations-common').projectDir = "$rootDir/libraries/kotlin.test/annotations-common" as File