Rescript-compiler: 5.2.0: Incorrect code generated for ifs nested in switch

Created on 15 Oct 2019  路  3Comments  路  Source: rescript-lang/rescript-compiler

BuckleScript 5.2.0 generates incorrect code in some circumstances. Consider the following code:

let compilerBug = (a, b, c, f) =>
  switch (a, b) {
  | (Some("x"), _)
  | (_, Some("x")) =>
    if (f()) {
      Js.log("Some x, f returns true");
    } else {
      Js.log("Some x, f returns false");
    }
  | _ =>
    if (c) {
      Js.log("No x, c is true");
    } else {
      Js.log("No x, c is false");
    }
  };

compilerBug(Some("x"), None, true, () => true);

This should output "Some x, f returns true", and it does in BuckleScript 5.0.4, see the Reason "Try"-Page which is currently still on 5.0.4.

However, when building and running the same code under 5.2.0, the output is "No x, c is true".

Looking at the compiled code under 5.2.0, we can see that exit is set to 2 if a === "x", but then overwritten again incorrectly when b !== "x":

function compilerBug(a, b, c, f) {
  var exit = 0;
  if (a !== undefined && a === "x") {
    exit = 2;
  }
  exit = b !== undefined && b === "x" ? 2 : 1;
  switch (exit) {
    case 1 :
        if (c) {
          console.log("No x, c");
          return /* () */0;
        } else {
          console.log("No x, not c");
          return /* () */0;
        }
    case 2 :
        if (Curry._1(f, /* () */0)) {
          console.log("Some x, f returns true");
          return /* () */0;
        } else {
          console.log("Some x, f returns false");
          return /* () */0;
        }

  }
}

Most helpful comment

@cknitt Thanks for reporting, we have several bug fixes together and will make a bug fix release soon

All 3 comments

@cknitt Thanks for reporting, we have several bug fixes together and will make a bug fix release soon

@cknitt can you confirm if it is fixed in master

@bobzhang Thanks a lot for the very quick fix! I can confirm that the generated code works correctly on master. 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bobzhang picture bobzhang  路  4Comments

TheSpyder picture TheSpyder  路  5Comments

bobzhang picture bobzhang  路  3Comments

wyze picture wyze  路  3Comments

alexfedoseev picture alexfedoseev  路  5Comments