ronantakizawa/codereview-bench huggingface dataset:
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/js/src/CoroutineContext.kt (Kotlin)

Code before:
private fun isJsdom() = jsTypeOf(navigator) != UNDEFINED &&
    navigator != null &&
    navigator.userAgent != null &&
    jsTypeOf(navigator.userAgent) != UNDEFINED &&
    jsTypeOf(navigator.userAgent.match) != UNDEFINED &&
    navigator.userAgent.match("\\bjsdom\\b")

internal actual val DefaultDelay: Delay
    get() = Dispatchers.Default as Delay

public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
    val combined = coroutineContext + context
    return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
        combined + Dispatchers.Default else combined
}

// No debugging facilities on JS
internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block()
internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block()
internal actual fun Continuation<*>.toDebugString(): String = toString()
internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on JS

internal actual class UndispatchedCoroutine<in T> actual constructor(
    context: CoroutineContext,
    uCont: Continuation<T>
) : ScopeCoroutine<T>(context, uCont, tr ue) {
    override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont))
}


Review comment (suggestion):
```suggestion
) : ScopeCoroutine<T>(context, uCont, true) {
```

Code after:
private fun isJsdom() = jsTypeOf(navigator) != UNDEFINED &&
    navigator != null &&
    navigator.userAgent != null &&
    jsTypeOf(navigator.userAgent) != UNDEFINED &&
    jsTypeOf(navigator.userAgent.match) != UNDEFINED &&
    navigator.userAgent.match("\\bjsdom\\b")

internal actual val DefaultDelay: Delay
    get() = Dispatchers.Default as Delay

public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
    val combined = coroutineContext + context
    return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
        combined + Dispatchers.Default else combined
}

// No debugging facilities on JS
internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block()
internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block()
internal actual fun Continuation<*>.toDebugString(): String = toString()
internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on JS

internal actual class UndispatchedCoroutine<in T> actual constructor(
    context: CoroutineContext,
    uCont: Continuation<T>
) : ScopeCoroutine<T>(context, uCont, true) {
    override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont))
}
Repository: Kotlin/kotlinx.coroutines
File: integration/kotlinx-coroutines-guava/test/FutureAsDeferredUnhandledCompletionExceptionTest.kt (Kotlin)

Code before:
        Thread.setDefaultUncaughtExceptionHandler { _, e -> caughtException = e }
    }

    @After
    fun tearDown() {
        Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
    }

    @Test
    fun testLostExceptionOnSuccess() = runTest {
        val future = SettableFuture.create<Int>()
        val deferred = future.asDeferred()
        deferred.invokeOnCompletion { throw TestException() }
        future.set(1)
        assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException }
    }

    @Test
    fun testLostExceptionOnFailure() = runTest {
        val future = SettableFuture.create<Int>()
        val deferred = future.asDeferred()
        deferred.invokeOnCompletion { throw TestException() }
        val failedFuture = CompletableDeferred<Int>().apply {
            completeExceptionally(TestException2())
        }.asListenableFuture()
        future.setFuture(failedFuture)
        assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException }
    }
}


Review comment (suggestion):
I think this code needs some comments, as it's not clear why it couldn't be just
```suggestion
        future.setException(TestException2())
```

Code after:
        Thread.setDefaultUncaughtExceptionHandler { _, e -> caughtException = e }
    }

    @After
    fun tearDown() {
        Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
    }

    @Test
    fun testLostExceptionOnSuccess() = runTest {
        val future = SettableFuture.create<Int>()
        val deferred = future.asDeferred()
        deferred.invokeOnCompletion { throw TestException() }
        future.set(1)
        assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException }
    }

    @Test
    fun testLostExceptionOnFailure() = runTest {
        val future = SettableFuture.create<Int>()
        val deferred = future.asDeferred()
        deferred.invokeOnCompletion { throw TestException() }
        future.setException(TestException2())
        assertTrue { caughtException is CompletionHandlerException && caughtException.cause is TestException }
    }
}
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt (Kotlin)

Code before:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After the successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but previously dispatched tasks will be run.
     *
     * Invocations of `close` on already closed dispatcher have no effect.
     */
    public abstract fun close()
}


Review comment (suggestion):
I've decided to leave an opportunity to [potentially] introduce `isClosed` later if we have enough use-cases

Code after:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides a method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
@ExperimentalCoroutinesApi
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After a successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
     *
     * Invocations of `close` are idempotent and thread-safe.
     */
    public abstract fun close()
}
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt (Kotlin)

Code before:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After the successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but previously dispatched tasks will be run.
     *
     * Invocations of `close` on already closed dispatcher have no effect.
     */
    public abstract fun close()
}


Review comment (suggestion):
It also worth checking if we can make it experimental without propagating experimentality to `ExecutorCoroutineDispatcher`, will do

