not sure if this is the right place to raise this....
# k2-adopters
n
not sure if this is the right place to raise this. i found a pretty subtle and nasty Kotlin -> Javascript bug that leads to invalid code generation. it reproduces on 1.9.25 and 2.0.20. the following code shows a minimum repro that results in an invalid
while
loop in JS.
Copy code
fun foo() {
    var value         = 10
    var test          = 23
    var someCondition = true

    do {
        if (someCondition) {
            break
        }

        println("HERE")
    } while (value != test)
}

fun main() {
    foo()
}
this code outputs the following. notice the break is incorporated into the while condition. this is bad b/c it fails to address the case when the condition for the break is actually true upon entering the do portion of the while. in that case, the while should be broken out of before continuing to the
println
. but the generated code does not do that.
Copy code
function foo() {
    var value = 10;
    var test = 23;
    var someCondition = true;

    // Incorrect translation. The break check can actually be false and therefore needs to remain in the do portion of the
    // loop
    $l$loop: do {
      println('HERE');
    } while (!(value === test) && !someCondition);
  }

  function main() {
    foo();
  }
note that adding a statement above the
break
causes the generated code to behave as expected:
Copy code
function foo() {
    var value = 10;
    var test = 23;
    var someCondition = true;
    $l$loop: do {
      if (someCondition) {    // <----------- now the condition is included correctly, allowing for the case of an early break
        println('test');
        break $l$loop;
      }
      println('HERE');
    } while (!(value === test));
  }
  function main() {
    foo();
  }
d
Please report an issue to YouTrack
n
done: KT-73130
thank you color 1