Skip to content

Integer range patterns ending at 2147483647 generate incorrect JavaScript #8716

Description

@fhammerschmidt

An integer or-pattern ending at 2147483647 (the maximum signed 32-bit integer) compiles to an impossible bound check and rejects values that should match.

Reproduced with the ReScript 12.3.1 compiler on macOS arm64, using the maintenance/12.x code both before and after the change in #8714.

type response = {status: int}

let inRange = (x: response) =>
  switch x {
  | {status: 2147483645 | 2147483646 | 2147483647} => true
  | _ => false
  }

Generated JavaScript, identical before and after #8714:

function inRange(x) {
  let match = x.status;
  if (match >= 2147483645) {
    return match < -2147483648;
  } else {
    return false;
  }
}

Expected and observed results:

Input status Expected Actual
2147483644 false false
2147483645 true false
2147483646 true false
2147483647 true false

The generated condition requires the same integer to be at least 2147483645 and less than -2147483648, so it can never succeed. This appears to be overflow when converting the exclusive upper bound 2147483647 + 1 into a 32-bit integer literal.

The same failure also reproduces with a plain integer switch, nested records, tuples, variant payloads, arrays, and switches on function-call results.

This was found while reviewing #8714 and is a separate, pre-existing bug: the unsigned-range fix in that PR neither introduces nor fixes it.

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