Skip to content

Commit a091db1

Browse files
cknittclaude
andcommitted
Move the v0 spread encoding into the PPX bridge and test spread literals
The res.spread call is only the v0 PPX wire format, so it now lives in the bridge: Ast_mapper_to0 builds it (spread_call, spread_marker) and Ast_mapper_from0 decodes it, with the module-name lookup local to the decoder. Ast_spread keeps what the type checker and analysis use: groups, parts and module_name. The ghost locations of the generated parts stay, as for any node the compiler makes up. Behaviour is unchanged. The new mocha test runs array literals with several spreads, items between them, a leading item and an empty spread, list literals with spreads before the tail and a tail-only spread, and checks that both kinds evaluate their items and spreads left to right; its generated JavaScript pins the Primitive_array.spread and Primitive_list.spread calls and the order of their parts. Area: compiler/ml tests Output: unchanged Tests: +tests/tests/src/spread_literal_test.res Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent ca4983f commit a091db1

5 files changed

Lines changed: 170 additions & 31 deletions

File tree

‎compiler/ml/ast_mapper_from0.ml‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,13 @@ module E = struct
679679
| _ -> Option.map rows (dict_rows ~marker:Ast_dict.literal_marker e)
680680

681681
(* The kind and elements of an array or list literal with spreads encoded as
682-
a call, see [Ast_spread] *)
682+
a call, see [Ast_mapper_to0.spread_call] *)
683683
let spread_elements (e : expression) =
684+
let kind_of_module_name name =
685+
if name = Primitive_modules.array then Some Pt.Spread_array
686+
else if name = Primitive_modules.list then Some Pt.Spread_list
687+
else None
688+
in
684689
let generated (e : expression) =
685690
e.pexp_loc.loc_ghost && e.pexp_attributes = []
686691
in
@@ -710,11 +715,11 @@ module E = struct
710715
( Asttypes.Noloc.Nolabel,
711716
{pexp_desc = Pexp_array parts; pexp_attributes = []} );
712717
] )
713-
when txt = Ast_spread.marker -> (
714-
match Ast_spread.kind_of_module_name module_name with
715-
| Some Spread_array ->
718+
when txt = Ast_mapper_to0.spread_marker -> (
719+
match kind_of_module_name module_name with
720+
| Some Pt.Spread_array ->
716721
Some (Pt.Spread_array, List.concat_map array_part parts)
717-
| Some Spread_list ->
722+
| Some Pt.Spread_list ->
718723
Some (Pt.Spread_list, List.concat_map list_part parts)
719724
| None -> None)
720725
| _ -> None

‎compiler/ml/ast_mapper_to0.ml‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@ let remove_ppx_context_string_attr attrs =
124124

125125
let map_loc sub {loc; txt} = {loc = sub.location sub loc; txt}
126126

127+
(* v0 has no array or list literals with spreads, [Pexp_spread]: they're the
128+
calls of [Primitive_array.spread] and [Primitive_list.spread] they used to be
129+
parsed into, whose [spread] carries the [res.spread] marker. The argument is
130+
an array of the [Ast_spread.parts]; [Ast_mapper_from0] decodes the parts
131+
with ghost locations back into the items. *)
132+
let spread_marker = "res.spread"
133+
134+
let spread_call ~loc ~attrs kind elements =
135+
Ast_helper.Exp.apply ~loc ~attrs
136+
(Ast_helper.Exp.ident ~loc
137+
~attrs:[(Location.mknoloc spread_marker, PStr [])]
138+
(Location.mkloc
139+
(Longident.Ldot
140+
(Longident.Lident (Ast_spread.module_name kind), "spread"))
141+
loc))
142+
[
143+
( Asttypes.Nolabel,
144+
Ast_helper.Exp.array ~loc (Ast_spread.parts kind elements) );
145+
]
146+
127147
(* Internal Parsetree0 bridge metadata; public res.* attributes pass through. *)
128148
let record_rest_attr_name = "_res.record_rest"
129149
let constructor_args_attr_name = "_res.constructor_args"
@@ -708,8 +728,7 @@ module E = struct
708728
(* v0 has no spread literals: they're the calls they used to be parsed
709729
into *)
710730
sub.expr sub
711-
(Ast_spread.to_call ~loc:e.pexp_loc ~attrs:e.pexp_attributes kind
712-
elements)
731+
(spread_call ~loc:e.pexp_loc ~attrs:e.pexp_attributes kind elements)
713732
| Pexp_ifthenelse (e1, e2, e3) ->
714733
ifthenelse ~loc ~attrs (sub.expr sub e1) (sub.expr sub e2)
715734
(map_opt (sub.expr sub) e3)

‎compiler/ml/ast_spread.ml‎

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
(* Array and list literals with spreads, [Pexp_spread]: [[a, ...b]] and
2-
[list{...a, b}]. They used to be parsed into calls of the runtime's
3-
[Primitive_array.spread] and [Primitive_list.spread], which remain their
4-
encoding on the v0 PPX wire and the calls they compile to. The [spread]
5-
carries the [res.spread] marker, and its argument is an array of parts:
2+
[list{...a, b}]. They compile to calls of the runtime's
3+
[Primitive_array.spread] and [Primitive_list.spread], whose argument is an
4+
array of parts:
65
- for arrays, the spreads, and array literals of the items between them;
76
- for lists, the items before each spread consed onto it
87
([a :: b :: spread]), and a list literal of the items after the last one.
9-
The array literals and [::] and [[]] constructors built from the items have
10-
ghost locations, which tells them apart from array and list literals that
11-
are spread. *)
8+
The type checker types a literal as its parts, so the items keep the errors
9+
of array and list literals. The array literals and [::] and [[]]
10+
constructors built from the items have ghost locations, like any node the
11+
compiler makes up; on the v0 PPX wire they also tell the items apart from
12+
array and list literals that are spread, see [Ast_mapper_to0.spread_call]. *)
1213

