Skip to content

Commit 385ecc8

Browse files
committed
example of detecting and providing rewrite for ReScript object -> record
1 parent 94aceb1 commit 385ecc8

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

compiler/ml/error_message_utils.ml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,35 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
227227
| _, Some ({desc = Tobject _}, ({Types.desc = Tconstr _} as t1))
228228
when is_record_type ~extract_concrete_typedecl ~env t1 ->
229229
fprintf ppf
230-
"\n\n\
231-
\ You're passing a @{<error>ReScript object@} where a @{<info>record@} \
232-
is expected. \n\n\
233-
\ - Did you mean to pass a record instead of an object? Objects are \
234-
written with quoted keys, and records with unquoted keys. Remove the \
235-
quotes from the object keys to pass it as a record instead of object. \n\n"
230+
"@,\
231+
@,\
232+
You're passing a @{<error>ReScript object@} where a @{<info>record@} is \
233+
expected. Objects are written with quoted keys, and records with \
234+
unquoted keys.";
235+
236+
let suggested_rewrite =
237+
Parser.reprint_expr_at_loc loc ~mapper:(fun exp ->
238+
match exp.Parsetree.pexp_desc with
239+
| Pexp_extension
240+
( {txt = "obj"},
241+
PStr
242+
[
243+
{
244+
pstr_desc =
245+
Pstr_eval (({pexp_desc = Pexp_record _} as record), _);
246+
};
247+
] ) ->
248+
Some record
249+
| _ -> None)
250+
in
251+
fprintf ppf
252+
"@,\
253+
@,\
254+
Possible solutions: @,\
255+
- Rewrite the object to a record, like: @{<info>%s@}@,"
256+
(match suggested_rewrite with
257+
| Some rewrite -> rewrite
258+
| None -> "")
236259
| _, Some ({Types.desc = Tconstr (p1, _, _)}, _)
237260
when Path.same p1 Predef.path_promise ->
238261
fprintf ppf "\n\n - Did you mean to await this promise before using it?\n"
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
We've found a bug for you!
3-
/.../fixtures/object_passed_when_record_expected.res:4:14-26
3+
/.../fixtures/object_literal_passed_when_record_expected.res:4:14-26
44

55
2 │ type xx = array<x>
66
3 │
@@ -9,7 +9,9 @@
99

1010
This has type: {"one": bool}
1111
But it's expected to have type: x
12-
13-
You're passing a ReScript object where a record is expected.
14-
15-
- Did you mean to pass a record instead of an object? Objects are written with quoted keys, and records with unquoted keys. Remove the quotes from the object keys to pass it as a record instead of object.
12+
13+
You're passing a ReScript object where a record is expected. Objects are written with quoted keys, and records with unquoted keys.
14+
15+
Possible solutions:
16+
- Rewrite the object to a record, like: {one: true}
17+


0 commit comments

Comments
 (0)