kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

NPE in `ExpressionCodegen.generate`

Open malachid opened this issue 4 years ago • 5 comments

Describe the bug When trying to integrate kotlinx.serialization, unable to compile.

e: java.lang.IllegalStateException: Backend Internal error: Exception during code generation
File being compiled: /home/malachid/work/poc/src/main/kotlin/TestData.kt
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
        at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:239)
        at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade.doGenerateFilesInternal$backend_jvm(JvmBackendFacade.kt:142)
        at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade.doGenerateFilesInternal$backend_jvm$default(JvmBackendFacade.kt:108)
        at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade.doGenerateFiles(JvmBackendFacade.kt:95)
        at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.generateModule(JvmIrCodegenFactory.kt:40)
        at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:616)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:203)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:164)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:51)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:86)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
        at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:346)
        at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:102)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:240)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.access$compileIncrementally(IncrementalCompilerRunner.kt:39)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner$compile$2.invoke(IncrementalCompilerRunner.kt:81)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:93)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:601)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:93)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1633)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
        at sun.rmi.transport.Transport$1.run(Transport.java:200)
        at sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Exception while generating code for:
CONSTRUCTOR visibility:public <> (seen1:kotlin.Int, thing:kotlin.Int?, serializationConstructorMarker:kotlinx.serialization.internal.SerializationConstructorMarker?) returnType:<root>.TestData
  annotations:
    Deprecated(message = 'This synthesized declaration should not be used directly', replaceWith = ReplaceWith(expression = '', imports = []), level = GET_ENUM 'ENUM_ENTRY IR_EXTERNAL_DECLARATION_STUB name:HIDDEN' type=kotlin.DeprecationLevel)
  VALUE_PARAMETER name:seen1 index:0 type:kotlin.Int
  VALUE_PARAMETER name:thing index:1 type:kotlin.Int?
  VALUE_PARAMETER name:serializationConstructorMarker index:2 type:kotlinx.serialization.internal.SerializationConstructorMarker?

        at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
        at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generateMethodNode(ClassCodegen.kt:268)
        at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generateMethod(ClassCodegen.kt:285)
        at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generate(ClassCodegen.kt:123)
        at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade.doGenerateFilesInternal$backend_jvm(JvmBackendFacade.kt:139)
        ... 37 more
Caused by: java.lang.NullPointerException
        at org.jetbrains.kotlin.backend.jvm.codegen.ExpressionCodegen.generate(ExpressionCodegen.kt:199)
        at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.doGenerate(FunctionCodegen.kt:105)
        at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:48)
        ... 41 more


FAILURE: Build failed with an exception.

To Reproduce

  1. copy this template https://github.com/JetBrains/compose-jb/tree/master/templates/desktop-template
  2. add kotlin("plugin.serialization") version "1.4.10"
  3. add implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1")
  4. add a very basic model into src/main/kotlin:
import kotlinx.serialization.*

@Serializable
data class TestData(var thing: Int? = null)

(I have tested w/ and w/o the default value; with var and val)

Expected behavior App should compile. As a side note, you have to include json support - even if that isn't the intended target; or the IDE can't resolve @Serializable.

Environment

  • Kotlin version: 1.4.10
  • Library version: 1.4.10 and 1.0.1
  • Kotlin platforms: JVM
  • Gradle version: 6.5.1
  • IDE version (if bug is related to the IDE) IntelliJ Ultimate 2020.3 EAP --or-- pure CLI
  • Other relevant context: Ubuntu 18.04, JDK 11

malachid avatar Nov 08 '20 23:11 malachid

kotlinx.serialization currently is not fully compatible with Jetpack Compose

sandwwraith avatar Nov 09 '20 12:11 sandwwraith

I've currently encountered the same issue for a sealed class's child which uses the @Serializable annotation: Stacktrace Gist

The implementation was working before when I was purely using Kotlin Platform Mobile (along with Jetpack Compose for Android), but it started failing once I introduced Jetbrains Compose for Desktop (along with its modifications for :common's build.gradle.kts file).

Is there a timeline on when compatibility for these two might be introduced?

sigmadeltasoftware avatar Nov 19 '20 15:11 sigmadeltasoftware

i've got the same error, anyone has a way out of this. Kinda Brings me down things were working well. :/

doughoff avatar Feb 13 '21 02:02 doughoff

I put my serializable classes into a gradle subproject with the serialization plugin and no compose plugin, and then had the subproject with the compose plugin and no serialization plugin depend on that. Hope that helps.

rossdanderson avatar Feb 13 '21 14:02 rossdanderson

I'm getting this error only on incremental compilation (clean builds work fine) in a Kotlin-only module on Android Studio and I'm not using Jetpack Compose. Any ideas on what could be causing this?

marcosalis avatar May 25 '22 09:05 marcosalis