1314
open Parsetree
1415

15-
let marker = "res.spread"
16-
1716
let module_name = function
1817
| Spread_array -> Primitive_modules.array
1918
| Spread_list -> Primitive_modules.list
2019

21-
let kind_of_module_name name =
22-
if name = Primitive_modules.array then Some Spread_array
23-
else if name = Primitive_modules.list then Some Spread_list
24-
else None
25-
2620
(* The items before each spread with that spread, and the items after the last
2721
spread *)
2822
let groups (elements : spread_element list) :
@@ -83,13 +77,3 @@ let parts kind elements =
8377
items tail;
8478
])
8579
(groups elements)
86-
87-
(* The call encoding the literal [kind] of [elements] at [loc] *)
88-
let to_call ~loc ~attrs kind elements =
89-
Ast_helper.Exp.apply ~loc ~attrs
90-
(Ast_helper.Exp.ident ~loc
91-
~attrs:[(Location.mknoloc marker, PStr [])]
92-
(Location.mkloc
93-
(Longident.Ldot (Longident.Lident (module_name kind), "spread"))
94-
loc))
95-
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc (parts kind elements))]
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Mocha from "mocha";
4+
import * as Test_utils from "./test_utils.mjs";
5+
import * as Stdlib_List from "@rescript/runtime/lib/es6/Stdlib_List.mjs";
6+
import * as Primitive_list from "@rescript/runtime/lib/es6/Primitive_list.mjs";
7+
import * as Primitive_array from "@rescript/runtime/lib/es6/Primitive_array.mjs";
8+
9+
let log = {
10+
contents: /* [] */0
11+
};
12+
13+
function note(s, v) {
14+
log.contents = {hd: s, tl: log.contents};
15+
return v;
16+
}
17+
18+
Mocha.describe("Spread_literal_test", () => {
19+
Mocha.test("array spreads keep items and spreads in order", () => {
20+
let a = [
21+
1,
22+
2
23+
];
24+
let b = [5];
25+
Test_utils.eq("File \"spread_literal_test.res\", line 14, characters 7-14", Primitive_array.spread([
26+
a,
27+
[
28+
3,
29+
4
30+
],
31+
b,
32+
[6]
33+
]), [
34+
1,
35+
2,
36+
3,
37+
4,
38+
5,
39+
6
40+
]);
41+
Test_utils.eq("File \"spread_literal_test.res\", line 15, characters 7-14", Primitive_array.spread([
42+
[0],
43+
a,
44+
[],
45+
b
46+
]), [
47+
0,
48+
1,
49+
2,
50+
5
51+
]);
52+
});
53+
Mocha.test("list spreads keep items and spreads in order", () => {
54+
let xs = {hd: 1, tl: {hd: 2, tl: /* [] */0}};
55+
let ys = {hd: 5, tl: /* [] */0};
56+
Test_utils.eq("File \"spread_literal_test.res\", line 21, characters 7-14", Primitive_list.spread([
57+
xs,
58+
{hd: 3, tl: ys},
59+
{hd: 6, tl: /* [] */0}
60+
]), {hd: 1, tl: {hd: 2, tl: {hd: 3, tl: {hd: 5, tl: {hd: 6, tl: /* [] */0}}}}});
61+
Test_utils.eq("File \"spread_literal_test.res\", line 22, characters 7-14", Primitive_list.spread([
62+
{hd: 0, tl: xs},
63+
/* [] */0,
64+
{hd: 4, tl: ys}
65+
]), {hd: 0, tl: {hd: 1, tl: {hd: 2, tl: {hd: 4, tl: {hd: 5, tl: /* [] */0}}}}});
66+
Test_utils.eq("File \"spread_literal_test.res\", line 23, characters 7-14", {hd: 0, tl: xs}, {hd: 0, tl: {hd: 1, tl: {hd: 2, tl: /* [] */0}}});
67+
});
68+
Mocha.test("items and spreads are evaluated left to right", () => {
69+
log.contents = /* [] */0;
70+
let arr = Primitive_array.spread([
71+
[note("a", 1)],
72+
note("b", [2]),
73+
[note("c", 3)]
74+
]);
75+
let lst = Primitive_list.spread([
76+
{hd: note("d", 4), tl: note("e", {hd: 5, tl: /* [] */0})},
77+
{hd: note("f", 6), tl: /* [] */0}
78+
]);
79+
Test_utils.eq("File \"spread_literal_test.res\", line 30, characters 7-14", [
80+
arr,
81+
lst
82+
], [
83+
[
84+
1,
85+
2,
86+
3
87+
],
88+
{hd: 4, tl: {hd: 5, tl: {hd: 6, tl: /* [] */0}}}
89+
]);
90+
Test_utils.eq("File \"spread_literal_test.res\", line 31, characters 7-14", Stdlib_List.reverse(log.contents), {hd: "a", tl: {hd: "b", tl: {hd: "c", tl: {hd: "d", tl: {hd: "e", tl: {hd: "f", tl: /* [] */0}}}}}});
91+
});
92+
});
93+
94+
export {
95+
log,
96+
note,
97+
}
98+
/* Not a pure module */
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
open Mocha
2+
open Test_utils
3+
4+
let log = ref(list{})
5+
let note = (s, v) => {
6+
log := list{s, ...log.contents}
7+
v
8+
}
9+
10+
describe(__MODULE__, () => {
11+
test("array spreads keep items and spreads in order", () => {
12+
let a = [1, 2]
13+
let b = [5]
14+
eq(__LOC__, [...a, 3, 4, ...b, 6], [1, 2, 3, 4, 5, 6])
15+
eq(__LOC__, [0, ...a, ...[], ...b], [0, 1, 2, 5])
16+
})
17+
18+
test("list spreads keep items and spreads in order", () => {
19+
let xs = list{1, 2}
20+
let ys = list{5}
21+
eq(__LOC__, list{...xs, 3, ...ys, 6}, list{1, 2, 3, 5, 6})
22+
eq(__LOC__, list{0, ...xs, ...list{}, 4, ...ys}, list{0, 1, 2, 4, 5})
23+
eq(__LOC__, list{0, ...xs}, list{0, 1, 2})
24+
})
25+
26+
test("items and spreads are evaluated left to right", () => {
27+
log := list{}
28+
let arr = [note("a", 1), ...note("b", [2]), note("c", 3)]
29+
let lst = list{note("d", 4), ...note("e", list{5}), note("f", 6)}
30+
eq(__LOC__, (arr, lst), ([1, 2, 3], list{4, 5, 6}))
31+
eq(__LOC__, List.reverse(log.contents), list{"a", "b", "c", "d", "e", "f"})
32+
})
33+
})

0 commit comments

Comments
 (0)