Skip to content

Commit 5c58e38

Browse files
cknittclaude
andcommitted
Represent array and list literals with spreads with a Pexp_spread node
The parser no longer turns [a, ...b] and list{...a, b} into calls of Primitive_array.spread and Primitive_list.spread marked with res.spread. Pexp_spread keeps the items and spreads in source order, and the type checker gives it a Texp_spread node. Translation emits new Parray_spread and Plist_spread primitives that compile to the same runtime calls. A list literal whose only spread is its tail stays a chain of ::. Before (rescript): let x = [...[1, 2], 3, ...[]] let y = @attr list{...xs, ...list{}, 1} After (rescript): let x = [...[1, 2], 3, ...[]] let y = @attr list{...xs, ...list{}, 1} Source: the old formatter printed [1, 2, 3, ] and list{...xs, , 1} without the attribute; the new one prints the input unchanged (tests/syntax_tests/data/printer/expr). Review: Why do the generated item groups have ghost locations? The v0 encoding has no marker on them, and the ghost flag tells a group of items from an array or list literal that is spread. A call built by a PPX with real locations decodes to spreads only, which concatenates the same values. Review: Why does the typed node hold the concatenated parts rather than the elements? The parts are the arrays and cons chains the runtime call takes, so the items keep the errors of array and list literals and translation needs no list building of its own. Area: syntax printer typechecker lambda codegen analysis reanalyze tests Output: unchanged, tests/tests JS byte-identical after a rebuild; a combinatorial harness of all item/spread sequences up to length 4 compiles to identical JS with the old and new bsc Formatting: changed spreads of array and list literals and attributes on literals with spreads are kept; comments in list spreads stay in place Formats: ast cmt PPX: unchanged wire: the v0 encoding is the res.spread call it was, decoded back to Pexp_spread (ast-mapping fixture SpreadLiterals.res) Editor: unchanged: pipe completion types the literal as the runtime call; SpreadLiterals.res covers AST dump, hints, highlighting and completion Tests: +tests/syntax_tests/data/ast-mapping/SpreadLiterals.res +tests/analysis_tests/tests/src/SpreadLiterals.res +tests/build_tests/super_errors/fixtures/array_spread_type_mismatch.res +tests/build_tests/super_errors/fixtures/list_spread_type_mismatch.res +tests/build_tests/super_errors/fixtures/array_spread_annotation_mismatch.res ~tests/ounit_tests/ounit_ast_mapper0_tests.ml ~tests/syntax_tests/data/printer/expr/array.res ~tests/syntax_tests/data/printer/expr/list.res ~tests/syntax_tests/data/printer/comments/array.res Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EUpL6dKCiSwAeriDGRHqNr Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 1eb91bb commit 5c58e38

61 files changed

Lines changed: 761 additions & 294 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎analysis/reanalyze/src/arnold.ml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,8 @@ module Compile = struct
995995
|> List.map (function Typedtree.Tdict_entry (_, e) | Tdict_spread e ->
996996
expression ~ctx e)
997997
|> Command.unordered_sequence
998+
| Texp_spread (_, parts) ->
999+
parts |> List.map (expression ~ctx) |> Command.unordered_sequence
9981000
| Texp_assert _ -> Command.nothing
9991001
| Texp_try (e, cases) ->
10001002
let c_e = e |> expression ~ctx in

‎analysis/reanalyze/src/side_effects.ml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ let rec expr_no_side_effects (expr : Typedtree.expression) =
6262
| Typedtree.Tdict_entry (_, e) -> e |> expr_no_side_effects
6363
(* Spreading a dict reads its properties *)
6464
| Tdict_spread _ -> false)
65+
(* A call of the runtime's [spread] *)
66+
| Texp_spread _ -> false
6567
| Texp_ifthenelse (e1, e2, eo) ->
6668
e1 |> expr_no_side_effects && e2 |> expr_no_side_effects
6769
&& eo |> expr_opt_no_side_effects

‎analysis/src/completion_front_end.ml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ let rec expr_to_context_path_inner ~(in_jsx_context : bool)
231231
| [] -> None
232232
| exp :: _ -> expr_to_context_path ~in_jsx_context exp))
233233
| Pexp_dict _ -> Some (CPDict None)
234+
| Pexp_spread (kind, _) ->
235+
(* Typed like the runtime call it compiles to, as when the parser
236+
represented it as [Pexp_apply] *)
237+
Some
238+
(CPApply
239+
( CPId
240+
{
241+
path = [Ast_spread.module_name kind; "spread"];
242+
completion_context = Value;
243+
loc = e.pexp_loc;
244+
},
245+
[Nolabel] ))
234246
| Pexp_ident {txt = Lident "->"} -> None
235247
| Pexp_ident {txt; loc} ->
236248
Some

