Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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\
\ Refactor to use a data-first `->` pipe instead."
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.
Refactor to 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.

let x = "x" |> String.replace("abc", "a")
let y = "x" -> String.replace("abc","a", _)

May have also been relevant to add. Not sure if that always works though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, but it's quite tricky what to suggest imo. That certainly works, but it implies continuing to use a data last API, which we don't really want to promote. But if this turns out to be an issue we can mull a bit on what example to use, if any.

Copy link
Member

Choose a reason for hiding this comment

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

That is fair though, we do want to move away from this, so that makes sense.


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