Repository navigation
[12.x] Keep the default of a dense int switch ending in an unreachable case - #9453
Open
illusionalsagacity wants to merge 2 commits into
Open
illusionalsagacity wants to merge 2 commits into
illusionalsagacity wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #9418 to 12.x.
Problem
When the highest cases of a dense
switchon 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 inMatching.as_interval_canfailstopped at the last case instead of the top of the range. Values above it were left out of the interval table, so the generated JSswitchhad no default and returnedundefinedfor them.Before (
f(6, false)isundefined):After:
Fix
As on master:
assert, so it no longer depends on assertions being compiled in.Differences from master:
as_interval_canfailworks on(low, action)pairs, where master has(low, high, action)triples; the change is otherwise the same.if falsedoesn'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'sclassifyTable, 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.rescovers 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 getundefined. No other module intests/testschanges output.Investigated by Claude.