Skip to content

Commit f6b0038

Browse files
authored
Add context to error message when unit is expected (#7045)
* add some context to error message when unit is expected * changelog
1 parent bf4a7ff commit f6b0038

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- Improve output of `@variadic` bindings. https://github.com/rescript-lang/rescript-compiler/pull/7030
2828
- Improve error messages around JSX components. https://github.com/rescript-lang/rescript-compiler/pull/7038
2929
- Improve output of record copying. https://github.com/rescript-lang/rescript-compiler/pull/7043
30+
- Provide additional context in error message when `unit` is expected. https://github.com/rescript-lang/rescript-compiler/pull/7045
3031

3132
#### :house: Internal
3233

jscomp/build_tests/super_errors/expected/function_call_mismatch.res.expected

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
8 │ }
1010

1111
This function call returns: string
12-
But it's expected to return: unit
12+
But it's expected to return: unit
13+
14+
- Did you mean to assign this to a variable?
15+
- 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

jscomp/build_tests/super_errors/expected/warnings2.res.expected

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
4 │ }
99

1010
This has type: int
11-
But it's expected to have type: unit
11+
But it's expected to have type: unit
12+
13+
- Did you mean to assign this to a variable?
14+
- 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

jscomp/ml/error_message_utils.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ let print_extra_type_clash_help ppf trace type_clash_context =
165165
\ - Use a tuple, if your array is of fixed length. Tuples can mix types \
166166
freely, and compiles to a JavaScript array. Example of a tuple: `let \
167167
myTuple = (10, \"hello\", 15.5, true)"
168+
| ( _,
169+
[
170+
({Types.desc = Tconstr (_p1, _, _)}, _); ({desc = Tconstr (p2, _, _)}, _);
171+
] )
172+
when Path.same Predef.path_unit p2 ->
173+
fprintf ppf
174+
"\n\n\
175+
\ - Did you mean to assign this to a variable?\n\
176+
\ - 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"
168177
| _ -> ()
169178

170179
let type_clash_context_from_function sexp sfunct =

0 commit comments

Comments
 (0)