From 57193951cedf40e566edf19b1444622e77934b0c Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 8 Nov 2025 13:22:28 +0100 Subject: [PATCH 1/4] some simple syntax error fixes --- CHANGELOG.md | 2 ++ compiler/syntax/src/res_diagnostics.ml | 33 ++++++++++++++++++- .../expected/old_data_last_pipe.res.expected | 12 +++++++ .../old_reason_array_literal.res.expected | 13 ++++++++ .../fixtures/old_data_last_pipe.res | 2 ++ .../fixtures/old_reason_array_literal.res | 2 ++ 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected create mode 100644 tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/old_data_last_pipe.res create mode 100644 tests/build_tests/super_errors/fixtures/old_reason_array_literal.res diff --git a/CHANGELOG.md b/CHANGELOG.md index b3272d4ce0..4baed8af4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/compiler/syntax/src/res_diagnostics.ml b/compiler/syntax/src/res_diagnostics.ml index 4b6ad8ca69..5def3ae933 100644 --- a/compiler/syntax/src/res_diagnostics.ml +++ b/compiler/syntax/src/res_diagnostics.ml @@ -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." + 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 = ""; }; diff --git a/tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected b/tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected new file mode 100644 index 0000000000..6d355135cf --- /dev/null +++ b/tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected @@ -0,0 +1,12 @@ + + Syntax error! + /.../fixtures/old_data_last_pipe.res:1:16 + + 1 │ let x = f => f |> String.length + 2 │ + 3 │ + + 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. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected b/tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected new file mode 100644 index 0000000000..21ce339acf --- /dev/null +++ b/tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected @@ -0,0 +1,13 @@ + + Syntax error! + /.../fixtures/old_reason_array_literal.res:1:10 + + 1 │ let s = [|1, 2, 3|] + 2 │ + 3 │ + + 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]` \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/old_data_last_pipe.res b/tests/build_tests/super_errors/fixtures/old_data_last_pipe.res new file mode 100644 index 0000000000..4d1b8d1d7b --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/old_data_last_pipe.res @@ -0,0 +1,2 @@ +let x = f => f |> String.length + diff --git a/tests/build_tests/super_errors/fixtures/old_reason_array_literal.res b/tests/build_tests/super_errors/fixtures/old_reason_array_literal.res new file mode 100644 index 0000000000..b809978e89 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/old_reason_array_literal.res @@ -0,0 +1,2 @@ +let s = [|1, 2, 3|] + From 4114f94fc2cbe7aa3c490ce1a10c3258fa984cf1 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 8 Nov 2025 13:28:28 +0100 Subject: [PATCH 2/4] move tests --- .../expected/old_data_last_pipe.res.expected | 12 ------------ .../old_reason_array_literal.res.expected | 13 ------------- .../fixtures/old_reason_array_literal.res | 2 -- .../expressions/expected/oldArraySyntax.res.txt | 16 ++++++++++++++++ .../expressions/expected/oldDataLastPipe.res.txt | 16 ++++++++++++++++ .../errors/expressions/oldArraySyntax.res | 3 +++ .../errors/expressions/oldDataLastPipe.res} | 1 + 7 files changed, 36 insertions(+), 27 deletions(-) delete mode 100644 tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected delete mode 100644 tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected delete mode 100644 tests/build_tests/super_errors/fixtures/old_reason_array_literal.res create mode 100644 tests/syntax_tests/data/parsing/errors/expressions/expected/oldArraySyntax.res.txt create mode 100644 tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt create mode 100644 tests/syntax_tests/data/parsing/errors/expressions/oldArraySyntax.res rename tests/{build_tests/super_errors/fixtures/old_data_last_pipe.res => syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res} (60%) diff --git a/tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected b/tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected deleted file mode 100644 index 6d355135cf..0000000000 --- a/tests/build_tests/super_errors/expected/old_data_last_pipe.res.expected +++ /dev/null @@ -1,12 +0,0 @@ - - Syntax error! - /.../fixtures/old_data_last_pipe.res:1:16 - - 1 │ let x = f => f |> String.length - 2 │ - 3 │ - - 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. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected b/tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected deleted file mode 100644 index 21ce339acf..0000000000 --- a/tests/build_tests/super_errors/expected/old_reason_array_literal.res.expected +++ /dev/null @@ -1,13 +0,0 @@ - - Syntax error! - /.../fixtures/old_reason_array_literal.res:1:10 - - 1 │ let s = [|1, 2, 3|] - 2 │ - 3 │ - - 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]` \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/old_reason_array_literal.res b/tests/build_tests/super_errors/fixtures/old_reason_array_literal.res deleted file mode 100644 index b809978e89..0000000000 --- a/tests/build_tests/super_errors/fixtures/old_reason_array_literal.res +++ /dev/null @@ -1,2 +0,0 @@ -let s = [|1, 2, 3|] - diff --git a/tests/syntax_tests/data/parsing/errors/expressions/expected/oldArraySyntax.res.txt b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldArraySyntax.res.txt new file mode 100644 index 0000000000..9fe6ea36a8 --- /dev/null +++ b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldArraySyntax.res.txt @@ -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|] \ No newline at end of file diff --git a/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt new file mode 100644 index 0000000000..f74fcf3efd --- /dev/null +++ b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt @@ -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. + +let x [arity:1]f = f +;;String.length \ No newline at end of file diff --git a/tests/syntax_tests/data/parsing/errors/expressions/oldArraySyntax.res b/tests/syntax_tests/data/parsing/errors/expressions/oldArraySyntax.res new file mode 100644 index 0000000000..e42c7d5f1c --- /dev/null +++ b/tests/syntax_tests/data/parsing/errors/expressions/oldArraySyntax.res @@ -0,0 +1,3 @@ +// Old Reason array literal +let s = [|1, 2, 3|] + diff --git a/tests/build_tests/super_errors/fixtures/old_data_last_pipe.res b/tests/syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res similarity index 60% rename from tests/build_tests/super_errors/fixtures/old_data_last_pipe.res rename to tests/syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res index 4d1b8d1d7b..23ce3be0ab 100644 --- a/tests/build_tests/super_errors/fixtures/old_data_last_pipe.res +++ b/tests/syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res @@ -1,2 +1,3 @@ +// Old data-last pipe let x = f => f |> String.length From eec3db5fb950384339793d0c3233309020454e99 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 8 Nov 2025 20:17:27 +0100 Subject: [PATCH 3/4] be consistent --- compiler/syntax/src/res_diagnostics.ml | 2 +- .../parsing/errors/expressions/expected/oldDataLastPipe.res.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/syntax/src/res_diagnostics.ml b/compiler/syntax/src/res_diagnostics.ml index 5def3ae933..6f71d1616c 100644 --- a/compiler/syntax/src/res_diagnostics.ml +++ b/compiler/syntax/src/res_diagnostics.ml @@ -164,7 +164,7 @@ let print_report ?(custom_intro = None) ?(formatter = Format.err_formatter) base ^ "\n\n\ \ The old data-last pipe `|>` has been removed from the language.\n\ - \ Use a data first `->` pipe instead." + \ Use a data-first `->` pipe instead." else explain d | _ -> explain d in diff --git a/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt index f74fcf3efd..53c055c2aa 100644 --- a/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt +++ b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt @@ -10,7 +10,7 @@ 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. + Use a data-first `->` pipe instead. let x [arity:1]f = f ;;String.length \ No newline at end of file From bb69ce63c1b4453794760ae09567792d4083161a Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 8 Nov 2025 20:20:32 +0100 Subject: [PATCH 4/4] slight change of wording --- compiler/syntax/src/res_diagnostics.ml | 2 +- .../parsing/errors/expressions/expected/oldDataLastPipe.res.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/syntax/src/res_diagnostics.ml b/compiler/syntax/src/res_diagnostics.ml index 6f71d1616c..76c39a3903 100644 --- a/compiler/syntax/src/res_diagnostics.ml +++ b/compiler/syntax/src/res_diagnostics.ml @@ -164,7 +164,7 @@ let print_report ?(custom_intro = None) ?(formatter = Format.err_formatter) base ^ "\n\n\ \ The old data-last pipe `|>` has been removed from the language.\n\ - \ Use a data-first `->` pipe instead." + \ Refactor to use a data-first `->` pipe instead." else explain d | _ -> explain d in diff --git a/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt index 53c055c2aa..32ea82b9cd 100644 --- a/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt +++ b/tests/syntax_tests/data/parsing/errors/expressions/expected/oldDataLastPipe.res.txt @@ -10,7 +10,7 @@ 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. + Refactor to use a data-first `->` pipe instead. let x [arity:1]f = f ;;String.length \ No newline at end of file