Closed jfrolich closed 6 months ago
cc @cristianoc
Smaller self contained repro:
type obj = {name:string}
@unboxed
type t =
| Boolean(bool)
| Object(obj)
| Array(array<int>)
switch Object({name: "hello"}) {
| Object(v) => Console.log(v)
| Array(v) => Console.log(v)
| _ => Console.log("Not an object or array1")
}
produces:
var v = {
name: "hello"
};
if (Array.isArray(v)) {
console.log(v);
} else {
switch (typeof v) {
case "boolean" :
console.log("Not an object or array1");
break;
case "object" :
}
}
It seems the code for case "object"
is not emitted.
The issue happens when 2 cases, here Object
and Array
, have identical body (here Console.log(v)
).
The switch-generation code, which was not designed with untagged unions in mind, merges the two cases into one (and makes one of the two empty).
However, for Object
and Array
, what's generated is not a straight switch, but a mix of if-then-else
and switch
. This means that the Object
and Array
cases are apart in the generated code, and merging them (making one empty) is wrong.
Repro here