Skip to content

Commit 1004d78

Browse files
authored
Improve array pattern spread error message (#7549)
* Mention Array.slice instead of Array.sub and Belt.Array.slice in array spread error * Improve error message grammar * Improve array pattern spread error message * Add CHANGELOG entry * Update snapshot tests
1 parent e81e1ff commit 1004d78

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
- Improve doc comment formatting to match the style of multiline comments. https://github.com/rescript-lang/rescript/pull/7529
5353
- Improve error messages around type mismatches for try/catch, if, for, while, and optional record fields + optional function arguments. https://github.com/rescript-lang/rescript/pull/7522
5454
- sync Reanalyze with the new APIs around exception. https://github.com/rescript-lang/rescript/pull/7536
55+
- Improve array pattern spread error message. https://github.com/rescript-lang/rescript/pull/7549
5556

5657
#### :house: Internal
5758

compiler/syntax/src/res_core.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module ErrorMessages = struct
6161
matching currently guarantees to never create new intermediate data."
6262

6363
let record_pattern_spread =
64-
"Record's `...` spread is not supported in pattern matches.\n\
64+
"Record spread (`...`) is not supported in pattern matches.\n\
6565
Explanation: you can't collect a subset of a record's field into its own \
6666
record, since a record needs an explicit declaration and that subset \
6767
wouldn't have one.\n\
@@ -70,13 +70,14 @@ module ErrorMessages = struct
7070
[@@live]
7171

7272
let array_pattern_spread =
73-
"Array's `...` spread is not supported in pattern matches.\n\
74-
Explanation: such spread would create a subarray; out of performance \
75-
concern, our pattern matching currently guarantees to never create new \
76-
intermediate data.\n\
77-
Solution: if it's to validate the first few elements, use a `when` clause \
78-
+ Array size check + `get` checks on the current pattern. If it's to \
79-
obtain a subarray, use `Array.sub` or `Belt.Array.slice`."
73+
"Array spread (`...`) is not supported in pattern matches.\n\n\
74+
Explanation: Allowing `...` here would require creating a new subarray at \
75+
match time, but for performance reasons pattern matching is guaranteed to \
76+
never create intermediate data.\n\n\
77+
Possible solutions:\n\
78+
- To validate specific elements: Use `if` with length checks and \
79+
`Array.get`\n\
80+
- To extract a subarray: Use `Array.slice`"
8081

8182
let record_expr_spread =
8283
"Records can only have one `...` spread, at the beginning.\n\

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
2 │
77
3 │ let record = {...x, ...y}
88

9-
Array's `...` spread is not supported in pattern matches.
10-
Explanation: such spread would create a subarray; out of performance concern, our pattern matching currently guarantees to never create new intermediate data.
11-
Solution: if it's to validate the first few elements, use a `when` clause + Array size check + `get` checks on the current pattern. If it's to obtain a subarray, use `Array.sub` or `Belt.Array.slice`.
9+
Array spread (`...`) is not supported in pattern matches.
10+
11+
Explanation: Allowing `...` here would require creating a new subarray at match time, but for performance reasons pattern matching is guaranteed to never create intermediate data.
12+
13+
Possible solutions:
14+
- To validate specific elements: Use `if` with length checks and `Array.get`
15+
- To extract a subarray: Use `Array.slice`
1216

1317

1418
Syntax error!
@@ -33,7 +37,7 @@ Explanation: since records have a known, fixed shape, a spread like `{a, ...b}`
3337
5 │
3438
6 │ let list{...x, ...y} = myList
3539

36-
Record's `...` spread is not supported in pattern matches.
40+
Record spread (`...`) is not supported in pattern matches.
3741
Explanation: you can't collect a subset of a record's field into its own record, since a record needs an explicit declaration and that subset wouldn't have one.
3842
Solution: you need to pull out each field you want explicitly.
3943

tests/syntax_tests/data/parsing/recovery/expression/expected/list.res.txt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
7 ┆ | [x, ...rest] => [x, ...loop(rest)]
99
8 ┆ | [] => []
1010

11-
Array's `...` spread is not supported in pattern matches.
12-
Explanation: such spread would create a subarray; out of performance concern, our pattern matching currently guarantees to never create new intermediate data.
13-
Solution: if it's to validate the first few elements, use a `when` clause + Array size check + `get` checks on the current pattern. If it's to obtain a subarray, use `Array.sub` or `Belt.Array.slice`.
11+
Array spread (`...`) is not supported in pattern matches.
12+
13+
Explanation: Allowing `...` here would require creating a new subarray at match time, but for performance reasons pattern matching is guaranteed to never create intermediate data.
14+
15+
Possible solutions:
16+
- To validate specific elements: Use `if` with length checks and `Array.get`
17+
- To extract a subarray: Use `Array.slice`
1418

1519

1620
Syntax error!
@@ -22,9 +26,13 @@ Solution: if it's to validate the first few elements, use a `when` clause + Arra
2226
8 ┆ | [] => []
2327
9 ┆ }
2428

25-
Array's `...` spread is not supported in pattern matches.
26-
Explanation: such spread would create a subarray; out of performance concern, our pattern matching currently guarantees to never create new intermediate data.
27-
Solution: if it's to validate the first few elements, use a `when` clause + Array size check + `get` checks on the current pattern. If it's to obtain a subarray, use `Array.sub` or `Belt.Array.slice`.
29+
Array spread (`...`) is not supported in pattern matches.
30+
31+
Explanation: Allowing `...` here would require creating a new subarray at match time, but for performance reasons pattern matching is guaranteed to never create intermediate data.
32+
33+
Possible solutions:
34+
- To validate specific elements: Use `if` with length checks and `Array.get`
35+
- To extract a subarray: Use `Array.slice`
2836

2937
let flags =
3038
((if reasonFormat

tests/syntax_tests/data/parsing/recovery/pattern/expected/record.res.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
4 │ | {a, _, b} => ()
2020
5 │ }
2121

22-
Record's `...` spread is not supported in pattern matches.
22+
Record spread (`...`) is not supported in pattern matches.
2323
Explanation: you can't collect a subset of a record's field into its own record, since a record needs an explicit declaration and that subset wouldn't have one.
2424
Solution: you need to pull out each field you want explicitly.
2525

0 commit comments

Comments
 (0)