Skip to content

[12.x] Keep the default of a dense int switch ending in an unreachable case - #9453

Open
illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-dense-switch-default-12.x
Open

illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-dense-switch-default-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9418 to 12.x.

Problem

When the highest cases of a dense switch on ints or chars map to the failure action (for example a case that can't match, whose action becomes the exit), the trailing failure interval in Matching.as_interval_canfail stopped at the last case instead of the top of the range. Values above it were left out of the interval table, so the generated JS switch had no default and returned undefined for them.

@warning("-11")
let f = (n: int, flag: bool) =>
  switch (n, flag) {
  | (_, true) => 0
  | (0, _) => 10
  | (1, _) => 11
  | (2, _) => 12
  | (3, _) => 13
  | (4, _) => 14
  | (5, true) => 15
  | _ => 99
  }

Before (f(6, false) is undefined):

function f(n, flag) {
  if (flag) {
    return 0;
  }
  if (n < 0) {
    return 99;
  }
  switch (n) {
    case 0 :
      return 10;
    case 1 :
      return 11;
    case 2 :
      return 12;
    case 3 :
      return 13;
    case 4 :
      return 14;
    case 5 :
      return 99;
  }
}

After:

function f(n, flag) {
  if (flag) {
    return 0;
  } else if (n > 4 || n < 0) {
    return 99;
  } else {
    return n + 10 | 0;
  }
}

Fix

As on master:

  • The trailing failure interval runs up to the top of the range, so values above the last case reach the failure action.
  • The store that reserves action index 0 for the failure action moves out of the assert, so it no longer depends on assertions being compiled in.

Differences from master:

  • 12.x's as_interval_canfail works on (low, action) pairs, where master has (low, high, action) triples; the change is otherwise the same.
  • The tests differ. 12.x has no int range patterns, and on 12.x a case guarded by if false doesn't reach the failure action, so master's tests pass on 12.x without the fix. Each case here is made unreachable by an earlier (_, true) arm instead, as in master's classifyTable, and there is an extra case for a char range, which 12.x expands into single cases.

Tests

tests/tests/src/dense_switch_default_test.res covers an unreachable last case after a table, after a hole and after a gap, unreachable first and middle cases, several consecutive unreachable cases, and char tables and ranges. Without the fix, 11 of its checks get undefined. No other module in tests/tests changes output.

Investigated by Claude.

When the highest cases of a dense int or char switch map to the failure
action, the trailing failure interval stopped at the last case instead of
the top of the range. Values above it were left out of the interval table,
so the generated JS switch had no default and returned undefined for them.

End the trailing failure interval at the top of the range. Also store the
failure action outside the assert, so that reserving index 0 for it does
not depend on assertions being compiled in.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Bring the backport in line with the version of rescript-lang#9418 merged on master
(ea323ae). Its change to Matching.as_interval_canfail is the same here,
with the (low, action) pairs 12.x has in place of master's
(low, high, action) triples.

The tests differ from master's. 12.x has no int range patterns, and a
case guarded by `if false` doesn't reach the failure action there, so
master's tests pass on 12.x without the fix. Each case here is guarded by
a second tuple component instead, which fails without the fix, and a char
range case is added, as 12.x expands char ranges into single cases.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant