Skip to content

Fix JSON switch to correctly handle null and arrays without explicit …#8254

Closed
jagguji wants to merge 1 commit intorescript-lang:masterfrom
jagguji:fix-json-switch-null-check
Closed

Fix JSON switch to correctly handle null and arrays without explicit …#8254
jagguji wants to merge 1 commit intorescript-lang:masterfrom
jagguji:fix-json-switch-null-check

Conversation

@jagguji
Copy link

@jagguji jagguji commented Feb 25, 2026

…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": ... } }

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

@jagguji jagguji force-pushed the fix-json-switch-null-check branch from 457e489 to 28e65d2 Compare February 25, 2026 10:43
…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>
@jagguji jagguji force-pushed the fix-json-switch-null-check branch from 28e65d2 to 8064fa9 Compare February 25, 2026 10:57
@jagguji jagguji closed this by deleting the head repository Feb 25, 2026
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