Skip to content

[12.x] Switch arms returning coerced variant or record constants are merged, returning the wrong value #9385

Open

Description

@illusionalsagacity

Thank you for filing! Check list:

  • Is it a bug? Usage questions should often be asked in the forum instead.
  • Concise, focused, friendly issue title & description.
  • A minimal, reproducible example.
  • OS and browser versions, if relevant.
  • Is it already fixed in master? Yes, master (02a99b7) compiles this correctly. It reproduces on maintenance/12.x and in 12.3.1.

Summary

When switch arms return constant blocks, the compiler merges arms whose blocks have the same tag and fields, even if their constructor names or record field names differ. After a coercion, two such blocks compile to different JavaScript, so the merged function returns the wrong value for some inputs.

Reproduction

type source = X | Y | Z

type small = Second(int)
type big = First(int) | Second(int)

let toBig = (x: source): big =>
  switch x {
  | X => First(1)
  | Y => (Second(1): small :> big)
  | Z => First(1)
  }

type xy = {x: int, y: int}
type yx = {y: int, x: int}

let toYx = (x: source): yx =>
  switch x {
  | X => {y: 1, x: 2}
  | Y => ({x: 1, y: 2}: xy :> yx)
  | Z => {y: 1, x: 2}
  }

Actual output (12.3.1)

All three arms are merged, so toBig(Y) returns First(1) and toYx(Y) returns {y: 1, x: 2}:

function toBig(x) {
  switch (x) {
    case "X" :
    case "Y" :
    case "Z" :
      return {
        TAG: "First",
        _0: 1
      };
  }
}

function toYx(x) {
  switch (x) {
    case "X" :
    case "Y" :
    case "Z" :
      return {
        y: 1,
        x: 2
      };
  }
}

Expected output (as on master)

function toBig(x) {
  switch (x) {
    case "Y" :
      return {
        TAG: "Second",
        _0: 1
      };
    case "X" :
    case "Z" :
      return {
        TAG: "First",
        _0: 1
      };
  }
}

function toYx(x) {
  switch (x) {
    case "Y" :
      return {
        x: 1,
        y: 2
      };
    case "X" :
    case "Z" :
      return {
        y: 1,
        x: 2
      };
  }
}

Cause

A block's tag is its constructor's position in its type. Second is at position 0 in small, as First is in big, and xy and yx records are both tag 0. Two equality checks compare the tag and the fields but ignore tag_info, which holds the constructor name and record field names the block is printed with:

  • Lam_constant.eq_approx (Const_block), which Lam_compile.group_apply uses to merge switch arms, as above;
  • Js_analyzer.eq_expression (Caml_block), which Js_stmt_make.if_ uses to merge if/else branches. For example, (b: bool): big => b ? First(1) : (Second(1): small :> big) returns First(1) for false.

Both need to compare tag_info too.

Found while working on #9317.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions