Compiler bug? Caused by: java.lang.NegativeArraySizeException


#1

File being compiled and position: (14,5) in /Users/cbeust/kotlin/klaxon/src/main/kotlin/com/beust/klaxon/Json.kt The root cause was thrown at: null:-1   at org.jetbrains.jet.codegen.FunctionCodegen.endVisit(FunctionCodegen.java:442)   at org.jetbrains.jet.codegen.FunctionCodegen.generateMethodHeaderAndBody(FunctionCodegen.java:164)   at org.jetbrains.jet.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:104)   at org.jetbrains.jet.codegen.FunctionCodegen.gen(FunctionCodegen.java:79)   at org.jetbrains.jet.codegen.MemberCodegen.genFunctionOrProperty(MemberCodegen.java:46)   at org.jetbrains.jet.codegen.ClassBodyCodegen.generateDeclaration(ClassBodyCodegen.java:86)   at org.jetbrains.jet.codegen.ImplementationBodyCodegen.generateDeclaration(ImplementationBodyCodegen.java:1538)   at org.jetbrains.jet.codegen.ClassBodyCodegen.generateClassBody(ClassBodyCodegen.java:78)   at org.jetbrains.jet.codegen.ClassBodyCodegen.generate(ClassBodyCodegen.java:59)   at org.jetbrains.jet.codegen.MemberCodegen.genImplementation(MemberCodegen.java:83)   at org.jetbrains.jet.codegen.MemberCodegen.genClassOrObject(MemberCodegen.java:137)   at org.jetbrains.jet.codegen.NamespaceCodegen.generateClassOrObject(NamespaceCodegen.java:173)   at org.jetbrains.jet.codegen.NamespaceCodegen.generate(NamespaceCodegen.java:124)   at org.jetbrains.jet.codegen.NamespaceCodegen.generate(NamespaceCodegen.java:96)   at org.jetbrains.jet.codegen.KotlinCodegenFacade.generateNamespace(KotlinCodegenFacade.java:73)   at org.jetbrains.jet.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:62)   at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.java:361)   at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:300)   at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:273)   at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileBunchOfSources(KotlinToJVMBytecodeCompiler.java:182)   at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:153)   at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:52)   at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:128)   at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execute(KotlinCompileMojoBase.java:185)   at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)   at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)   at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)   at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)   at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)   at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)   at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)   at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)   at java.lang.reflect.Method.invoke(Method.java:597)   at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)   at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)   at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)   at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352) Caused by: java.lang.NegativeArraySizeException   at org.jetbrains.asm4.Frame.a(Unknown Source)   at org.jetbrains.asm4.MethodWriter.visitMaxs(Unknown Source)   at org.jetbrains.jet.codegen.FunctionCodegen.endVisit(FunctionCodegen.java:436)

package com.beust.klaxon

import java.util.ArrayList

abstract class Factory<T> {
  abstract fun create() : T
}

abstract class J

abstract class JObject : J() {
  val children = ArrayList<J>()

  protected fun initTag<T : J>(init : T.() -> Unit) : T
           where class object T : Factory<T> {
  val tag = T.create()
  tag.init()
  children.add(tag)
  return tag
  }
}

class JSON : JObject() {
  class object : Factory<JSON>() {
  override fun create() = JSON()
  }

  fun obj(init : JObject.() -> Unit) = initTag(init)
}

fun json(init : JSON.() -> Unit) : JSON {
  val json = JSON()
  json.init()
  return json
}

//class TextElement(val text : String) : JObject()
//
//abstract class JString(val string : String) : JObject() {
//
//   fun String.plus() {
//   children.add(TextElement(this))
//   }
//}


#2

Yes, it is a bug. Please report it to the tracker.


#3

Done: http://youtrack.jetbrains.com/issue/KT-3562

This is preventing me from creating a DSL like the one shown in the documentation (the HTML builder).


#4

THe problem is in the class object bound:

  protected fun initTag<T : J>(init : T.() -> Unit) : T            where class object T : Factory<T> {

Do not use it, and your DSL will be OK. Our experience with Kara shows that there’s no real need in this particular way of doing builders.

``

package com.beust.klaxon

import java.util.ArrayList

abstract class Factory<T> {
  abstract fun create() : T
}

abstract class J

abstract class JObject : J() {
  val children = ArrayList<J>()

  protected fun initTag<T : J>(tag: T, init : T.() -> Unit) : T {
  tag.init()
  children.add(tag)
  return tag
  }
}

class JSON : JObject() {
  fun obj(init : JObject.() -> Unit) = initTag(JSON(), init)
}

fun json(init : JSON.() -> Unit) : JSON {
  val json = JSON()
  json.init()
  return json
}