ECMA-262 in Lean

Spec issues & errata · defect of the text · docs/spec-issues/69-ifabruptrejectpromise-in-promise-return.md

69. Synthetic Module Record.Evaluate and AsyncFromSyncIteratorContinuation expand IfAbruptRejectPromise's ? inside : a Promise operations

Location
§16.2.1.8.4.5 Evaluate ( ) of a Synthetic Module Record (#sec-smr-Evaluate), step 14; §27.1.5.4 AsyncFromSyncIteratorContinuation (#sec-asyncfromsynciteratorcontinuation), steps 3, 5, and 8; against §27.5.1.1.1 IfAbruptRejectPromise (#sec-ifabruptrejectpromise), step 2.a, §5.2.4.3 Shorthands for Unwrapping Completion Records (#sec-shorthands-for-unwrapping-completion-records), and §16.2.1.6.1.3 Evaluate ( ) of a Cyclic Module Record (#sec-moduleevaluation), step 9.d.
Kind
completion-type contradiction (? inside an operation whose declared return type is a Promise, not a Completion Record).
Model erratum
IfAbruptRejectPromiseInPromiseReturn, baked-in SpecTec reading literal (spec/spectec/clauses/S16.spectec and S27.spectec); its readings: literal, proposed.
Upstream
Checked 2026-12-01: none found (searched for "IfAbruptRejectPromise" with "sec-smr-Evaluate" and "AsyncFromSyncIteratorContinuation").
On this site
IfAbruptRejectPromiseInPromiseReturn on Spec issues & errata (defect of the text; SpecTec bakes in the literal text)

Text

Evaluate ( ) of a Synthetic Module Record (: a Promise):

13. Let promiseCapability be ! NewPromiseCapability(%Promise%).

14. IfAbruptRejectPromise(result, promiseCapability).

15. Perform ! Call(promiseCapability.[[Resolve]], undefined, « undefined »).

16. Return promiseCapability.[[Promise]].

AsyncFromSyncIteratorContinuation (: a Promise):

1. NOTE: Because promiseCapability is derived from the intrinsic %Promise%, the calls to promiseCapability.[[Reject]] entailed by the use IfAbruptRejectPromise below are guaranteed not to throw.

2. Let done be Completion(IteratorComplete(result)).

3. IfAbruptRejectPromise(done, promiseCapability).

4. Let value be Completion(IteratorValue(result)).

5. IfAbruptRejectPromise(value, promiseCapability).

…

8. IfAbruptRejectPromise(valueWrapper, promiseCapability).

IfAbruptRejectPromise:

1. Assert: value is a Completion Record.

2. If value is an abrupt completion, then

a. Perform ? Call(capability.[[Reject]], undefined, « value.[[Value]] »).

b. Return capability.[[Promise]].

3. Set value to ! value.

§5.2.4.3:

Prefix “?” and “!” are used as shorthands which unwrap Completion Records. “?” is used to propagate an abrupt completion to the caller, or otherwise to unwrap a normal completion.

Problem

Both Evaluate ( ) of a Synthetic Module Record (and its row in §16.2.1.5 #table-abstract-methods-of-module-records) and AsyncFromSyncIteratorContinuation declare return type a Promise — a bare Object sort, not a Completion Record. Yet both invoke IfAbruptRejectPromise(_x_, _promiseCapability_), whose definition in §27.5.1.1.1 expands step 2.a to Perform ? Call(_capability_.[[Reject]], *undefined*, « _value_.[[Value]] »), which by §5.2.4.3 expands to:

1. Let tmp1 be Call(promiseCapability.[[Reject]], undefined, « value.[[Value]] »).

2. Assert: tmp1 is a Completion Record.

3. If tmp1 is an abrupt completion, return tmp1.

Step 3 of that expansion returns _tmp1_ — an abrupt Completion Record — directly from an operation whose header declares : a Promise and whose callers (InnerModuleEvaluation / top-level Evaluate and %AsyncFromSyncIteratorPrototype%.next/return/throw) do not unwrap a Completion Record when expecting a Promise:

  1. In Synthetic Module Record.Evaluate (§16.2.1.8.4.5 step 14), _promiseCapability_ was just created via ! NewPromiseCapability(%Promise%) on the immediately preceding step 13, and step 15 right below already writes Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « *undefined* »). Using IfAbruptRejectPromise in step 14 (without even an explanatory NOTE) is inconsistent with both step 15 and the parallel step 9.d of Cyclic Module Record.Evaluate (§16.2.1.6.1.3), which rejects an intrinsic %Promise% capability directly with Perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _result_.[[Value]] »).
  2. In AsyncFromSyncIteratorContinuation (§27.1.5.4 steps 3, 5, and 8), step 1's NOTE acknowledges that _promiseCapability_ is derived from %Promise% so Call(_promiseCapability_.[[Reject]], …) dynamically never throws (aside from step 1's typo "by the use IfAbruptRejectPromise"), but leaves IfAbruptRejectPromise's ? expansion in place inside a non-Completion algorithm.

Static modular typechecking (ecma-typecheck --errors-only) flags all four sites as abrupt-in-pure-fn. Because %Promise%'s built-in reject function never throws at run time, the abrupt branch of ? Call(…) is dynamically unreachable and no JavaScript program stops there.

Examples

async function collect() {
  const iter = {
    [Symbol.iterator]() {
      return { next() { throw new Error("boom"); } };
    }
  };
  try {
    for await (const _ of iter) {}
  } catch (e) {
    print("caught: " + e.message);
  }
}
collect();
for await over throwing sync iterator
static modular typecheck (ecma-typecheck) abrupt-in-pure-fn on #sec-smr-Evaluate step 14 and #sec-asyncfromsynciteratorcontinuation steps 3, 5, 8
V8, SpiderMonkey, JavaScriptCore caught: boom
formal model (literal and proposed readings) caught: boom

Proposed wording

In Synthetic Module Record.Evaluate step 14 and AsyncFromSyncIteratorContinuation steps 3, 5, and 8 (where _promiseCapability_ is guaranteed to be an intrinsic %Promise% capability), replace IfAbruptRejectPromise with an explicit ! Call(_promiseCapability_.[[Reject]], …) and Return _promiseCapability_.[[Promise]] (matching Cyclic Module Record.Evaluate step 9.d), and drop AsyncFromSyncIteratorContinuation step 1's NOTE:

--- a/spec.html
+++ b/spec.html
@@ -28238,7 +28238,10 @@
               1. Suspend _moduleContext_ and remove it from the execution context stack.
               1. Resume the context that is now on the top of the execution context stack as the running execution context.
               1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
-              1. IfAbruptRejectPromise(_result_, _promiseCapability_).
+              1. If _result_ is an abrupt completion, then
+                1. Perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _result_.[[Value]] »).
+                1. Return _promiseCapability_.[[Promise]].
               1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « *undefined* »).
               1. Return _promiseCapability_.[[Promise]].
             </emu-alg>

test262

language/statements/for-await-of/ exercises AsyncFromSyncIteratorContinuation on abrupt IteratorComplete, IteratorValue, and IteratorClose completions; language/import/import-attributes/ exercises Synthetic Module Record.Evaluate on JSON modules.