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..76c39a3903 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\ + \ 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 = ""; }; 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..32ea82b9cd --- /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. + Refactor to 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/syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res b/tests/syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res new file mode 100644 index 0000000000..23ce3be0ab --- /dev/null +++ b/tests/syntax_tests/data/parsing/errors/expressions/oldDataLastPipe.res @@ -0,0 +1,3 @@ +// Old data-last pipe +let x = f => f |> String.length +