CompilationException with complex parameter bounds in generic constructor


#1

A CompilationException is thrown when trying to instantiate a non-generic Java class with a generic constructor that has bounded parameter T like <T extends ClassA & InterfaceB> or <T extends interfaceA & InterfaceB>. The following code illustrates the problem.

Kotlin source:

public class Buggable() : Breakable(), Crashable

fun main(args: Array<String>) {
  val a = Buggable()
  val b = Bug(a) // CompilationException
  Bug.reproduce(a) // no problem here!
}

Java source:

public abstract class Breakable { }

public interface Crashable { }

public class Bug {
  public <T extends Breakable & Crashable> Bug(T arg) { }

  public static <T extends Breakable & Crashable> void reproduce(T arg) { }
}


Note that constructor compiles and works perfectly with <T extends Breakable> or <T extends Crashable> bounds and crashes only on combined one. Also note that the static method is ok in either situation.
Is this a compiler bug?

The Java code (simplified for the sake of example) is from the library I use. Is there any chance of Kotlin-side workaround?

EDIT: Stack trace, of course

Error:(5, 13) Kotlin: [Internal Error] org.jetbrains.jet.codegen.CompilationException: Back-end (JVM) Internal error: no descriptor for type constructor of Object
Cause: no descriptor for type constructor of Object
File being compiled and position: (5,13) in /home/mike/Projects/Kotlin/playground/src/Main.kt
PsiElement: Bug(a)
The root cause was thrown at: JetTypeMapper.java:247
     at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:243)
     at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:214)
     at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:249)
     at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:253)
     at org.jetbrains.jet.codegen.ExpressionCodegen$3.fun(ExpressionCodegen.java:3154)
     at org.jetbrains.jet.codegen.ExpressionCodegen$3.fun(ExpressionCodegen.java:3150)
     at org.jetbrains.jet.codegen.ExpressionCodegen.initializeLocalVariable(ExpressionCodegen.java:3230)
     at org.jetbrains.jet.codegen.ExpressionCodegen.visitProperty(ExpressionCodegen.java:3150)
     at org.jetbrains.jet.codegen.ExpressionCodegen.visitProperty(ExpressionCodegen.java:84)
     at org.jetbrains.jet.lang.psi.JetProperty.accept(JetProperty.java:55)
     at org.jetbrains.jet.codegen.CodegenStatementVisitor.visitJetElement(CodegenStatementVisitor.java:31)
     at org.jetbrains.jet.codegen.CodegenStatementVisitor.visitJetElement(CodegenStatementVisitor.java:22)
     at org.jetbrains.jet.lang.psi.JetVisitor.visitExpression(JetVisitor.java:158)
     at org.jetbrains.jet.lang.psi.JetVisitor.visitDeclaration(JetVisitor.java:29)
     at org.jetbrains.jet.lang.psi.JetVisitor.visitNamedDeclaration(JetVisitor.java:354)
     at org.jetbrains.jet.lang.psi.JetVisitor.visitProperty(JetVisitor.java:45)
     at org.jetbrains.jet.lang.psi.JetProperty.accept(JetProperty.java:55)
     at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:233)
     at org.jetbrains.jet.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:262)
     at org.jetbrains.jet.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1421)
     at org.jetbrains.jet.codegen.ExpressionCodegen.visitBlockExpression(ExpressionCodegen.java:1278)
     at org.jetbrains.jet.codegen.ExpressionCodegen.visitBlockExpression(ExpressionCodegen.java:84)
     at org.jetbrains.jet.lang.psi.JetBlockExpression.accept(JetBlockExpression.java:42)
     at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:233)
     at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:214)
     at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:249)
     at org.jetbrains.jet.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1598)
     at org.jetbrains.jet.codegen.FunctionGenerationStrategy$FunctionDefault.doGenerateBody(FunctionGenerationStrategy.java:66)
     at org.jetbrains.jet.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:88)
     at org.jetbrains.jet.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:314)
     at org.jetbrains.jet.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:167)
     at org.jetbrains.jet.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:118)
     at org.jetbrains.jet.codegen.FunctionCodegen.gen(FunctionCodegen.java:103)
     at org.jetbrains.jet.codegen.MemberCodegen.genFunctionOrProperty(MemberCodegen.java:120)
     at org.jetbrains.jet.codegen.PackagePartCodegen.generateBody(PackagePartCodegen.java:64)
     at org.jetbrains.jet.codegen.MemberCodegen.generate(MemberCodegen.java:90)
     at org.jetbrains.jet.codegen.PackageCodegen.generate(PackageCodegen.java:312)
     at org.jetbrains.jet.codegen.PackageCodegen.generate(PackageCodegen.java:207)
     at org.jetbrains.jet.codegen.KotlinCodegenFacade.generatePackage(KotlinCodegenFacade.java:80)
     at org.jetbrains.jet.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:67)
     at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.java:349)
     at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.java:138)
     at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:143)
     at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:53)
     at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:139)
     at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:125)
     at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:57)
     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:483)
     at org.jetbrains.jet.compiler.runner.CompilerRunnerUtil.invokeExecMethod(CompilerRunnerUtil.java:129)
     at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.execCompiler(KotlinCompilerRunner.java:112)
     at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.access$000(KotlinCompilerRunner.java:40)
     at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner$1.fun(KotlinCompilerRunner.java:95)
     at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner$1.fun(KotlinCompilerRunner.java:92)
     at org.jetbrains.jet.compiler.runner.CompilerRunnerUtil.outputCompilerMessagesAndHandleExitCode(CompilerRunnerUtil.java:138)
     at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.runCompiler(KotlinCompilerRunner.java:92)
     at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.runK2JvmCompiler(KotlinCompilerRunner.java:58)
     at org.jetbrains.jet.jps.build.KotlinBuilder.build(KotlinBuilder.java:209)
     at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1106)
     at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:814)
     at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:862)
     at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:777)
     at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:600)
     at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:352)
     at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:184)
     at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:129)
     at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:224)
     at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:113)
     at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:133)
     at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
     at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.UnsupportedOperationException: no descriptor for type constructor of Object
     at org.jetbrains.jet.codegen.state.JetTypeMapper.mapType(JetTypeMapper.java:247)
     at org.jetbrains.jet.codegen.state.JetTypeMapper.mapType(JetTypeMapper.java:202)
     at org.jetbrains.jet.codegen.state.JetTypeMapper.writeParameter(JetTypeMapper.java:739)
     at org.jetbrains.jet.codegen.state.JetTypeMapper.mapSignature(JetTypeMapper.java:574)
     at org.jetbrains.jet.codegen.state.JetTypeMapper.mapSignature(JetTypeMapper.java:562)
     at org.jetbrains.jet.codegen.state.JetTypeMapper.mapToCallableMethod(JetTypeMapper.java:838)
     at org.jetbrains.jet.codegen.ExpressionCodegen.generateConstructorCall(ExpressionCodegen.java:3274)
     at org.jetbrains.jet.codegen.ExpressionCodegen.generateNewCall(ExpressionCodegen.java:3253)
     at org.jetbrains.jet.codegen.ExpressionCodegen.visitCallExpression(ExpressionCodegen.java:1906)
     at org.jetbrains.jet.codegen.ExpressionCodegen.visitCallExpression(ExpressionCodegen.java:84)
     at org.jetbrains.jet.lang.psi.JetCallExpression.accept(JetCallExpression.java:37)
     at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:233)
     … 76 more


#2

I have copied this issue to our tracker (KT-5883). Thank you for the report.