Skip to content

Destructuring bugs (numeric key with default, object rest, extra next()) and async finally { return } #5648

Open

Description

@trackoor

Describe the bug

Three destructuring issues and one async try/catch/finally issue:

  1. Numeric key with a default in an assignment or arrow-parameter object pattern is rejected as a SyntaxError. ObjectLiteral::to_pattern (core/ast/src/expression/literal/object.rs) checks let name = name.literal()?; in the (_, Expression::Assign(assign)) => AssignTarget::Identifier(ident) arm, which returns None for numeric keys because the parser stores numeric property names as PropertyName::Computed(Expression::Literal(...)).
  2. Object rest after a nested pattern with a default does not exclude the key and evaluates its getter twice. In compile_declaration_pattern_impl (core/engine/src/bytecompiler/declaration/declaration_pattern.rs), the ObjectPatternElement::Pattern arm does not push the property key to excluded_keys_registers, so CopyDataProperties copies the property into the rest object again.
  3. Array rest calls next() again even when the iterator is already marked done. iterator_to_array (core/engine/src/bytecompiler/iterator.rs) calls iterator_next before checking iterator_done, ignoring the done flag already set on the iterator record by an earlier destructuring element.
  4. In an async function, finally { return } does not override an exception thrown in catch. When return executes inside finally, JumpRecordKind::Return (core/engine/src/bytecompiler/jump_control.rs) checks MaybeException and rejects the async function's promise capability with the pending exception from catch instead of resolving it with the finally return value.

To Reproduce

// 1. Numeric key with default in assignment / arrow parameter / for-of
let v;
({ 0: v = 5 } = []);
// SyntaxError: Invalid left-hand side in assignment at line 2, col 15

console.log((({ 0: a = 6 }) => a)([]));
// SyntaxError: invalid object binding pattern in formal parameter list at line 1, col 14

for ({ 7: v = 1 } of [{}]) {}
// SyntaxError: invalid object destructuring pattern in iterable loop initializer
// 2. Object rest after a nested pattern with a default
let n = 0;
const { b: { c } = {}, ...rest } = { get b() { n++; return {}; }, c: 3 };
console.log(JSON.stringify(rest), n);
// Boa:      {"b":{},"c":3} 2
// Expected: {"c":3} 1

// 3. Array rest calls next() again after done
let calls = 0;
const it = { [Symbol.iterator]() { return this; }, next() { calls++; return { done: true }; } };
const [x, ...y] = it;
console.log(calls);
// Boa:      2
// Expected: 1

// 4. Async function finally { return } after catch rethrow
async function a() { try { throw 1; } catch (e) { throw e; } finally { return 2; } }
a().then((v) => console.log("ok", v), (e) => console.log("rejected", e));
// Boa:      rejected 1
// Expected: ok 2

Expected behavior

  • Case 1 parses and evaluates v to 5, a to 6, and v to 1 (var { 0: v = 5 } = [], function f({ 0: v = 5 }), ({ "0": v = 5 } = []), and ({ 0: v } = []) already work).
  • Case 2 prints {"c":3} 1 (RestBindingInitialization excludes all bound property keys).
  • Case 3 prints 1 (BindingRestElement only steps the iterator when iteratorRecord.[[Done]] is false).
  • Case 4 prints ok 2 (in TryStatement evaluation, an abrupt return completion from Finally replaces the abrupt throw completion from Catch; synchronous functions and generators in Boa already return 2).

Build environment (please complete the following information):

  • OS: Debian GNU/Linux 12 (bookworm)
  • Version: main (bfaa15a7d6, 2026-11-09) and v0.22.0 release binary
  • Target triple: x86_64-unknown-linux-gnu
  • Rustc version: rustc 1.98.1 (48a229cea 2026-09-01)

Additional context

Happy to split these into separate issues if preferred.

Related: labelled break out of a nested for…of

break L out of an inner for…of ends the enclosing for…of

const r = [];
for (const k of ["a", "b", "c"]) {
  L: { for (const x of [0]) { break L; } }
  r.push(k);
}
console.log(r.join());   // "a", expected "a,b,c"

Same with for…in inside, or L: on a loop instead of a block. Outer for (let i…) is fine. With L: do { … } while (false) as the target the program never terminates.

Credit: https://ucsbplse.github.io/

Activity

  1. Tushar15769 commented on Nov 10, 2026

    @Tushar15769

    Hey there @trackoor !!
    I'd like to work and take a look at Bug 3 in this issue, where array destructuring seems to call next() again even after the iterator is already done. I'll try my best fixing that behaviour and add a test to make sure next() is only called once in this case, without affecting normal array destructuring.

  2. tarushmohindru commented on Nov 27, 2026

    @tarushmohindru
    Contributor

    Hi! I'd like to work on Bug 4 (async finally { return } not overriding a catch rethrow) if no one else has picked it up.

    I see #5656 covers Bug 3 and the labelled-break case is being tracked in #5671, so this shouldn't overlap with either. I'll link the PR here once it's up.

  3. Surri757 commented on Nov 29, 2026

    @Surri757

    Hi @trackoor, thanks for the very detailed report. I'd like to take Bug 1 — the numeric key with a default in an assignment or arrow-parameter object pattern being rejected as a SyntaxError (the ObjectLiteral::to_pattern issue in core/ast/src/expression/literal/object.rs). I've reproduced all three cases from the issue (({ 0: v = 5 } = []), (({ 0: a = 6 }) => a)([]), and for ({ 7: v = 1 } of [{}]) {}) and I'm working on a fix with tests. This shouldn't overlap with #5656 (Bug 3) or #5695 (Bug 4) — I'll link the PR here once it's ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions