Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#### :nail_care: Polish

- 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

#### :house: Internal

- Rename Core to Stdlib in tests/tests. https://github.com/rescript-lang/rescript/pull/8005
Expand Down
33 changes: 32 additions & 1 deletion compiler/syntax/src/res_diagnostics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,43 @@ let print_report ?(custom_intro = None) ?(formatter = Format.err_formatter)
match diagnostics with
| [] -> ()
| d :: rest ->
(* A few specializations for best-effort error messages for old syntax etc. *)
let msg =
match d.category with
| Unexpected {token = Token.Bar; _} ->
let idx_prev = d.start_pos.pos_cnum - 1 in
let idx_next = d.end_pos.pos_cnum in
if
idx_prev >= 0
&& idx_prev < String.length src
&& String.get src idx_prev = '['
then
let base = explain d in
base
^ "\n\n\
\ Did you mean to write an array literal? Arrays are written \
with `[ ... ]` (not `[| ... |]`)."
^ "\n Quick fix: replace `[|` with `[` and `|]` with `]`."
^ "\n Example: `[|1, 2, 3|]` -> `[1, 2, 3]`"
else if
idx_next >= 0
&& idx_next < String.length src
&& String.get src idx_next = '>'
then
let base = explain d in
base
^ "\n\n\
\ The old data-last pipe `|>` has been removed from the language.\n\
\ Use a data first `->` pipe instead."
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if an LLM would understand this.
Did you test this error message?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it works well in my tests.

else explain d
| _ -> explain d
in
Location.report_error ~custom_intro ~src:(Some src) formatter
Location.
{
loc =
{loc_start = d.start_pos; loc_end = d.end_pos; loc_ghost = false};
msg = explain d;
msg;
sub = [];
if_highlight = "";
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Syntax error!
syntax_tests/data/parsing/errors/expressions/oldArraySyntax.res:2:10

1 │ // Old Reason array literal
2 │ let s = [|1, 2, 3|]
3 │
4 │

I'm not sure what to parse here when looking at "|".

Did you mean to write an array literal? Arrays are written with `[ ... ]` (not `[| ... |]`).
Quick fix: replace `[|` with `[` and `|]` with `]`.
Example: `[|1, 2, 3|]` -> `[1, 2, 3]`

let s = [|1;2;3|]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Syntax error!
syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res:2:16

1 │ // Old data-last pipe
2 │ let x = f => f |> String.length
3 │
4 │

I'm not sure what to parse here when looking at "|".

The old data-last pipe `|>` has been removed from the language.
Use a data first `->` pipe instead.
Copy link
Member

Choose a reason for hiding this comment

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

Those are not interchangeable, so as a user if I natively just swap |> with ->, I'm just heading for the next error.

String.length example only works because there is one argument right?
Might be good to have an example with two or more so it becomes clear how to deal with this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct. I made a small wording change to call it "Refactor to use a ..." instead of "Use a ..." to signify that some form of action is needed. Given it's removed from the language, not just deprecated, I think it's enough. LLMs understand it anyway because they know what -> vs |> is. And, users who end up in this situation can continue to the next error and have something to Google for if they need more info.


let x [arity:1]f = f
;;String.length
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Old Reason array literal
let s = [|1, 2, 3|]

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Old data-last pipe
let x = f => f |> String.length

Loading