Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to error message when unit is expected #7045

Merged
merged 2 commits into from
Sep 23, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -9,4 +9,7 @@
8 │ }

This function call returns: string
But it's expected to return: unit
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
Original file line number Diff line number Diff line change
@@ -8,4 +8,7 @@
4 │ }

This has type: int
But it's expected to have type: unit
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
9 changes: 9 additions & 0 deletions jscomp/ml/error_message_utils.ml
Original file line number Diff line number Diff line change
@@ -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 @{<info>_@} via @{<info>let _ = ...@} or pipe it to @{<info>ignore@} via @{<info>expression->ignore@}\n\n"
| _ -> ()

let type_clash_context_from_function sexp sfunct =