Skip to content

Commit af43e11

Browse files
authored
Suggest awaiting promise before using it when type mismatches (#7498)
* suggest awaiting promise before using it when type mismatches * format * changelog
1 parent 64624e2 commit af43e11

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497
1818

19+
#### :nail_care: Polish
20+
21+
- Suggest awaiting promise before using it when types mismatch. https://github.com/rescript-lang/rescript/pull/7498
22+
1923
# 12.0.0-alpha.13
2024

2125
#### :boom: Breaking Change

compiler/ml/error_message_utils.ml

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ let is_record_type ~extract_concrete_typedecl ~env ty =
6262
| _ -> false
6363
with _ -> false
6464

65-
let print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf trace
65+
let print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf
66+
(bottom_aliases : (Types.type_expr * Types.type_expr) option)
6667
type_clash_context =
67-
match (type_clash_context, trace) with
68+
match (type_clash_context, bottom_aliases) with
6869
| Some (MathOperator {for_float; operator; is_constant}), _ -> (
6970
let operator_for_other_type =
7071
match operator with
@@ -86,12 +87,8 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf trace
8687
| _ -> "compute"
8788
in
8889
(* TODO check int vs float explicitly before showing this *)
89-
(match (operator, trace) with
90-
| ( "+",
91-
[
92-
({Types.desc = Tconstr (p1, _, _)}, _);
93-
({desc = Tconstr (p2, _, _)}, _);
94-
] )
90+
(match (operator, bottom_aliases) with
91+
| "+", Some ({Types.desc = Tconstr (p1, _, _)}, {desc = Tconstr (p2, _, _)})
9592
when Path.same Predef.path_string p1 || Path.same Predef.path_string p2 ->
9693
fprintf ppf
9794
"\n\n\
@@ -111,7 +108,7 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf trace
111108
@{<info>Belt.Float.toInt@} and @{<info>Belt.Int.fromFloat@}."
112109
operator_text
113110
(if for_float then "float" else "int"));
114-
match (is_constant, trace) with
111+
match (is_constant, bottom_aliases) with
115112
| Some constant, _ ->
116113
if for_float then
117114
fprintf ppf
@@ -125,11 +122,8 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf trace
125122
\ - Make @{<info>%s@} an @{<info>int@} by removing the dot or \
126123
explicitly converting to int"
127124
constant
128-
| ( _,
129-
[
130-
({Types.desc = Tconstr (p1, _, _)}, _);
131-
({desc = Tconstr (p2, _, _)}, _);
132-
] ) -> (
125+
| _, Some ({Types.desc = Tconstr (p1, _, _)}, {desc = Tconstr (p2, _, _)})
126+
-> (
133127
match (Path.name p1, Path.name p2) with
134128
| "float", "int" | "int", "float" ->
135129
fprintf ppf
@@ -171,18 +165,15 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf trace
171165
\ - Use a tuple, if your array is of fixed length. Tuples can mix types \
172166
freely, and compiles to a JavaScript array. Example of a tuple: `let \
173167
myTuple = (10, \"hello\", 15.5, true)"
174-
| ( _,
175-
[
176-
({Types.desc = Tconstr (_p1, _, _)}, _); ({desc = Tconstr (p2, _, _)}, _);
177-
] )
168+
| _, Some ({Types.desc = Tconstr (_p1, _, _)}, {desc = Tconstr (p2, _, _)})
178169
when Path.same Predef.path_unit p2 ->
179170
fprintf ppf
180171
"\n\n\
181172
\ - Did you mean to assign this to a variable?\n\
182173
\ - If you don't care about the result of this expression, you can \
183174
assign it to @{<info>_@} via @{<info>let _ = ...@} or pipe it to \
184175
@{<info>ignore@} via @{<info>expression->ignore@}\n\n"
185-
| _, [({desc = Tobject _}, _); (({Types.desc = Tconstr _} as t1), _)]
176+
| _, Some ({desc = Tobject _}, ({Types.desc = Tconstr _} as t1))
186177
when is_record_type ~extract_concrete_typedecl ~env t1 ->
187178
fprintf ppf
188179
"\n\n\
@@ -191,6 +182,9 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf trace
191182
\ - Did you mean to pass a record instead of an object? Objects are \
192183
written with quoted keys, and records with unquoted keys. Remove the \
193184
quotes from the object keys to pass it as a record instead of object. \n\n"
185+
| _, Some ({Types.desc = Tconstr (p1, _, _)}, _)
186+
when Path.same p1 Predef.path_promise ->
187+
fprintf ppf "\n\n - Did you mean to await this promise before using it?\n"
194188
| _ -> ()
195189

196190
let type_clash_context_from_function sexp sfunct =

compiler/ml/typecore.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ let print_expr_type_clash ?type_clash_context env trace ppf =
784784
(function
785785
| ppf -> error_type_text ppf type_clash_context)
786786
(function ppf -> error_expected_type_text ppf type_clash_context);
787-
print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf trace
788-
type_clash_context;
787+
print_extra_type_clash_help ~extract_concrete_typedecl ~env ppf
788+
bottom_aliases_result type_clash_context;
789789
show_extra_help ppf env trace
790790

791791
let report_arity_mismatch ~arity_a ~arity_b ppf =
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/promise_needs_await.res:7:3-5
4+
5+
5 │ let x = () => {
6+
6 │ let res = Promise.resolve({one: "hi"})
7+
7 │ res.one
8+
8 │ }
9+
9 │
10+
11+
This has type: Promise.t<record> (defined as promise<record>)
12+
But it's expected to have type: record
13+
14+
- Did you mean to await this promise before using it?
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type record = {one: string}
2+
3+
external getRecord: unit => promise<record> = "getRecord"
4+
5+
let x = () => {
6+
let res = Promise.resolve({one: "hi"})
7+
res.one
8+
}

0 commit comments

Comments
 (0)