Compiling Kotlin to JS failes: TranslationRuntimeException

#1

Hello,

I’ve got a problem with the kotlin js compiler.

When I try to compile my code, I get the following exception:

org.jetbrains.kotlin.js.facade.exceptions.TranslationRuntimeException: Unexpected error occurred compiling the following fragment: 'BackgroundMessageService.onReceive(::onTrans
lationRequest)' at (113,5) in .../HoverTranslateWebExtension/background_script/src/main/kotlin/background_script.kt
 at org.jetbrains.kotlin.js.translate.general.Translation.doTranslateExpression(Translation.java:232)
        at org.jetbrains.kotlin.js.translate.general.Translation.translateExpression(Translation.java:133)
        at org.jetbrains.kotlin.js.translate.expression.ExpressionVisitor.visitBlockExpression(ExpressionVisitor.java:115)
        at org.jetbrains.kotlin.js.translate.expression.ExpressionVisitor.visitBlockExpression(ExpressionVisitor.java:82)
        at org.jetbrains.kotlin.psi.KtBlockExpression.accept(KtBlockExpression.java:44)
        at org.jetbrains.kotlin.js.translate.general.Translation.doTranslateExpression(Translation.java:226)
        at org.jetbrains.kotlin.js.translate.general.Translation.translateExpression(Translation.java:133)
        at org.jetbrains.kotlin.js.translate.utils.FunctionBodyTranslator.translate(FunctionBodyTranslator.java:140)
        at org.jetbrains.kotlin.js.translate.utils.FunctionBodyTranslator.translateFunctionBody(FunctionBodyTranslator.java:74)
        at org.jetbrains.kotlin.js.translate.expression.FunctionTranslatorKt.translateFunction(FunctionTranslator.kt:96)
        at org.jetbrains.kotlin.js.translate.declaration.AbstractDeclarationVisitor.translateFunction(AbstractDeclarationVisitor.kt:139)
        at org.jetbrains.kotlin.js.translate.declaration.AbstractDeclarationVisitor.visitNamedFunction(AbstractDeclarationVisitor.kt:99)
        at org.jetbrains.kotlin.js.translate.declaration.AbstractDeclarationVisitor.visitNamedFunction(AbstractDeclarationVisitor.kt:41)
        at org.jetbrains.kotlin.psi.KtNamedFunction.accept(KtNamedFunction.java:49)
        at org.jetbrains.kotlin.js.translate.general.Translation.translateFile(Translation.java:429)
        at org.jetbrains.kotlin.js.translate.general.Translation.doGenerateAst(Translation.java:346)
        at org.jetbrains.kotlin.js.translate.general.Translation.generateAst(Translation.java:308)
        at org.jetbrains.kotlin.js.facade.K2JSTranslator.translateUnits(K2JSTranslator.java:137)
        at org.jetbrains.kotlin.js.facade.K2JSTranslator.translate(K2JSTranslator.java:99)
        at org.jetbrains.kotlin.cli.js.K2JSCompiler.translate(K2JSCompiler.java:152)
        at org.jetbrains.kotlin.cli.js.K2JSCompiler.doExecute(K2JSCompiler.java:286)
        at org.jetbrains.kotlin.cli.js.K2JSCompiler.doExecute(K2JSCompiler.java:82)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:96)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:52)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:93)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$1$2.invoke(CompileServiceImpl.kt:442)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$1$2.invoke(CompileServiceImpl.kt:102)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:1029)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:102)
        at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:1071)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:1028)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:441)
        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)

It worked fine until I changed the following interface:

I only added “suspend” to make it work with callbacks which are co-routines.

And this is the place where the compiler fails:

Is this a bug, or did I do something wrong. Intellij seems fine with the code

#2

I just played around with it, and found a way how get the compiler to compile the code:

Is this a compiler bug? If so I will create a bug report here:
https://youtrack.jetbrains.com

So I just ended up wrapping the code within another lambda and then call it. So I guess the wrapping lambda will become a suspend function implicitly. In this scenario I had to be more explicit regarding the types of the lambda.

#3

Here is another version which compiles, but with less explicit typing:

#4

I reported the issue:
https://youtrack.jetbrains.com/issue/KT-30361

#5

It’s probably another duplicate of https://youtrack.jetbrains.com/issue/KT-27855

1 Like
#6

@rjaros I think you are right. It looks like the same issue. And the ticket you have posted already has 2 other duplicates linked.

Awesome, and I just created the next duplicate.

#7

Just add a comment to your new issue that it might be a duplicate, but you only found out after you created it. That way the kotlin team does not need to spend too much time figuring out that it’s a duplicate.

#8

done