Skip to content

Switch sometimes (non-deterministically?) results in redundant conditional block #7465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mediremi opened this issue May 12, 2025 · 0 comments

Comments

@mediremi
Copy link
Contributor

ReScript version: v12.0.0-alpha.13

The following code (playground link)

type actionType1 =
  | WithoutPayload1
  | WithPayload1({x: int})

type actionType2 =
  | WithoutPayload2
  | WithPayload2({y: int})
  | WithPayload3({y: int})

type actionType3 =
  | WithPayload4({x: int})
  | WithPayload5({y: int})
  | WithPayload6({x: int})
  | WithPayload7({y: int})
  | WithPayload8({x: int})

type action =
  | ...actionType1
  | ...actionType2
  | ...actionType3

let f = (action: action) => {
  switch action {
  | ...actionType3 => Console.log("hello")
  | _ => ()
  }
  42
}

sometimes gets compiled to

function f(action) {
  if (typeof action !== "object") {
    action === "WithoutPayload1";
  } else {
    switch (action.TAG) {
      case "WithPayload1" :
      case "WithPayload2" :
      case "WithPayload3" :
        break;
      default:
        console.log("hello");
    }
  }
  return 42;
}

I say 'sometimes' as by rerunning the compiler you can get the correct output of

function f(action) {
  if (typeof action === "object") {
    switch (action.TAG) {
      case "WithPayload4" :
      case "WithPayload5" :
      case "WithPayload6" :
      case "WithPayload7" :
      case "WithPayload8" :
        console.log("hello");
        break;
    }
  }
  return 42;
}

See this video:

Kooha-2025-05-12-19-47-57.webm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant