Fix JSON switch to correctly handle null and arrays without explicit …#8254
Closed
jagguji wants to merge 1 commit intorescript-lang:masterfrom
Closed
Fix JSON switch to correctly handle null and arrays without explicit …#8254jagguji wants to merge 1 commit intorescript-lang:masterfrom
jagguji wants to merge 1 commit intorescript-lang:masterfrom
Conversation
457e489 to
28e65d2
Compare
…cases
When switching on JSON.t values without explicit Null or Array cases,
both null and arrays were incorrectly matching the Object case because
in JavaScript:
- typeof null === "object"
- typeof [] === "object"
Changes:
- Modified compiler/core/lam_compile.ml to add null/array guards when
the switch has an Object clause but no explicit Null/Array clauses
- Added tests/tests/src/JSONSwitchNullCheck_test.res with comprehensive
test cases covering all scenarios
Fixes scenarios like:
switch jsonNull {
| Object(_) => "Object"
| Array(_) => "Array"
| _ => "default" // null was incorrectly matching Object
}
The fix generates guards like:
if (x !== null && !Array.isArray(x)) {
switch (typeof x) { case "object": ... }
}
Signed-Off-By: Claude Opus 4.6 <noreply@anthropic.com>
28e65d2 to
8064fa9
Compare
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.
…cases
When switching on JSON.t values without explicit Null or Array cases, both null and arrays were incorrectly matching the Object case because in JavaScript:
Changes:
Fixes scenarios like:
switch jsonNull { | Object() => "Object" | Array() => "Array" | _ => "default" // null was incorrectly matching Object }
The fix generates guards like:
if (x !== null && !Array.isArray(x)) { switch (typeof x) { case "object": ... } }
Cases
switch jsonNull { | Object() => "Object" | Array() => "Array" | _ => "default" }
switch jsonNull { | Object() => "Object" | String() => "String" | _ => "default" }
switch jsonNull { | Object() => "Object" | Number() => "Number" | _ => "default" }
switch jsonNull->JSON.Classify.classify { | Object() => "Object" | Bool() => "Boolean" | _ => "default" }
switch jsonNull { | Object() => "Object" | Array() => "Array" | String() => "String" | _ => "default" }
switch jsonArray { | Object() => "Object" | String() => "String" | _ => "default" }
switch jsonArray { | Object() => "Object" | Number() => "Number" | _ => "default" }
switch jsonArray { | Object() => "Object" | Bool(_) => "Boolean" | _ => "default" }
Which are failing