Skip to content

Incorrect code emitted for untagged variant switch case #7289

Closed
@cknitt

Description

@cknitt

This

let printLength = (json: JSON.t) =>
  switch json {
  | Object(o) => Console.log2("Length: ", o->Dict.valuesToArray->Array.length)
  | _ => ()
  }

compiles to

function printLength(json) {
  if (typeof json !== "object" || Array.isArray(json)) {
    return;
  }
  console.log("Length: ", Object.values(json).length);
}

which is incorrect because a check for null is missing.

It will crash at runtime if passed a null value.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions