Skip to content

Error message for when trying to await something that is not a promise #7561

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

Merged
merged 3 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# 12.0.0-alpha.15 (Unreleased)

#### :nail_care: Polish

- Better error message for when trying to await something that is not a promise. https://github.com/rescript-lang/rescript/pull/7561

# 12.0.0-alpha.14

#### :boom: Breaking Change
Expand Down
19 changes: 19 additions & 0 deletions compiler/ml/ast_await.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ let create_await_expression (e : Parsetree.expression) =
in
Ast_helper.Exp.apply ~loc unsafe_await [(Nolabel, e)]

let is_await_expr (e : Parsetree.expression) =
match e with
| {
pexp_loc = {loc_ghost = true};
pexp_desc =
Pexp_apply
{
funct =
{
pexp_loc = {loc_ghost = true};
pexp_desc = Pexp_ident {txt = Ldot (Lident ident, "unsafe_await")};
};
args = [(Nolabel, _)];
};
}
when ident = Primitive_modules.promise ->
true
| _ -> false

(* Transform `@res.await M` to unpack(@res.await Js.import(module(M: __M0__))) *)
let create_await_module_expression ~module_type_lid (e : Parsetree.module_expr)
=
Expand Down
16 changes: 15 additions & 1 deletion compiler/ml/error_message_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type type_clash_context =
| FunctionArgument of {optional: bool; name: string option}
| Statement of type_clash_statement
| ForLoopCondition
| Await

let context_to_string = function
| Some WhileCondition -> "WhileCondition"
Expand All @@ -120,6 +121,7 @@ let context_to_string = function
| Some (FunctionArgument _) -> "FunctionArgument"
| Some ComparisonOperator -> "ComparisonOperator"
| Some IfReturn -> "IfReturn"
| Some Await -> "Await"
| None -> "None"

let fprintf = Format.fprintf
Expand Down Expand Up @@ -185,6 +187,10 @@ let error_expected_type_text ppf type_clash_context =
"But it's being used with the @{<info>%s@} operator, which works on:"
operator
| Some StringConcat -> fprintf ppf "But string concatenation is expecting:"
| Some Await ->
fprintf ppf
"But you're using @{<info>await@} on this expression, so it is expected \
to be of type:"
| Some MaybeUnwrapOption | None ->
fprintf ppf "But it's expected to have type:"

Expand Down Expand Up @@ -282,6 +288,14 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
"\n\n\
\ To fix this, change the highlighted code so it evaluates to a \
@{<info>bool@}."
| Some Await, _ ->
fprintf ppf
"\n\n\
\ You're trying to await something that is not a promise.\n\n\
Possible solutions:\n\
\ - Remove the @{<info>await@} if this is not expected to be a promise\n\
\ - Wrap the expression in @{<info>Promise.resolve@} to convert the \
expression to a promise"
| Some IfReturn, _ ->
fprintf ppf
"\n\n\
Expand Down Expand Up @@ -533,7 +547,7 @@ let type_clash_context_from_function sexp sfunct =
Some (MathOperator {for_float = true; operator; is_constant})
| Pexp_ident {txt = Lident (("/" | "*" | "+" | "-") as operator)} ->
Some (MathOperator {for_float = false; operator; is_constant})
| _ -> None
| _ -> if Ast_await.is_await_expr sexp then Some Await else None

let type_clash_context_for_function_argument ~label type_clash_context sarg0 =
match type_clash_context with
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

We've found a bug for you!
/.../fixtures/awaiting_non_promise.res:3:15

1 │ let x = 1
2 │
3 │ let f = await x
4 │

This has type: int
But you're using await on this expression, so it is expected to be of type:
promise<'a>

You're trying to await something that is not a promise.

Possible solutions:
- Remove the await if this is not expected to be a promise
- Wrap the expression in Promise.resolve to convert the expression to a promise
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let x = 1

let f = await x