From 628013adfc16306180d854028c1d4cfe974e8d4c Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 5 Nov 2026 05:55:31 +0000 Subject: [PATCH] Start a recursive array record as an empty array Fable review: a recursive value starts as an empty placeholder that is filled in afterwards, and the placeholder of every record was `{}`, so a `let rec` of an array record stayed an object. It is now `[]` for an array record. An array record in an arrow body also no longer gets the parentheses an object literal needs. JS modified (recursive array record): Example: type rec cell = {@as("0") value: int, @as("1") next: cell} let rec cell = {value: 1, next: cell} Before (js): let cell = {}; cell[0] = 1; cell[1] = cell; After (js): let cell = []; cell[0] = 1; cell[1] = cell; Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gLWSz9kGHhX9sue9WB Signed-off-by: Christoph Knittel --- changes/array-record-let-rec.fix.md | 1 + compiler/core/js_dump.ml | 5 ++- compiler/core/js_exp_make.ml | 7 +-- tests/tests/src/array_record_test.mjs | 64 ++++++++++++++++++--------- tests/tests/src/array_record_test.res | 18 ++++++++ 5 files changed, 69 insertions(+), 26 deletions(-) create mode 100644 changes/array-record-let-rec.fix.md diff --git a/changes/array-record-let-rec.fix.md b/changes/array-record-let-rec.fix.md new file mode 100644 index 0000000000..9a5fe3055c --- /dev/null +++ b/changes/array-record-let-rec.fix.md @@ -0,0 +1 @@ +- Build a recursive `let rec` value of a record whose fields have the runtime names `"0"`, `"1"`, … as a JavaScript array instead of an object. diff --git a/compiler/core/js_dump.ml b/compiler/core/js_dump.ml index c8ad659754..de9d66f0ce 100644 --- a/compiler/core/js_dump.ml +++ b/compiler/core/js_dump.ml @@ -222,8 +222,9 @@ let rec exp_need_paren ?(arrow = false) (e : J.expression) = | Caml_block ( _, _, - ( Blk_record _ | Blk_module _ | Blk_poly_var | Blk_extension - | Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ ) ) + ( Blk_record {array = false} + | Blk_module _ | Blk_poly_var | Blk_extension | Blk_record_ext _ + | Blk_record_inlined _ | Blk_constructor _ ) ) | Object _ -> true | Json_literal _ -> true diff --git a/compiler/core/js_exp_make.ml b/compiler/core/js_exp_make.ml index 74678ad054..2ab7b5595d 100644 --- a/compiler/core/js_exp_make.ml +++ b/compiler/core/js_exp_make.ml @@ -366,10 +366,11 @@ let dummy_obj ?comment (info : Lambda.tag_info) : t = for other it is [[]] *) match info with - | Blk_record _ | Blk_module _ | Blk_constructor _ | Blk_record_inlined _ - | Blk_poly_var | Blk_extension | Blk_record_ext _ -> + | Blk_record {array = false} + | Blk_module _ | Blk_constructor _ | Blk_record_inlined _ | Blk_poly_var + | Blk_extension | Blk_record_ext _ -> {comment; source_loc = None; expression_desc = Object []} - | Blk_tuple | Blk_module_export _ -> + | Blk_record {array = true} | Blk_tuple | Blk_module_export _ -> {comment; source_loc = None; expression_desc = Array []} (* TODO: complete diff --git a/tests/tests/src/array_record_test.mjs b/tests/tests/src/array_record_test.mjs index 7b5bc63f3a..35d86d7573 100644 --- a/tests/tests/src/array_record_test.mjs +++ b/tests/tests/src/array_record_test.mjs @@ -93,6 +93,19 @@ function split(r) { ]; } +let constantCell = []; + +constantCell[0] = 1; + +constantCell[1] = constantCell; + +function makeCell(value) { + let cell = []; + cell[0] = value; + cell[1] = cell; + return cell; +} + function classify(p) { if (typeof p === "string") { return "text"; @@ -104,7 +117,14 @@ function classify(p) { } Mocha.describe("Array_record_test", () => { - Mocha.test("an untagged array record payload dispatches as an array", () => Test_utils.eq("File \"array_record_test.res\", line 108, characters 6-13", [ + Mocha.test("a recursive array record is an array", () => { + Test_utils.ok("File \"array_record_test.res\", line 116, characters 7-14", Array.isArray(constantCell)); + Test_utils.eq("File \"array_record_test.res\", line 117, characters 7-14", 1, 1); + let cell = makeCell(7); + Test_utils.ok("File \"array_record_test.res\", line 119, characters 7-14", Array.isArray(cell)); + Test_utils.eq("File \"array_record_test.res\", line 120, characters 7-14", cell[1][0], 7); + }); + Mocha.test("an untagged array record payload dispatches as an array", () => Test_utils.eq("File \"array_record_test.res\", line 126, characters 6-13", [ [ 1, 2 @@ -126,8 +146,8 @@ Mocha.describe("Array_record_test", () => { r_0, 2 ]; - Test_utils.ok("File \"array_record_test.res\", line 116, characters 7-14", Array.isArray(r)); - Test_utils.eq("File \"array_record_test.res\", line 117, characters 7-14", [ + Test_utils.ok("File \"array_record_test.res\", line 134, characters 7-14", Array.isArray(r)); + Test_utils.eq("File \"array_record_test.res\", line 135, characters 7-14", [ 1, 2 ], [ @@ -138,8 +158,8 @@ Mocha.describe("Array_record_test", () => { undefined, 2 ]; - Test_utils.ok("File \"array_record_test.res\", line 119, characters 7-14", Array.isArray(r$1)); - Test_utils.eq("File \"array_record_test.res\", line 120, characters 7-14", [ + Test_utils.ok("File \"array_record_test.res\", line 137, characters 7-14", Array.isArray(r$1)); + Test_utils.eq("File \"array_record_test.res\", line 138, characters 7-14", [ undefined, 2 ], [ @@ -149,9 +169,9 @@ Mocha.describe("Array_record_test", () => { }); Mocha.test("spread over a record with 20 fields is an array", () => { let r = updateBig(bigValue); - Test_utils.ok("File \"array_record_test.res\", line 125, characters 7-14", Array.isArray(r)); - Test_utils.eq("File \"array_record_test.res\", line 126, characters 7-14", r.length, 20); - Test_utils.eq("File \"array_record_test.res\", line 127, characters 7-14", [ + Test_utils.ok("File \"array_record_test.res\", line 143, characters 7-14", Array.isArray(r)); + Test_utils.eq("File \"array_record_test.res\", line 144, characters 7-14", r.length, 20); + Test_utils.eq("File \"array_record_test.res\", line 145, characters 7-14", [ r[0], r[1], r[19] @@ -168,8 +188,8 @@ Mocha.describe("Array_record_test", () => { ]; r[0] = 5; let r$1 = updateMutable(r); - Test_utils.ok("File \"array_record_test.res\", line 134, characters 7-14", Array.isArray(r$1)); - Test_utils.eq("File \"array_record_test.res\", line 135, characters 7-14", [ + Test_utils.ok("File \"array_record_test.res\", line 152, characters 7-14", Array.isArray(r$1)); + Test_utils.eq("File \"array_record_test.res\", line 153, characters 7-14", [ r$1[0], r$1[1] ], [ @@ -178,16 +198,16 @@ Mocha.describe("Array_record_test", () => { ]); }); Mocha.test("an empty literal is an array", () => { - Test_utils.ok("File \"array_record_test.res\", line 139, characters 7-14", Array.isArray(emptyValue)); - Test_utils.eq("File \"array_record_test.res\", line 140, characters 7-14", emptyValue[0], undefined); + Test_utils.ok("File \"array_record_test.res\", line 157, characters 7-14", Array.isArray(emptyValue)); + Test_utils.eq("File \"array_record_test.res\", line 158, characters 7-14", emptyValue[0], undefined); }); Mocha.test("a type spread keeps the array layout", () => { - Test_utils.ok("File \"array_record_test.res\", line 144, characters 7-14", Array.isArray(tripleValue)); - Test_utils.eq("File \"array_record_test.res\", line 145, characters 7-14", 3, 3); + Test_utils.ok("File \"array_record_test.res\", line 162, characters 7-14", Array.isArray(tripleValue)); + Test_utils.eq("File \"array_record_test.res\", line 163, characters 7-14", 3, 3); }); Mocha.test("runtime names out of order give an object", () => { - Test_utils.ok("File \"array_record_test.res\", line 149, characters 7-14", !Array.isArray(swappedValue)); - Test_utils.eq("File \"array_record_test.res\", line 150, characters 7-14", [ + Test_utils.ok("File \"array_record_test.res\", line 167, characters 7-14", !Array.isArray(swappedValue)); + Test_utils.eq("File \"array_record_test.res\", line 168, characters 7-14", [ 1, 2 ], [ @@ -207,17 +227,17 @@ Mocha.describe("Array_record_test", () => { return -1; } }; - Test_utils.eq("File \"array_record_test.res\", line 160, characters 7-14", classify([ + Test_utils.eq("File \"array_record_test.res\", line 178, characters 7-14", classify([ 0, 7, undefined ]), 7); - Test_utils.eq("File \"array_record_test.res\", line 161, characters 7-14", classify([ + Test_utils.eq("File \"array_record_test.res\", line 179, characters 7-14", classify([ 1, 7, 9 ]), 9); - Test_utils.eq("File \"array_record_test.res\", line 162, characters 7-14", classify([ + Test_utils.eq("File \"array_record_test.res\", line 180, characters 7-14", classify([ 1, 7, undefined @@ -230,8 +250,8 @@ Mocha.describe("Array_record_test", () => { 3 ]); let rest = match[1]; - Test_utils.eq("File \"array_record_test.res\", line 167, characters 7-14", match[0], 1); - Test_utils.eq("File \"array_record_test.res\", line 168, characters 7-14", [ + Test_utils.eq("File \"array_record_test.res\", line 185, characters 7-14", match[0], 1); + Test_utils.eq("File \"array_record_test.res\", line 186, characters 7-14", [ rest[1], rest[2] ], [ @@ -251,6 +271,8 @@ export { tripleValue, swappedValue, split, + constantCell, + makeCell, classify, } /* Not a pure module */ diff --git a/tests/tests/src/array_record_test.res b/tests/tests/src/array_record_test.res index 1daee06eb4..16553e8dec 100644 --- a/tests/tests/src/array_record_test.res +++ b/tests/tests/src/array_record_test.res @@ -88,6 +88,16 @@ let split = (r: source) => { (s0, rest) } +// A recursive value starts as an empty array that is then filled +type rec cell = {@as("0") value: int, @as("1") next: cell} + +let rec constantCell = {value: 1, next: constantCell} + +let makeCell = value => { + let rec cell = {value, next: cell} + cell +} + // As an untagged payload, an array record is told apart as an array type point = {@as("0") px?: int, @as("1") py?: int} type named = {name: string} @@ -102,6 +112,14 @@ let classify = (p: payload) => } describe(__MODULE__, () => { + test("a recursive array record is an array", () => { + ok(__LOC__, isArray(constantCell)) + eq(__LOC__, constantCell.next.next.value, 1) + let cell = makeCell(7) + ok(__LOC__, isArray(cell)) + eq(__LOC__, cell.next.value, 7) + }) + test("an untagged array record payload dispatches as an array", () => { // Through Array.map, so that the match runs instead of being folded eq(