Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Add spread element check when the last element is a spread. #673

Merged
merged 8 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3716,6 +3716,14 @@ and parseSpreadExprRegion p =
| _ -> None

and parseListExpr ~startPos p =
let check_all_non_spread_exp exprs =
exprs
|> List.map (fun (spread, expr) ->
if spread then
Parser.err p (Diagnostics.message ErrorMessages.listExprSpread);
expr)
|> List.rev
in
let listExprs =
parseCommaDelimitedReversedList p ~grammar:Grammar.ListExpr ~closing:Rbrace
~f:parseSpreadExprRegion
Expand All @@ -3724,17 +3732,10 @@ and parseListExpr ~startPos p =
let loc = mkLoc startPos p.prevEndPos in
match listExprs with
| (true, expr) :: exprs ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add comment true (* spread *)

let exprs = exprs |> List.map snd |> List.rev in
let exprs = check_all_non_spread_exp exprs in
makeListExpression loc exprs (Some expr)
| exprs ->
let exprs =
exprs
|> List.map (fun (spread, expr) ->
if spread then
Parser.err p (Diagnostics.message ErrorMessages.listExprSpread);
expr)
|> List.rev
in
let exprs = check_all_non_spread_exp exprs in
makeListExpression loc exprs None

(* Overparse ... and give a nice error message *)
Expand Down
14 changes: 14 additions & 0 deletions tests/parsing/errors/other/expected/spread.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ Explanation: you can't collect a subset of a record's field into its own record,
Solution: you need to pull out each field you want explicitly.


Syntax error!
tests/parsing/errors/other/spread.res:8:1-3

6 │
7 │ let myList = list{...x, ...y}
8 │ let list{...x, ...y} = myList
9 │
10 │ type t = {...a}

Lists can only have one `...` spread, and at the end.
Explanation: lists are singly-linked list, where a node contains a value and points to the next node. `list[a, ...bc]` efficiently creates a new item and links `bc` as its next nodes. `[...bc, a]` would be expensive, as it'd need to traverse `bc` and prepend each item to `a` one by one. We therefore disallow such syntax sugar.
Solution: directly use `concat`.


Syntax error!
tests/parsing/errors/other/spread.res:8:13-22

Expand Down