ZacSweers / MoshiX

Extensions for Moshi including IR plugins, moshi-sealed, and more.
Apache License 2.0
490 stars 37 forks source link

UninitializedPropertyAccessException thrown during compilation with a value class param #442

Open ZacSweers opened 1 year ago

ZacSweers commented 1 year ago

The following snippet seems to result in a compiler error, unsure if it's a bug in kotlinc or moshix doing something wrong

/** Indicates this issue should be retried after a [delay]. */
@TypeLabel("delayed")
@JsonClass(generateAdapter = true)
data class RetryDelayed(val delay: Duration = 1.minutes) : RetrySignal

Results in this

e: org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: /Users/zacsweers/dev/slack/oss/kotlin-cli-util/src/main/kotlin/slack/cli/exec/RetrySignal.kt
The root cause kotlin.UninitializedPropertyAccessException was thrown at: org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl.getParent(IrValueParameterImpl.kt:43)
        at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:253)
        at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:237)
        at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:68)
        at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:55)
        at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:41)
        at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
        at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
        at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:29)
        at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:16)
        at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
        at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
        at org.jetbrains.kotlin.backend.common.phaser.CompilerPhaseKt.invokeToplevel(CompilerPhase.kt:43)
        at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.invokeLowerings(JvmIrCodegenFactory.kt:308)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.runLowerings(KotlinToJVMBytecodeCompiler.kt:331)
ZacSweers commented 1 year ago

Removing the default value works around it for now

mhelder commented 6 days ago

Gave this a quick try on MoshiX 0.27.1. The following seems to compile just fine:

@JsonClass(generateAdapter = true, generator = "sealed:type")
sealed class Message {

    @TypeLabel("success")
    @JsonClass(generateAdapter = true)
    data class Success(
        @Json(name = "value")
        val value: Duration = 1.minutes
    ) : Message()

    @DefaultObject
    object Unknown : Message()
}

(Kotlin 1.9.21, KSP 1.9.21-1.0.16)