Skip to content

Commit f65c1af

Browse files
committed
fix issue (rescript-lang#670)
1 parent a40b8b6 commit f65c1af

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/res_core.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3716,6 +3716,13 @@ and parseSpreadExprRegion p =
37163716
| _ -> None
37173717

37183718
and parseListExpr ~startPos p =
3719+
let check_all_non_spread_exp exprs =
3720+
exprs
3721+
|> List.map (fun (spread, expr) ->
3722+
if spread then
3723+
Parser.err p (Diagnostics.message ErrorMessages.listExprSpread);
3724+
expr)
3725+
|> List.rev in
37193726
let listExprs =
37203727
parseCommaDelimitedReversedList p ~grammar:Grammar.ListExpr ~closing:Rbrace
37213728
~f:parseSpreadExprRegion
@@ -3724,16 +3731,10 @@ and parseListExpr ~startPos p =
37243731
let loc = mkLoc startPos p.prevEndPos in
37253732
match listExprs with
37263733
| (true, expr) :: exprs ->
3727-
let exprs = exprs |> List.map snd |> List.rev in
3734+
let exprs = check_all_non_spread_exp exprs in
37283735
makeListExpression loc exprs (Some expr)
37293736
| exprs ->
3730-
let exprs =
3731-
exprs
3732-
|> List.map (fun (spread, expr) ->
3733-
if spread then
3734-
Parser.err p (Diagnostics.message ErrorMessages.listExprSpread);
3735-
expr)
3736-
|> List.rev
3737+
let exprs = check_all_non_spread_exp exprs
37373738
in
37383739
makeListExpression loc exprs None
37393740

tests/parsing/errors/other/expected/spread.res.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ Explanation: you can't collect a subset of a record's field into its own record,
4949
Solution: you need to pull out each field you want explicitly.
5050

5151

52+
Syntax error!
53+
tests/parsing/errors/other/spread.res:8:1-3
54+
55+
6 │
56+
7 │ let myList = list{...x, ...y}
57+
8 │ let list{...x, ...y} = myList
58+
9 │
59+
10 │ type t = {...a}
60+
61+
Lists can only have one `...` spread, and at the end.
62+
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.
63+
Solution: directly use `concat`.
64+
65+
5266
Syntax error!
5367
tests/parsing/errors/other/spread.res:8:13-22
5468

0 commit comments

Comments
 (0)