From 3bbde1a89cf252a05dfc5631da22fbbcf68a9312 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 21 Sep 2024 12:25:55 +0200 Subject: [PATCH 1/2] add some context to error message when unit is expected --- .../expected/function_call_mismatch.res.expected | 5 ++++- .../super_errors/expected/warnings2.res.expected | 5 ++++- jscomp/ml/error_message_utils.ml | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/jscomp/build_tests/super_errors/expected/function_call_mismatch.res.expected b/jscomp/build_tests/super_errors/expected/function_call_mismatch.res.expected index d3cee635d3..da64a4f475 100644 --- a/jscomp/build_tests/super_errors/expected/function_call_mismatch.res.expected +++ b/jscomp/build_tests/super_errors/expected/function_call_mismatch.res.expected @@ -9,4 +9,7 @@ 8 │ } This function call returns: string - But it's expected to return: unit \ No newline at end of file + But it's expected to return: unit + + - Did you mean to assign this to a variable? + - If you don't care about the result of this expression, you can assign it to _ via let _ = ... or pipe it to ignore via expression->ignore \ No newline at end of file diff --git a/jscomp/build_tests/super_errors/expected/warnings2.res.expected b/jscomp/build_tests/super_errors/expected/warnings2.res.expected index 324f794b04..e9bdee54fb 100644 --- a/jscomp/build_tests/super_errors/expected/warnings2.res.expected +++ b/jscomp/build_tests/super_errors/expected/warnings2.res.expected @@ -8,4 +8,7 @@ 4 │ } This has type: int - But it's expected to have type: unit \ No newline at end of file + But it's expected to have type: unit + + - Did you mean to assign this to a variable? + - If you don't care about the result of this expression, you can assign it to _ via let _ = ... or pipe it to ignore via expression->ignore \ No newline at end of file diff --git a/jscomp/ml/error_message_utils.ml b/jscomp/ml/error_message_utils.ml index d178f2129f..9ebf2c984f 100644 --- a/jscomp/ml/error_message_utils.ml +++ b/jscomp/ml/error_message_utils.ml @@ -165,6 +165,15 @@ let print_extra_type_clash_help ppf trace type_clash_context = \ - Use a tuple, if your array is of fixed length. Tuples can mix types \ freely, and compiles to a JavaScript array. Example of a tuple: `let \ myTuple = (10, \"hello\", 15.5, true)" + | ( _, + [ + ({Types.desc = Tconstr (_p1, _, _)}, _); ({desc = Tconstr (p2, _, _)}, _); + ] ) + when Path.same Predef.path_unit p2 -> + fprintf ppf + "\n\n\ + \ - Did you mean to assign this to a variable?\n\ + \ - If you don't care about the result of this expression, you can assign it to @{_@} via @{let _ = ...@} or pipe it to @{ignore@} via @{expression->ignore@}\n\n" | _ -> () let type_clash_context_from_function sexp sfunct = From dbf5e340c3f2f464ebc5af70b065ea4114fed2af Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 21 Sep 2024 12:28:01 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4272471bd2..355bea035b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ - Improve output of `@variadic` bindings. https://github.com/rescript-lang/rescript-compiler/pull/7030 - Improve error messages around JSX components. https://github.com/rescript-lang/rescript-compiler/pull/7038 - Improve output of record copying. https://github.com/rescript-lang/rescript-compiler/pull/7043 +- Provide additional context in error message when `unit` is expected. https://github.com/rescript-lang/rescript-compiler/pull/7045 # 12.0.0-alpha.3