Skip to content

Commit 75a5381

Browse files
authored
Better syntax errors for [| and |> (#8008)
* some simple syntax error fixes * move tests * be consistent * slight change of wording
1 parent 6162030 commit 75a5381

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#### :nail_care: Polish
3030

31+
- Dedicated error messages for old Reason array literal syntax (`[|` and `|]`), and for the old pipe (`|>`). Primarly intended to help LLMs that might try to use old code patterns. https://github.com/rescript-lang/rescript/pull/8010
32+
3133
#### :house: Internal
3234

3335
- Rename Core to Stdlib in tests/tests. https://github.com/rescript-lang/rescript/pull/8005

compiler/syntax/src/res_diagnostics.ml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,43 @@ let print_report ?(custom_intro = None) ?(formatter = Format.err_formatter)
137137
match diagnostics with
138138
| [] -> ()
139139
| d :: rest ->
140+
(* A few specializations for best-effort error messages for old syntax etc. *)
141+
let msg =
142+
match d.category with
143+
| Unexpected {token = Token.Bar; _} ->
144+
let idx_prev = d.start_pos.pos_cnum - 1 in
145+
let idx_next = d.end_pos.pos_cnum in
146+
if
147+
idx_prev >= 0
148+
&& idx_prev < String.length src
149+
&& String.get src idx_prev = '['
150+
then
151+
let base = explain d in
152+
base
153+
^ "\n\n\
154+
\ Did you mean to write an array literal? Arrays are written \
155+
with `[ ... ]` (not `[| ... |]`)."
156+
^ "\n Quick fix: replace `[|` with `[` and `|]` with `]`."
157+
^ "\n Example: `[|1, 2, 3|]` -> `[1, 2, 3]`"
158+
else if
159+
idx_next >= 0
160+
&& idx_next < String.length src
161+
&& String.get src idx_next = '>'
162+
then
163+
let base = explain d in
164+
base
165+
^ "\n\n\
166+
\ The old data-last pipe `|>` has been removed from the language.\n\
167+
\ Refactor to use a data-first `->` pipe instead."
168+
else explain d
169+
| _ -> explain d
170+
in
140171
Location.report_error ~custom_intro ~src:(Some src) formatter
141172
Location.
142173
{
143174
loc =
144175
{loc_start = d.start_pos; loc_end = d.end_pos; loc_ghost = false};
145-
msg = explain d;
176+
msg;
146177
sub = [];
147178
if_highlight = "";
148179
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Syntax error!
3+
syntax_tests/data/parsing/errors/expressions/oldArraySyntax.res:2:10
4+
5+
1 │ // Old Reason array literal
6+
2 │ let s = [|1, 2, 3|]
7+
3 │
8+
4 │
9+
10+
I'm not sure what to parse here when looking at "|".
11+
12+
Did you mean to write an array literal? Arrays are written with `[ ... ]` (not `[| ... |]`).
13+
Quick fix: replace `[|` with `[` and `|]` with `]`.
14+
Example: `[|1, 2, 3|]` -> `[1, 2, 3]`
15+
16+
let s = [|1;2;3|]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Syntax error!
3+
syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res:2:16
4+
5+
1 │ // Old data-last pipe
6+
2 │ let x = f => f |> String.length
7+
3 │
8+
4 │
9+
10+
I'm not sure what to parse here when looking at "|".
11+
12+
The old data-last pipe `|>` has been removed from the language.
13+
Refactor to use a data-first `->` pipe instead.
14+
15+
let x [arity:1]f = f
16+
;;String.length
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Old Reason array literal
2+
let s = [|1, 2, 3|]
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Old data-last pipe
2+
let x = f => f |> String.length
3+

0 commit comments

Comments
 (0)