Code after:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides a method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
@ExperimentalCoroutinesApi
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After a successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
     *
     * Invocations of `close` are idempotent and thread-safe.
     */
    public abstract fun close()
}
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt (Kotlin)

Code before:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After the successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but previously dispatched tasks will be run.
     *
     * Invocations of `close` on already closed dispatcher have no effect.
     */
    public abstract fun close()
}


Review comment (suggestion):
```suggestion
     * After a successful call to [close], no new tasks will
```
To my ear, "the" implies that the call will always happen eventually.

Code after:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides a method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
@ExperimentalCoroutinesApi
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After a successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
     *
     * Invocations of `close` are idempotent and thread-safe.
     */
    public abstract fun close()
}
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt (Kotlin)

Code before:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After the successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but previously dispatched tasks will be run.
     *
     * Invocations of `close` on already closed dispatcher have no effect.
     */
    public abstract fun close()
}


Review comment (suggestion):
```suggestion
     * Invocations of `close` on an already closed dispatcher have no effect.
```

Code after:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides a method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
@ExperimentalCoroutinesApi
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After a successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
     *
     * Invocations of `close` are idempotent and thread-safe.
     */
    public abstract fun close()
}
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt (Kotlin)

Code before:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After the successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but previously dispatched tasks will be run.
     *
     * Invocations of `close` on already closed dispatcher have no effect.
     */
    public abstract fun close()
}


Review comment (suggestion):
```suggestion
     * be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
```

Code after:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides a method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
@ExperimentalCoroutinesApi
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After a successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
     *
     * Invocations of `close` are idempotent and thread-safe.
     */
    public abstract fun close()
}
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt (Kotlin)

Code before:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After the successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but previously dispatched tasks will be run.
     *
     * Invocations of `close` on already closed dispatcher have no effect.
     */
    public abstract fun close()
}


Review comment (suggestion):
```suggestion
 * [CoroutineDispatcher] that provides a method to close it,
```

Code after:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

/**
 * [CoroutineDispatcher] that provides a method to close it,
 * causing the rejection of any new tasks and cleanup of all underlying resources
 * associated with the current dispatcher.
 * Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
 * by `kotlin.native.Worker`.
 *
 * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
 * might be added to this interface in the future, but is stable for use.
 */
@ExperimentalCoroutinesApi
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {

    /**
     * Initiate the closing sequence of the coroutine dispatcher.
     * After a successful call to [close], no new tasks will
     * be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
     *
     * Invocations of `close` are idempotent and thread-safe.
     */
    public abstract fun close()
}
Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-test/common/src/TestCoroutineExceptionHandler.kt (Kotlin)

Code before:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.test

import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import kotlin.coroutines.*

/**
 * Access uncaught coroutine exceptions captured during test execution.
 */
@Deprecated(
    "Consider whether the default mechanism of handling uncaught exceptions is sufficient. " +
        "If not, try writing your own `CoroutineExceptionHandler` and " +
        "please report your use case at https://github.com/Kotlin/kotlinx.coroutines/issues.",
    level = DeprecationLevel.WARNING
)
public interface UncaughtExceptionCaptor {
    /**
     * List of uncaught coroutine exceptions.
     *
     * The returned list is a copy of the currently caught exceptions.
     * During [cleanupTestCoroutines] the first element of this list is rethrown if it is not empty.
     */
    public val uncaughtExceptions: List<Throwable>

    /**
     * Call after the test completes to ensure that there were no uncaught exceptions.
     *
     * The first exception in uncaughtExceptions is rethrown. All other exceptions are
     * printed using [Throwable.printStackTrace].
     *
     * @throws Throwable the first uncaught exception, if there are any uncaught exceptions.
     */
    public fun cleanupTestCoroutines()
}

/**

Review comment (suggestion):
[nit] would suggest starting such deprecations with a clear intent: "Deprecated for removal without replacement. ..."

Code after:
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.test

import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import kotlin.coroutines.*

/**
 * Access uncaught coroutine exceptions captured during test execution.
 */
@Deprecated(
    "Deprecated for removal without a replacement. " +
        "Consider whether the default mechanism of handling uncaught exceptions is sufficient. " +
        "If not, try writing your own `CoroutineExceptionHandler` and " +
        "please report your use case at https://github.com/Kotlin/kotlinx.coroutines/issues.",
    level = DeprecationLevel.WARNING
)
public interface UncaughtExceptionCaptor {
    /**
     * List of uncaught coroutine exceptions.
     *
     * The returned list is a copy of the currently caught exceptions.
     * During [cleanupTestCoroutines] the first element of this list is rethrown if it is not empty.
     */
    public val uncaughtExceptions: List<Throwable>

    /**
     * Call after the test completes to ensure that there were no uncaught exceptions.
     *
     * The first exception in uncaughtExceptions is rethrown. All other exceptions are
     * printed using [Throwable.printStackTrace].
     *
     * @throws Throwable the first uncaught exception, if there are any uncaught exceptions.
     */
    public fun cleanupTestCoroutines()
}