Skip to content

Enum class implementing CoroutineContext.Element causes NullPointerException #4500

@whirvis

Description

@whirvis

Describe the bug

When implementing CoroutineContext.Element on an enum class using a companion object for the key, a NullPointerException will be thrown when trying to do something like start a new job. My guess is that this occurs because the enumerations of Foo are initialized before Foo.Key is initialized. But if that's the case, shouldn't there be a compiler error to prevent key from having a null value?

enum class Foo : CoroutineContext.Element {
    BAR, BAZ, QUX;
    companion object Key : CoroutineContext.Key<Foo>
    override val key = Key /* <- no compiler error? */
}

fun main() = runBlocking {
    async(Foo.BAR) {}.await() /* <- exception here */
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions