A JS backend bug... workaround?

Any idea what might be causing this? I'll work on trying to narrow it down.

I have code like :

  for (p in people) {
  …
  td {
  a {   
           href = “doSomething?id=${p.id}”
           className = “red”
           +p.name
  }
  }

Those functions td and a are builders, taking extension function/closures as their last argument.  The generated js is doubled, as you see below, and it crashes.  p_0 is undefined.

  f_18: function (p) {
           return function () {
           this.href = ‘doSomething?id=’ + p.id;
           this.className = ‘red’;
           .mymod.plus(p.name);
           this.href = ‘doSomething?id=’ + p_0.id;
           this.className = ‘red’;                   
           
.mymod.plus(p_0.name);
           };
  },

Rob

Could you provide more informations?   - your project or minimal code for reproduce;   - compiler version.

It looks like bug, feel free to create issue in our tracker with details.

Thanks!

I could not reproduce it on kotlin-demo with:

trait People {   val id: Int   val name: String }

trait A {
  var href: String
  var className: String
  fun String.plus()
}

trait TD {
  fun a(f: A.() -> Unit)
}

fun td(f: TD.() -> Unit) {}

fun test(people: List<People>) {
  for (p in people) {
  td {
  a {   
           href = “doSomething?id=${p.id}”
           className = “red”
           +p.name
  }
  }
  }
}

I sent you some code via email. Didn't want to post here in case I accidentally include too much.

Rob

I can't reproduce problem with your project too.

Please tell us your Kotlin compiler version.
And please check the code that I wrote above on your environment.

Thanks.

I'm running IDEA 13 CE, Kotlin plugin 0.8.11.  Your example code above compiles fine and does not produce the duplicate JS.

Rob

There are some warnings about core/javautil.kt that always appear when I run the JS backend. I figured I could ignore them, but just in case there is something is wrong with my installation, here is the compiler output:

Information:Kotlin: Kotlin JPS plugin version 0.8.11
Information:Kotlin: Using kotlinHome=/Users/rob/Library/Application Support/IdeaIC13/Kotlin/kotlinc
Information:Kotlin: Invoking K2JSCompiler with arguments -tags -verbose -version -printArgs -output /Users/rob/Workspace/KotlinBugs/Bug1/out/production/Bug1/Bug1.js -libraryFiles /Users/rob/Workspace/KotlinBugs/Bug1/lib/kotlin-jslib.jar -sourceFiles /Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/HtmlBuilders.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/Misc.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/ProjectList.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/Theirs.kt,/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/bugs/Bugs.kt
Information:Kotlin: Kotlin Compiler version 0.8.11
Information:Module “Bug1” was fully rebuilt due to project configuration/dependencies changes
Information:Compilation completed successfully with 8 warnings in 9 sec
Information:0 errors
Information:8 warnings
Warning:Kotlin: ‘E’ has a nullable upper bound. This means that a value of this type may be null. Using ‘E?’ is likely to mislead the reader (core/javautil.kt (70:25))
Warning:Kotlin: ‘E’ has a nullable upper bound. This means that a value of this type may be null. Using ‘E?’ is likely to mislead the reader (core/javautil.kt (71:25))
Warning:Kotlin: ‘V’ has a nullable upper bound. This means that a value of this type may be null. Using ‘V?’ is likely to mislead the reader (core/javautil.kt (97:42))
Warning:Kotlin: Projection is redundant: the corresponding type parameter of Map has the same variance (core/javautil.kt (100:46))
Warning:Kotlin: ‘V’ has a nullable upper bound. This means that a value of this type may be null. Using ‘V?’ is likely to mislead the reader (core/javautil.kt (101:45))
/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/ProjectList.kt
Warning:(22, 17) Kotlin: Variable ‘row’ is never used
Warning:(41, 9) Kotlin: Variable ‘pl’ is never used
/Users/rob/Workspace/KotlinBugs/Bug1/src/kotlinbug1/Theirs.kt
Warning:(22, 8) Kotlin: Parameter ‘f’ is never used

You are right it's not your problem, it's problem in stdlib.

I simplified your code to:

...

fun test(flag: Boolean) {
  buildTr {
  td {
           a {
           global += “1”
           }
  }
  td {
           a {
           href = “something” + if(flag) “a” else “b”
// workaround:
//           val tmp = if(flag) “a” else “b”
//           href = “something” + tmp
           }
  }
  }
}


This and your code works fine on current master. So You can apply workaround or update your compiler.

Thanks for your report!

Thank you.  I'm curious, so you were able to reproduce the bug in the older version?

Rob

Yes,I reproduced it on IDEA 13 with Kotlin M8.