Hey, I got an `Exception during IR lowering` error...
# server
h
Hey, I got an
Exception during IR lowering
error and am a little bit stumped. The relevant error part is this I presume:
Copy code
Caused by: java.lang.RuntimeException: Exception while generating code for:
FUN GENERATED_DATA_CLASS_MEMBER name:copy visibility:public modality:FINAL <> ($this:<redacted>.server.domain.system.events.StreamUpdate.New<T of <redacted>.server.domain.system.events.StreamUpdate.New>, new:T of <redacted>.server.domain.system.events.StreamUpdate.New) returnType:<redacted>.server.domain.system.events.StreamUpdate.New<T of <redacted>.server.domain.system.events.StreamUpdate.New>
  $this: VALUE_PARAMETER name:<this> type:<redacted>.server.domain.system.events.StreamUpdate.New<T of <redacted>.server.domain.system.events.StreamUpdate.New>
  VALUE_PARAMETER name:new index:0 type:T of <redacted>.server.domain.system.events.StreamUpdate.New
    EXPRESSION_BODY
      ERROR_EXPR 'Default Stub' type=T of <redacted>.server.domain.system.events.StreamUpdate.New
  BLOCK_BODY
    RETURN type=kotlin.Nothing from='public final fun copy (new: T of <redacted>.server.domain.system.events.StreamUpdate.New): <redacted>.server.domain.system.events.StreamUpdate.New<T of <redacted>.server.domain.system.events.StreamUpdate.New> declared in <redacted>.server.domain.system.events.StreamUpdate.New'
      CONSTRUCTOR_CALL 'public constructor <init> ($context_receiver_0: <redacted>.server.domain.BaseService<T of <redacted>.server.domain.system.events.StreamUpdate.New>, new: T of <redacted>.server.domain.system.events.StreamUpdate.New) [primary] declared in <redacted>.server.domain.system.events.StreamUpdate.New' type=<redacted>.server.domain.system.events.StreamUpdate.New<T of <redacted>.server.domain.system.events.StreamUpdate.New> origin=null
        <class: T>: T of <redacted>.server.domain.system.events.StreamUpdate.New
        $context_receiver_0: GET_VAR 'new: T of <redacted>.server.domain.system.events.StreamUpdate.New declared in <redacted>.server.domain.system.events.StreamUpdate.New.copy' type=T of <redacted>.server.domain.system.events.StreamUpdate.New origin=null
Relevant Code:
Copy code
enum class UpdateType {
    NEW,
    UPDATE,
    MOVE,
    REMOVE
}

sealed class StreamUpdate<T: Any>(val type: UpdateType, open val old: T? = null) {

    context(BaseService<T>)
    data class New<T: Any>(val new: T): StreamUpdate<T>(UpdateType.NEW)
    
    context(BaseService<T>)
    data class Update<T: Any>(val new: T, override val old: T? = null): StreamUpdate<T>(UpdateType.UPDATE, old ?: getRepository().getEntity(new))

//    data class Move<T: Any>(val old: T, val new: T): MqttUpdate<T>(UpdateType.MOVE) // We might need this later
    context(BaseService<T>)
    data class Remove<T: Any>(override val old: T): StreamUpdate<T>(UpdateType.REMOVE, old)
}

object EventStream {

    val logger = KotlinLogging.logger { }
    val triggerMap = mutableMapOf<KType, PublishSubject<StreamUpdate<*>>>()

    inline fun <reified T : Any>publish(update: StreamUpdate<T>) =
        triggerMap
            .getOrPut(typeOf<T>()) { PublishSubject.create() }
            .onNext(update)

    @Suppress("UNCHECKED_CAST")
    inline fun <reified T : Any> subscribe(): Flowable<StreamUpdate<T>> =
        triggerMap
            .getOrPut(typeOf<T>()) { PublishSubject.create() }
            .toFlowable(BackpressureStrategy.LATEST)
            .filter {
                // We don't need updates without changes.
                if(it is StreamUpdate.Update)
                    it.old != it.new
                else
                    true
            }
            .map { it as StreamUpdate<T> }
            .distinctUntilChanged()
            .doOnError { logger.error("StreamUpdate Error on Trigger ${typeOf<T>()}:", it) }
}
s
That exception indicates a bug/error in the compiler. Probably your code is theoretically valid, but contains some edge case that the compiler wasn't expecting. With some investigation of the stacktrace and the code, you might be able to figure out more about which bit of code it doesn't like, and work around it. Or you could just report it at kotl.in/issue and hope it gets fixed.
h
Ahh dang, thank you. I will post an issue and try to work around it.
I found that if I remove the
context(BaseService<T>)
from
New
and
Remove
it works. I can keep it on
Update
, it seems when the compiler has a problem when then receiver is unused.
s
Oh, I didn't spot that you were using context receivers. Since they're an unreleased feature, some bugs are to be expected. Good that you found the issue and can report it! But it's on your own head if you're using them in production code 😄
h
Project is currently slated for release at the end of the year. I just cross my fingers it's stable by then. 😂