Skip to content

Kotlin 2.2.0 regression: Creating a fun interface CoroutineContext.Element with SAM lamda results in a AbstractMethodError #4512

@matejdro

Description

@matejdro

Describe the bug

Creating a CoroutineContext.Element with the SAM lambda (ACoroutineContextElement { ... }) results in a `AbstractMethodError˙ crash:

Exception in thread "main" java.lang.AbstractMethodError: Receiver class FileKt$$Lambda$14/0x00007f153c0195e8 does not define or inherit an implementation of the resolved method 'abstract java.lang.Object fold(java.lang.Object, kotlin.jvm.functions.Function2)' of interface kotlin.coroutines.CoroutineContext.
 at kotlinx.coroutines.CoroutineContextKt.hasCopyableElements (CoroutineContext.kt:40) 
 at kotlinx.coroutines.CoroutineContextKt.newCoroutineContext (CoroutineContext.kt:35) 

Provide a Reproducer

Run the following playground: https://pl.kotl.in/K7_VMaqK2, with Kotlin 2.2.0 selected. 2.1.21 works fine.

Alternatively, run the following code locally:

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

fun main() = runBlocking {
    val aCoroutineContextElement = createACoroutineContextElement(10)
    withContext(aCoroutineContextElement) {
        println("Hello World")
    }
}

fun createACoroutineContextElement(number: Int): ACoroutineContextElement {
   return ACoroutineContextElement { number }
}

fun interface ACoroutineContextElement : CoroutineContext.Element {
   override val key: CoroutineContext.Key<*>
      get() = ACoroutineContextElement

   fun number(): Int

   companion object : CoroutineContext.Key<ACoroutineContextElement>
}

I can reproduce it with the Kotlin 2.2.0 & coroutines 1.10.2. Downgrading to 2.1.21 or creating the ACoroutineContextElement with the object : syntax fixes the issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions