Skip to content

Reject await in a function that an async function returns directly - #9169

Merged
mununki merged 1 commit into
masterfrom
fix-await-in-returned-function
Nov 1, 2026
Merged

mununki merged 1 commit into
masterfrom
fix-await-in-returned-function

Conversation

@mununki

@mununki mununki commented Nov 1, 2026

Copy link
Copy Markdown
Member

Fixes: #9168

await in a plain arrow that an async function returns directly passed the await check, so let f = async x => y => await y + x compiled to async function f(x) { return y => await y + x | 0; }, which fails to load with a SyntaxError.

The built-in PPX gave a function in the body of a function the async context of its parent (async_context := (old_in_function_def && !async_context) || async in bs_builtin_ppx.ml). That was needed when async (x, y) => ... was nested Pexp_fun nodes. #8566 kept it, but in the n-ary parsetree a Pexp_fun holds all parameters of its function, so a Pexp_fun in its body is always a function of its own. A function is now in an async context exactly when it is async, and in_function_def, which served only this, is removed.

  • Programs that still compile produce the same JavaScript, since only the await check reads the async context.
  • Newly rejected programs all produced JavaScript with await in a function that is not async, except where the optimizer had removed the await (an unused definition, a branch under a constant false). The same code in any other plain function is already rejected.
  • Still accepted: async (x, y) => await y, async x => async y => await y, await in the async function's own parameter defaults, and @directive on async functions with several parameters (Fix @directive on function level with async and multiple parameters #7977).

Tests: a super_errors fixture for the rejected case, and mocha tests for async functions that return async and plain functions.

Checklist

  • Added a changelog fragment (see changes/README.md), or no release notes are needed and a maintainer should apply changelog:skip.
  • Added tests for the fix or feature.
  • Gave every commit a description below its subject, saying why it is made (see "Commit messages" in CONTRIBUTING.md).
  • Signed off every commit for the DCO (git commit -s).

Why:
    The built-in PPX let a function in the body of an async function
    inherit its async context, so `await` in a plain arrow returned by an
    async function passed the check, and the JavaScript had `await` in a
    function that is not async (#9168). The inheritance comes from the
    curried parsetree, where `async (x, y) => ...` was nested functions;
    in the n-ary parsetree a function in a function's body is a function
    of its own, so each function is now in an async context exactly when
    it is async.

Diagnostic added (await in a returned function):
Example:
    let f = async x => y => await y + x
Before (js): a SyntaxError when the module loads
    async function f(x) {
      return y => await y + x | 0;
    }
After (diagnostic):
    A.res:1:25-31
    Await on expression not in an async context

Review: Does a working program stop compiling? Only one whose `await` in
    such a function was removed by the optimizer, as in an unused
    definition or a branch under a constant `false`; the same code in any
    other plain function is already rejected. Programs that still compile
    give the same JavaScript, since only the await check reads the async
    context.

Signed-off-by: mununki <woonki.moon@gmail.com>
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Reject await in a function that an async function returns directly, as in async x => y => await y, which produced JavaScript with await outside an async function.

Semantic Diff

1 commit · Full page

Reject await in a function that an async function returns directly

1cb2b80 · mununki · Nov 01, 2026, 18:02 UTC

Why

The built-in PPX let a function in the body of an async function inherit its async context, so await in a plain arrow returned by an async function passed the check, and the JavaScript had await in a function that is not async (#9168). The inheritance comes from the curried parsetree, where async (x, y) => ... was nested functions; in the n-ary parsetree a function in a function's body is a function of its own, so each function is now in an async context exactly when it is async.

Diagnostic added (await in a returned function)

let f = async x => y => await y + x

js · Before · a SyntaxError when the module loads

async function f(x) {
  return y => await y + x | 0;
}

diagnostic · After

A.res:1:25-31
Await on expression not in an async context

Review

  • Does a working program stop compiling? Only one whose await in such a function was removed by the optimizer, as in an unused definition or a branch under a constant false; the same code in any other plain function is already rejected. Programs that still compile give the same JavaScript, since only the await check reads the async context.

@mununki
mununki requested review from cknitt and cristianoc November 1, 2026 18:05
@mununki

mununki commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T18:07:27.806206Z 1cb2b80 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 1cb2b80e6a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@codecov

codecov Bot commented Nov 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.67%. Comparing base (f88294f) to head (1cb2b80).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9169      +/-   ##
==========================================
- Coverage   81.67%   81.67%   -0.01%     
==========================================
  Files         455      455              
  Lines       64859    64857       -2     
==========================================
- Hits        52974    52972       -2     
  Misses      11885    11885              
Files with missing lines Coverage Δ
compiler/frontend/bs_builtin_ppx.ml 90.26% <100.00%> (-0.07%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9169

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9169

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9169

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9169

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9169

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9169

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9169

commit: 1cb2b80

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@mununki
mununki merged commit c10223e into master Nov 1, 2026
26 checks passed
@mununki
mununki deleted the fix-await-in-returned-function branch November 1, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

await in a function returned directly by an async function compiles to invalid JavaScript

3 participants