‎analysis/src/dump_ast.ml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,23 @@ and print_expr_item expr ~pos ~indentation =
222222
^ "\n"
223223
^ add_indentation indentation
224224
^ ")"
225+
| Pexp_spread (kind, elements) ->
226+
(match kind with
227+
| Spread_array -> "Pexp_spread(array,\n"
228+
| Spread_list -> "Pexp_spread(list,\n")
229+
^ (elements
230+
|> List.map (fun element ->
231+
add_indentation (indentation + 1)
232+
^
233+
match element with
234+
| Parsetree.Pspread_item expr ->
235+
print_expr_item expr ~pos ~indentation:(indentation + 1)
236+
| Pspread expr ->
237+
"..." ^ print_expr_item expr ~pos ~indentation:(indentation + 1))
238+
|> String.concat "\n")
239+
^ "\n"
240+
^ add_indentation indentation
241+
^ ")"
225242
| Pexp_match (match_expr, cases) ->
226243
"Pexp_match("
227244
^ print_expr_item match_expr ~pos ~indentation:0

‎analysis/src/hint.ml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ let inlay ~source ~kind_file ~pos ~max_length ~full ~state ~debug =
5959
match expr.pexp_desc with
6060
| Pexp_constant _ | Pexp_tuple _ | Pexp_record _ | Pexp_variant _
6161
| Pexp_apply _ | Pexp_match _ | Pexp_construct _ | Pexp_ifthenelse _
62-
| Pexp_ternary _ | Pexp_array _ | Pexp_dict _ | Pexp_ident _ | Pexp_try _
63-
| Pexp_object_get _ | Pexp_object_set _ | Pexp_field _ | Pexp_open _
64-
| Pexp_fun _ | Pexp_template _ | Pexp_tagged_template _ ->
62+
| Pexp_ternary _ | Pexp_array _ | Pexp_dict _ | Pexp_spread _
63+
| Pexp_ident _ | Pexp_try _ | Pexp_object_get _ | Pexp_object_set _
64+
| Pexp_field _ | Pexp_open _ | Pexp_fun _ | Pexp_template _
65+
| Pexp_tagged_template _ ->
6566
push vb.pvb_pat.ppat_loc Type
6667
| _ -> ())
6768
| {pvb_pat = {ppat_desc = Ppat_tuple _}} -> process_pattern vb.pvb_pat

‎analysis/src/semantic_tokens.ml‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,6 @@ let command ~debug ~emitter ~source ~kind_file =
257257
(* Don't emit semantic tokens for identifiers not present in source code *)
258258
let should_emit =
259259
match lid with
260-
(* Collection spreads (`...`) are converted to primitive spread calls
261-
with an `@res.spread` decorator. *)
262-
| Ldot (Lident ("Primitive_array" | "Primitive_list"), "spread") ->
263-
let has_spread_attr =
264-
e.pexp_attributes
265-
|> List.exists (fun ({Location.txt}, _) -> txt = "res.spread")
266-
in
267-
not has_spread_attr
268260
(* Array access (`arr[index]`) is converted to `Array.get` *)
269261
| Ldot (Lident "Array", "get") -> false
270262
(* Array mutation (`arr[index]`) is converted to `Array.set` *)

‎analysis/src/utils.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ let identify_pexp pexp =
123123
| Pexp_jsx_element _ -> "Pexp_jsx_element"
124124
| Pexp_regexp _ -> "Pexp_regexp"
125125
| Pexp_dict _ -> "Pexp_dict"
126+
| Pexp_spread _ -> "Pexp_spread"
126127
| Pexp_template _ -> "Pexp_template"
127128
| Pexp_tagged_template _ -> "Pexp_tagged_template"
128129

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Report a spread of the wrong collection type, such as a list spread into an array, as a mismatch of the spread instead of an array item.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix the formatter dropping spreads of array and list literals, attributes on literals with spreads, and comments in list literals with spreads.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Represent array and list literals with spreads with a dedicated parsetree node.

0 commit comments

Comments
 (0)