compiler 0.0.2-preview.1

The Zena self-hosted compiler

Readme Files Dependencies Versions

compiler#

The Zena compiler, written in Zena. It parses, type-checks, and compiles Zena source code to WebAssembly (Wasm GC) and WebAssembly Text (WAT).

Usage#

Parsing a Single File#

Use compiler/parser to parse Zena source text into an AST (Module):

import { parse } from 'compiler/parser';

let ast = parse('export let answer = 42;', 'example.zena');

Loading and Checking a Program#

Use compiler (createCompiler and checkCompilation) with a CompilerHost to resolve imports across a module graph and type-check a program:

import {
  createCompiler, checkCompilation, buildStdlibConfig
} from 'compiler';
import { FsCompilerHost } from 'compiler/fs-host';

let stdlibRoot = '/stdlib';
let host = new FsCompilerHost(stdlibRoot);
let packages = buildStdlibConfig(stdlibRoot, 'host');
let compiler = createCompiler(host, {
  stdlibRoot,
  packages,
});

let result = await compiler.compile({
  entry: '/workspace/main.zena',
  target: 'host',
});
let checked = checkCompilation(result);

Compiling a File to WebAssembly#

Use compiler/compile-file to compile an entry file on disk to a .wasm or .wat output file:

import { compileFile } from 'compiler/compile-file';

let ok = await compileFile({
  entry: 'src/main.zena',
  outPath: 'out/main.wasm',
  stdlibRoot: '/stdlib',
});

Libraries#

  • compiler / compiler/compiler — compiler entry point (Compiler, createCompiler, checkCompilation, ProgramCheckResult)
  • compiler/compile-file — end-to-end file compilation (compileFile, CompileFileOptions)
  • compiler/tokenizer — lexer and token definitions (tokenize, Token, TokenType)
  • compiler/parser — recursive-descent parser (parse, Parser)
  • compiler/ast — AST node definitions (Node, Module, Statement, Expression, TypeAnnotation)
  • compiler/visitor — AST traversal helpers (walk, Visitor)
  • compiler/scope — lexical scope and symbol resolution (ScopeBuilder, ScopeResult, Symbol)
  • compiler/checker — static type checker (checkModule, CheckResult)
  • compiler/semantic-model — semantic queries over a checked module (SemanticModel)
  • compiler/types — compiler type representations (Type, ClassType, FunctionType, typeToString)
  • compiler/diagnostics — compiler diagnostics and source spans (Diagnostic, DiagnosticCode, DiagnosticSeverity, SourceSpan)
  • compiler/package-manifest — zena-package.json manifest and workspace loader (loadWorkspace, loadPackageMap)
  • compiler/module-resolver — package and relative module specifier resolution (ModuleResolver, PackageConfig)
  • compiler/library-loader — module graph loader (LibraryLoader, CompilerHost, SourceFile)
  • compiler/codegen/binary-generator — WebAssembly GC binary emitter (BinaryGenerator)
  • compiler/codegen/wat-generator — WebAssembly Text (WAT) emitter (WatGenerator)

License#

MIT

165.7 KiB Raw
/**
 * Merge-shaped control flow, split out of lowering.zena: match, if
 * (statement and expression), try, while, C-style for, let-condition
 * binds, and the value-node arm helper. These own env save/restore,
 * reachability, and MergeState — possible as a module since #121-#123
 * moved that state and machinery onto the shared core; ForIn (index
 * and iterator forms) and nullish-coalesce joined once the
 * interface-call and optional-chain machinery became core methods
 * and seams. Bodies are streaming's shapes.
 */
import {
  ClassKey, getClassKey, getClassMethodKey, getClassPrivateFieldKey, unwrapAliases,
  pendingSomeClass, parseI64OrU64Literal,
} from '../codegen-utils.zena';
import {
  BindingPattern, BindingProperty, BlockStatement, BooleanLiteral, CatchClause, ClassDeclaration, ClassPattern, Expression, FieldDefinition, ForStatement, Identifier, IfExpression, IfStatement, LetPatternCondition, LiteralPattern, MatchExpression, Node, RecordPattern, Statement, ThrowExpression, TryExpression, TuplePattern, UsingStatement, ShieldedStatement, VariableDeclaration, WhileStatement, ExpressionStatement, NullLiteral, NumberLiteral, StringLiteral, BinaryExpression, CallExpression, ForInStatement, IdentifierPattern, IndexExpression, MemberExpression, RangeExpression,
  hasOptionalChainStep,
} from '../../ast.zena';
import {
  ArrayType, BoolLiteralType, ClassType, HoleType, InlineTupleType, sameNumericLiteralRaw, InterfaceType, TupleType, NumLiteralType, NullType, I32Type, RecordType, Type, UnionType, VoidType, baseType, isVoidOrNeverType, NeverType, arrayElementType, isSignedNarrowIntType, isOwnershipWrapper, ownershipHandleInner, ownershipHandleOf, soleNonNullMember, UnionType, isScopedType, scopedInner, releaseMemberOf, scopedFieldNames, OwnershipHandle, holdsOwner, typeContainsScoped
} from '../../types.zena';
import { Symbol, SymbolId } from '../../scope/symbol.zena';
import {
  ValType, ValTypeI32, ValTypeI8, ValTypeI16, ValTypeRef, ValTypeRefNull,
  ValTypeExternRef, ValTypeOptRef, ValTypeVoid, WasmType, isNullableAnyRef,
  isRefValType, refTargetOf,
} from '../wasm.zena';
import {
  ClassCodegenInfo, RecordDispatchInfo, WasmFunction, WasmGlobal, WasmStruct,
  WasmTag, asWasmArray, isValSubtype,
} from '../wasm-module.zena';
import {
  eraseTypeParameters, findExtendedWellKnownInterface,
  findImplementedWellKnownInterface, getNonNullableType,
  typeToValTypeWithSub, InlineLayout, inlineLayoutOf, inlineRecordUnionMembers, iteratorInterfaceOf,
  stepInlineValue, scopedFieldTypesAt, getCellStruct,
} from '../type-mapping.zena';
import { IrBody, IrOp, IrRefField, IrRefFunc, IrRefGlobal, IrRefTag } from './ir.zena';
import { lowerContractClauses } from './contracts.zena';
import { emitGeneratorDispose } from './generators.zena';
import { boxInline, heldInline, lowerIntoSlots, memberTest, returnInlineTail, coalesceArmAdaptation, exactLiteralFields, inSuspensionBody, lowerExpr, lowerExprRaw, lowerOptionalChainCoalesce, lowerStmt, multiSource, MultiValues, inlineMembersMatching, pathElements, representativeMember, tupleRestIndexOf, unboxTuples, packHeldRecord, returnSlotVt, suspendOnFuture, throwStmt, varDecl } from './lowering.zena';
import {
  FinallyScope, LetCondBinds, LoopCtx, LoweringContext, MergeState, ResultSlots, ZirUnsupported, noValues,
} from './lowering-context.zena';
import {
  branchUnless, lowerPatternBindings, lowerPatternTest, maskBitTest,
  patternBinds, patternRefutable, propTypeAdmitsNull,
  recordDispatchOfValue,
} from './patterns.zena';
import { WellKnown, isWellKnown } from '../../well-known.zena';
import { walkEnter } from '../../visitor.zena';
import { HashMap, HashSet } from 'zena:collections';
import { growable } from 'zena:core';

/** Bail with the lowering-side message shape (reason @function). */
function bail(cx: LoweringContext, reason: String): never {
  throw new ZirUnsupported(`${reason} @${cx.func.name ?? '?'}`);
}

/**
 * `let PATTERN = init` conditions (if-let / while-let): evaluates
 * init as a multi-value source, binds the pattern's non-literal
 * elements, and returns the CONDITION value. Streaming's contract,
 * mirrored exactly: the FIRST tuple element IS the boolean — a
 * literal `true` in the pattern is not re-checked, and the trailing
 * bindings are unconditionally bound (they only flow into the arm
 * the checker allows them in).
 */
export function lowerLetCondition(cx: LoweringContext, lpc: LetPatternCondition): LetCondBinds {
  var pat = lpc.pattern;
  if (pat is BindingPattern) {
    pat = pat.pattern;
  }
  // A record pattern against a union of inline records in slots: the
  // members it can match are tested for, and each field it names is
  // read from its slot, its pattern deferring into the taken block.
  if (pat is RecordPattern) {
    // Narrowing can leave the binding's type one record, while the binding
    // still holds the union's slots.
    let initType = cx.substitutedNodeType(lpc.init);
    let held = heldInline(cx, lpc.init);
    let heldRecords = held != null && held.layout != null &&
        (held.layout as InlineLayout).records != null;
    if (heldRecords || (initType != null && inlineRecordUnionMembers(initType) != null)) {
      let source = held ?? multiSource(cx, lpc.init);
      let sourceLayout = source.layout;
      if (sourceLayout != null && sourceLayout.records != null) {
        let lanesOf = inlineRecordPatternLanes(cx, pat, source);
        let patterns = new Array<Node>();
        for (let prop in pat.properties) {
          patterns.push(prop.value);
        }
        return new LetCondBinds(lanesOf.cond, new Array<Symbol>(), new Array<i32>(),
            new Array<ValType>(), patterns, lanesOf.values);
      }
    }
  }
  if (!(pat is TuplePattern)) {
    // A non-tuple let-condition (a record presence pattern): the whole
    // pattern defers into the taken block, where its tests branch to
    // the not-taken side and its bindings apply.
    if (!patternBinds(cx, pat) && !patternRefutable(cx, pat)) {
      bail(cx, 'let-condition pattern');
    }
    let value = lowerExpr(cx, lpc.init, null);
    let patterns = growable<Node>([pat]);
    let patternValues = growable<i32>([value]);
    let cond = cx.b.constI32(1, new ValTypeI32() as ValType);
    return new LetCondBinds(cond, new Array<Symbol>(), new Array<i32>(),
        new Array<ValType>(), patterns, patternValues);
  }
  // One value per element, a nested tuple pattern at a nested inline
  // position read leaf by leaf.
  let elements = new Array<Node | null>();
  let source = multiSource(cx, lpc.init);
  let values = multiPositions(cx, source, pat, elements);
  if (values.length != elements.length) {
    bail(cx, 'let-condition arity');
  }
  var cond = values[0];
  if (!(cx.valueTypeOf(cond) is ValTypeI32)) {
    bail(cx, 'let-condition flag type');
  }
  // A literal `false` first element matches when the flag is 0:
  // invert. Literal `true` and plain bindings take the flag as-is.
  // Numeric discriminants (`0`, `1`, `2` in `Step` / `AsyncStep`)
  // compare against the literal value.
  // The `let` is explicit here, so the element is a LiteralPattern; the
  // bare-literal form is still accepted for patterns built elsewhere.
  var first = elements[0];
  if (first is LiteralPattern) {
    first = first.value;
  }
  if (first is BooleanLiteral) {
    if (!first.value) {
      cond = cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), cond, -1);
    }
  } else if (first is NumberLiteral) {
    if (let (true, parsed) = parseI64OrU64Literal(first.raw)) {
      let lit = cx.b.constI32(parsed as i32, new ValTypeI32());
      cond = cx.b.binary(IrOp.i32_eq, new ValTypeI32(), cond, lit);
    } else {
      bail(cx, 'unparseable pattern literal');
    }
  }
  let syms = new Array<Symbol>();
  let bindValues = new Array<i32>();
  let vts = new Array<ValType>();
  let patterns = new Array<Node>();
  let patternValues = new Array<i32>();
  for (var i = 0; i < elements.length; i += 1) {
    let el = elements[i];
    if (el == null) {
      continue;
    }
    if (i > 0 && el is LiteralPattern) {
      // A literal inside a nested tuple pattern, spliced in: its test
      // defers to the taken block like a nested pattern's.
      patterns.push(el);
      patternValues.push(values[i]);
      continue;
    }
    if (el is LiteralPattern || el is BooleanLiteral ||
        el is NumberLiteral || el is StringLiteral || el is NullLiteral) {
      continue;
    }
    let sym = cx.func.model.resolveDeclaration(el);
    if (sym == null) {
      // Nested destructuring elements defer to the taken block,
      // where their tests can branch to the not-taken side.
      if (patternBinds(cx, el) ||
          patternRefutable(cx, el)) {
        patterns.push(el);
        patternValues.push(values[i]);
      }
      continue;
    }
    let bindingType = cx.func.model.getBindingType(sym);
    if (bindingType == null) {
      bail(cx, 'untyped binding');
    }
    let bindVt = typeToValTypeWithSub(
        cx.wasm, bindingType, cx.func);
    if (bindVt is ValTypeVoid) {
      // A zero-width lane: the decode wove NO_VALUE here, and the
      // binding holds nothing, like any void local.
      syms.push(sym);
      bindValues.push(values[i]);
      vts.push(bindVt);
      continue;
    }
    cx.requireSupported(bindVt, 'let-condition element type');
    if (!isValSubtype(cx.valueTypeOf(values[i]), bindVt) &&
        !isRefValType(bindVt)) {
      bail(cx, 'let-condition element type');
    }
    syms.push(sym);
    bindValues.push(values[i]);
    vts.push(bindVt);
  }
  let binds = new LetCondBinds(cond, syms, bindValues, vts, patterns, patternValues);
  let layout = source.layout;
  if (layout != null && layoutHoldsOwner(layout)) {
    let allowed = inlineMembersMatching(layout, pat.elements);
    binds.ownerSource = source;
    binds.ownerAllowed = allowed;
    binds.ownerKept = boundPaths(cx, layout, allowed, pat.elements);
  }
  return binds;
}

/** Whether some member of `layout` holds an owner. */
function layoutHoldsOwner(layout: InlineLayout): boolean {
  for (var m = 0; m < layout.members.length; m += 1) {
    if (memberReleases(layout, m, null)) {
      return true;
    }
  }
  return false;
}

/**
 * The paths a tuple pattern binds a name at, read against the members
 * `allowed` keeps: the owner at each is its binding's to release. A
 * position under `_`, or one a literal tests, binds nothing.
 */
function boundPaths(cx: LoweringContext, layout: InlineLayout,
    allowed: GrowableArray<boolean> | null,
    elements: GrowableArray<Node | null>): HashSet<String> {
  let bound = new HashSet<String>();
  let rep = representativeMember(layout, allowed);
  for (let entry in pathElements(layout.originals[rep], elements)) {
    let node = entry.node;
    if (node != null && patternBinds(cx, node)) {
      bound.add(entry.path);
    }
  }
  return bound;
}

/**
 * The owners a let-condition's pattern took out of its source without
 * binding them, released in the taken block once its tests pass: the
 * pattern consumed the source, and only a bound owner is its branch's.
 */
export function emitTakenOwnerDrops(cx: LoweringContext, binds: LetCondBinds): void {
  let source = binds.ownerSource;
  if (source != null) {
    emitSlotsDropExcept(cx, source, binds.ownerAllowed, binds.ownerKept);
  }
}

/**
 * The source of a let-condition whose pattern did not match, released
 * on the not-taken side: the pattern consumed it.
 */
export function emitNotTakenOwnerDrops(cx: LoweringContext, binds: LetCondBinds): void {
  let source = binds.ownerSource;
  if (source != null) {
    emitSlotsDrop(cx, source);
  }
}

/**
 * The owners a `match` arm over a tuple union took out of the source
 * without binding them: those under `_` in a tuple pattern, or the whole
 * source for `case _`.
 */
function emitArmOwnerDrops(cx: LoweringContext, source: MultiValues, pattern: Node) {
  let layout = source.layout;
  if (layout == null || !layoutHoldsOwner(layout)) {
    return;
  }
  if (let TuplePattern {elements} = pattern) {
    let allowed = inlineMembersMatching(layout, elements);
    emitSlotsDropExcept(cx, source, allowed, boundPaths(cx, layout, allowed, elements));
  } else {
    emitSlotsDrop(cx, source);
  }
}

/** Whether a let-condition owes its source a release on either side. */
function letBindsOwnSource(binds: LetCondBinds | null) {
  return binds != null && binds.ownerSource != null;
}

/** Whether any deferred let-condition pattern is refutable. */
export function letBindsNeedFail(cx: LoweringContext, binds: LetCondBinds): boolean {
  for (let pat in binds.patterns) {
    if (patternRefutable(cx, pat)) {
      return true;
    }
  }
  return false;
}

/**
 * Applies a let-condition's bindings in the CURRENT (taken) block.
 * Nested destructuring elements run their tests here, branching to
 * `failB` (the not-taken side) on mismatch, then bind.
 */
export function applyLetBinds(cx: LoweringContext, binds: LetCondBinds, failB: i32): void {
  for (var i = 0; i < binds.syms.length; i += 1) {
    var bound = binds.values[i];
    let bindVt = binds.vts[i];
    if (bindVt is ValTypeVoid) {
      // Zero-width binding: nothing to conform or copy.
      cx.bindPatternSym(binds.syms[i], bound);
      continue;
    }
    if (!isValSubtype(cx.valueTypeOf(bound), bindVt)) {
      // Erased/nullable slot element vs the concrete binding:
      // runtime downcast — in the taken block, so an exhausted
      // source's null payload is never coerced.
      bound = cx.b.emit(IrOp.ref_cast, bindVt, bound,
          cx.b.internType(bindVt));
    }
    cx.bindPatternSym(binds.syms[i], bound);
  }
  for (var i = 0; i < binds.patterns.length; i += 1) {
    let value = cx.stripNonNull(binds.patternValues[i]);
    if (patternRefutable(cx, binds.patterns[i])) {
      lowerPatternTest(cx, binds.patterns[i], value, failB);
    }
    lowerPatternBindings(cx, binds.patterns[i], value);
  }
}

/**
 * Lowers an if-expression or match-case arm: a plain expression, or a
 * value-block whose trailing expression statement supplies the value —
 * or whose tail is a statement that never falls through (`return`,
 * `break`, `continue`, a bare `throw`), in which case the arm ends
 * unreachable and supplies no value at all.
 * `ctxVt` is the arm's contextual result type (for null literals);
 * null when the context is void. Returns -1 when the arm ends
 * unreachable (or the context is void).
 */
export function lowerValueNode(cx: LoweringContext, node: Node, isVoid: boolean,
    ctxVt: ValType | null, tail: boolean = false): i32 {
  if (node is Expression) {
    if (node is ThrowExpression) {
      // A bare throw as an arm: the arm ends unreachable and
      // produces no value; callers skip their merge branch.
      throwStmt(cx, node);
      return -1;
    }
    if (node is CallExpression) {
      // A never-returning call as an arm ends it the same way a bare
      // throw does. Statement lowering closes the block after the call.
      let t = cx.func.model.getNodeType(node);
      if (t != null && unwrapAliases(t) is NeverType) {
        lowerStmt(cx, new ExpressionStatement(node, node.loc));
        return -1;
      }
    }
    if (tail) {
      cx.tailExitScopes.push(node);
      returnInlineTail(cx, node);
      cx.tailExitScopes.pop();
      return -1;
    }
    let value = lowerExpr(cx, node, ctxVt);
    emitNodeExitDrops(cx, node);
    return if (isVoid) -1 else value;
  }
  if (node is BlockStatement) {
    let body = node.body;
    if (body.length == 0) {
      if (isVoid) {
        emitNodeExitDrops(cx, node);
        return -1;
      }
      bail(cx, 'empty value block');
    }
    cx.precellBindings(body);
    return lowerBlockValueFrom(cx, body, 0, isVoid, ctxVt, tail, node);
  }
  cx.fail('value arm kind');
}

/**
 * Statements `start..` of a value block, ending at the tail. A `using`
 * or an implicit-drop declaration scopes the rest of the block — tail
 * expression included — so it opens a release region here: a void
 * block routes through the statement regions, and a value block
 * carries the tail's value out through the region's result variable,
 * exactly as a valued try/finally does.
 */
function lowerBlockValueFrom(cx: LoweringContext, body: GrowableArray<Statement>,
    start: i32, isVoid: boolean, ctxVt: ValType | null, tail: boolean = false,
    scopeExit: Node | null = null): i32 {
  if (isVoid) {
    // A void block (a try body, a statement arm) is a statement list
    // like any other, tail included.
    lowerStatementRange(cx, body, start, body.length, scopeExit);
    return -1;
  }
  let plans = varDropPlans(cx, body);
  var i = start;
  while (i < body.length - 1) {
    if (!cx.reachable) {
      return -1;
    }
    // A `var` owner binding's interval: one ending before the tail is
    // a void region the block continues after; one reaching the tail
    // carries the tail's value out like the regions below.
    var openedEnd = -1;
    for (let plan in plans) {
      if (cx.openVarDrops.has(plan.symId as i32)) { continue; }
      let raw = varDropIntervalEnd(plan, i, body.length);
      if (raw == -2) { continue; }
      if (raw < 0) {
        return lowerValueRegionRestVar(cx, plan, body, i, ctxVt, tail, scopeExit);
      }
      // As in lowerRangeAcquiring: the interval closes after the
      // acquisition of a statement in it that scopes the rest of the
      // block, and reopens inside that statement's region.
      let opener = firstScopeOpener(cx, body, i, raw);
      if (opener >= 0) {
        let acquired = lowerVarDropRegion(cx, plan, body, i, opener, false, true);
        if (acquired == null) { return -1; }
        return lowerValueRegionRest(cx, acquired.values, acquired.plans, 0, body,
            opener + 1, ctxVt, tail, scopeExit);
      }
      lowerVarDropRegion(cx, plan, body, i, raw, false, false);
      openedEnd = raw;
      break;
    }
    if (openedEnd >= 0) {
      i = openedEnd;
      continue;
    }
    if (opensScope(cx, body[i])) {
      // Acquire, then wrap the rest of the block in the result-carrying
      // region.
      let acquired = acquireScope(cx, body[i]);
      return lowerValueRegionRest(cx, acquired.values, acquired.plans, 0, body,
          i + 1, ctxVt, tail, scopeExit);
    }
    lowerStmt(cx, body[i]);
    i += 1;
  }
  if (!cx.reachable) {
    return -1;
  }
  let last = body[body.length - 1];
  if (last is ExpressionStatement) {
    let lastExpr = last.expression;
    if (lastExpr is ThrowExpression) {
      // A throw in arm-tail position: the arm ends unreachable and
      // produces no value; callers skip their merge branch.
      throwStmt(cx, lastExpr);
      return -1;
    }
    if (lastExpr is CallExpression) {
      // A never-returning call in tail position: same as a throw.
      let t = cx.func.model.getNodeType(lastExpr);
      if (t != null && unwrapAliases(t) is NeverType) {
        lowerStmt(cx, last);
        return -1;
      }
    }
    if (tail) {
      if (scopeExit != null) {
        cx.tailExitScopes.push(scopeExit);
      }
      returnInlineTail(cx, lastExpr);
      if (scopeExit != null) {
        cx.tailExitScopes.pop();
      }
      return -1;
    }
    let value = lowerExpr(cx, lastExpr, ctxVt);
    if (scopeExit != null) {
      emitNodeExitDrops(cx, scopeExit);
    }
    return if (isVoid) -1 else value;
  }
  // A non-expression tail supplies no value. That is fine when the
  // context is void, and fine in a value context when the statement
  // cannot fall through — `return`/`break`/`continue` end the arm
  // unreachable, so the caller skips its merge branch and the arm
  // never has to produce one. Anything that DOES fall through in a
  // value context is a real gap; bail on it.
  lowerStmt(cx, last);
  if (isVoid || !cx.reachable) {
    return -1;
  }
  bail(cx, 'value block tail');
}

/**
 * The rest of a value block behind a release region: the tail value
 * computes inside, parks in the region's result variable, and rides
 * out through the dispatch — so the release runs after the value is
 * read, and every early exit and the unwind path release too.
 */
function lowerValueRegionRest(cx: LoweringContext, values: GrowableArray<i32>,
    plans: GrowableArray<DropPlan>, idx: i32, body: GrowableArray<Statement>, start: i32,
    ctxVt: ValType | null, tail: boolean = false,
    scopeExit: Node | null = null): i32 {
  // A tail that returns carries no value out of the region.
  if (ctxVt == null && !tail) {
    bail(cx, 'value region result type');
  }
  let mirrored = new Array<SymbolId>();
  for (var k = start; k < body.length; k += 1) {
    cx.appendAssignedEnvSyms(body[k] as Node, mirrored);
  }
  let value = values[idx];
  let plan = plans[idx];
  return lowerFinallyRegion(cx, mirrored, tail, if (tail) null else ctxVt,
      (scope, resultVar, saved) => {
        let v = if (idx + 1 < values.length) {
          lowerValueRegionRest(cx, values, plans, idx + 1, body, start, ctxVt, tail, scopeExit)
        } else {
          lowerBlockValueFrom(cx, body, start, false, ctxVt, tail, scopeExit)
        };
        finishProtectedArm(cx, scope, resultVar, if (tail) -1 else v);
      },
      (fscope) => { emitDrop(cx, value, plan); });
}

/**
 * The same, for a `var` owner binding whose release interval reaches
 * the block's tail: the finalizer releases what the binding holds at
 * the exit (see lowerVarDropRegion).
 */
function lowerValueRegionRestVar(cx: LoweringContext, plan: VarDropPlan,
    body: GrowableArray<Statement>, start: i32, ctxVt: ValType | null,
    tail: boolean = false, scopeExit: Node | null = null): i32 {
  if (ctxVt == null && !tail) {
    bail(cx, 'value region result type');
  }
  let mirrored = new Array<SymbolId>();
  for (var k = start; k < body.length; k += 1) {
    cx.appendAssignedEnvSyms(body[k] as Node, mirrored);
  }
  cx.openVarDrops.add(plan.symId as i32);
  let result = lowerFinallyRegion(cx, mirrored, tail, if (tail) null else ctxVt,
      (scope, resultVar, saved) => {
        let v = lowerBlockValueFrom(cx, body, start, false, ctxVt, tail, scopeExit);
        finishProtectedArm(cx, scope, resultVar, if (tail) -1 else v);
      },
      (fscope) => { emitVarDropRelease(cx, plan); });
  cx.openVarDrops.delete(plan.symId as i32);
  return result;
}

/**
 * Match lowering. Each case chains its tests so the first failure
 * branches to the next case's entry block — which also keeps nested
 * property tests guarded behind their enclosing ref.test (the
 * streaming backend gets the same guarantee from if/else
 * accumulators) — then binds pattern variables into the SSA
 * environment, tests the guard, and lowers the body. Completed arms
 * branch to a lazily created merge block carrying a result parameter
 * when the match produces a value. The final fail block only traps:
 * the checker proves exhaustiveness, mirroring streaming's trailing
 * unreachable.
 */
/** What a record pattern against a union of inline records reads. */
final class InlineRecordLanes {
  /** Whether the value is a member the pattern can match. */
  cond: i32;
  /** Each named field's value, in the pattern's order. */
  values: GrowableArray<i32>;
  new(this.cond, this.values);
}

/**
 * A record pattern read against a union of inline records held in slots:
 * the members it can match (each field it names present, and its literals
 * agreeing), a test for them, and each field's value for them.
 */
function inlineRecordPatternLanes(cx: LoweringContext, pattern: RecordPattern,
    source: MultiValues): InlineRecordLanes {
  let layout = source.layout as InlineLayout;
  let allowed = new Array<boolean>();
  var all = true;
  for (let member in layout.originals) {
    var ok = true;
    for (let prop in pattern.properties) {
      let position = layout.fieldPosition(prop.name.name);
      if (position < 0 || position >= member.elementTypes.length ||
          member.elementTypes[position] is HoleType) {
        ok = false;
        continue;
      }
      var written = prop.value;
      if (written is LiteralPattern) {
        written = written.value;
      }
      let memberType = member.elementTypes[position];
      if (memberType is BoolLiteralType && written is BooleanLiteral &&
          memberType.value != written.value) {
        ok = false;
      } else if (memberType is NumLiteralType && written is NumberLiteral &&
          !sameNumericLiteralRaw(memberType.raw, written.raw)) {
        ok = false;
      }
    }
    allowed.push(ok);
    all = all && ok;
  }
  let i32Vt = new ValTypeI32() as ValType;
  var cond = -1;
  if (all) {
    cond = cx.b.constI32(1, i32Vt);
  } else {
    for (var m = 0; m < allowed.length; m += 1) {
      if (!allowed[m]) {
        continue;
      }
      let test = memberTest(cx, layout, source.values, m);
      cond = if (cond == -1) test else cx.b.binary(IrOp.i32_or, i32Vt, cond, test);
    }
    if (cond == -1) {
      cond = cx.b.constI32(0, i32Vt);
    }
  }
  let values = new Array<i32>();
  for (let prop in pattern.properties) {
    let position = layout.fieldPosition(prop.name.name);
    // -1 is `NO_VALUE`, which cannot be imported across the cycle.
    let v = if (position >= 0) source.valueAt(cx, i32ToString(position), allowed) else -1;
    values.push(v);
  }
  return new InlineRecordLanes(cond, values);
}

/**
 * A multi-value source's value at each position, as `pattern` reads it:
 * its literal elements rule out the union's other members, so it can
 * read a position those members keep in a slot of their own.
 */
function multiPositions(cx: LoweringContext, source: MultiValues,
    pattern: TuplePattern, nodes: GrowableArray<Node | null>): GrowableArray<i32> {
  let layout = source.layout;
  let allowed = if (layout != null) {
    inlineMembersMatching(layout, pattern.elements)
  } else {
    null
  };
  return source.patternValues(cx, pattern.elements, allowed, nodes);
}

/**
 * A `match` discriminant whose type is a single inline tuple or a
 * union of them arrives as multi-value lanes, not a boxed struct, so
 * it is decomposed with the same `multiSource` machinery as a
 * let-condition rather than read with `struct_get`. So is a union of
 * boxed tuples: its members can have different structs, and
 * `multiSource` reads whichever one it holds into the same slots
 * (`#unboxTuples`).
 */
function isTupleUnionSource(t: Type): boolean {
  // A union of inline records is held in slots too.
  if (inlineRecordUnionMembers(t) != null) { return true; }
  let resolved = unwrapAliases(t);
  if (resolved is InlineTupleType) { return true; }
  if (!(resolved is UnionType)) { return false; }
  let types = resolved.types;
  if (types.length == 0) { return false; }
  var allInline = true;
  var allBoxed = true;
  for (let member in types) {
    if (!(member is InlineTupleType)) { allInline = false; }
    if (!(member is TupleType)) { allBoxed = false; }
  }
  return allInline || (allBoxed && types.length > 1);
}

/**
 * Tests and binds one tuple-pattern arm against the discriminant's
 * lanes. Literal lanes (the discriminant) branch to `failB` on
 * mismatch; the surviving lanes bind directly, so nothing is boxed.
 * The checker has already narrowed each binding to the arm its
 * discriminant selects.
 */
function lowerInlineTupleArm(cx: LoweringContext, pat: TuplePattern,
    source: MultiValues, failB: i32) {
  let elements = new Array<Node | null>();
  let lanes = multiPositions(cx, source, pat, elements);
  if (lanes.length != elements.length) {
    bail(cx, 'match inline union arity');
  }
  for (var i = 0; i < elements.length; i += 1) {
    let el = elements[i];
    if (el != null && patternRefutable(cx, el)) {
      lowerPatternTest(cx, el, lanes[i], failB);
    }
  }
  for (var i = 0; i < elements.length; i += 1) {
    let el = elements[i];
    if (el != null && patternBinds(cx, el)) {
      lowerPatternBindings(cx, el, lanes[i]);
    }
  }
}

export function lowerMatch(cx: LoweringContext, expr: MatchExpression): i32 {
  let armFields = cx.takeResultFields();
  // A returned inline value: each arm returns its own (returnInlineTail).
  let returnTail = cx.takeReturnTail();
  // An inline value in slots: each arm passes its slots to the merge.
  let armSlots = cx.takeResultSlots();
  let nodeType = cx.substitutedNodeType(expr);
  if (nodeType == null) {
    bail(cx, 'untyped match');
  }
  let isVoid = isVoidOrNeverType(nodeType);
  let resultVt = if (!isVoid && !returnTail && armSlots == null) {
    let vt = typeToValTypeWithSub(cx.wasm, nodeType, cx.func);
    cx.requireSupported(vt, 'match result type');
    vt
  } else {
    null
  };

  let discType = cx.substitutedNodeType(expr.discriminant);
  // A binding of a union of inline records holds its slots even where
  // narrowing has left its type one record.
  let heldDisc = heldInline(cx, expr.discriminant);
  let heldRecordUnion = heldDisc != null && heldDisc.layout != null &&
      (heldDisc.layout as InlineLayout).records != null;
  let lanes = if (heldRecordUnion) {
    heldDisc
  } else if (discType != null && isTupleUnionSource(discType)) {
    multiSource(cx, expr.discriminant)
  } else {
    null
  };
  let disc = if (lanes == null) lowerExpr(cx, expr.discriminant, null) else -1;

  let mergeSyms = new Array<SymbolId>();
  for (let mc in expr.cases) {
    cx.appendAssignedEnvSyms(mc.body, mergeSyms);
    if (mc.guard != null) {
      cx.appendAssignedEnvSyms(mc.guard as Node, mergeSyms);
    }
  }

  let m = new MergeState(mergeSyms, isVoid, resultVt);
  m.returnTail = returnTail;
  if (!isVoid) {
    m.fields = armFields;
    m.slots = armSlots;
  }
  let saved = cx.copyEnv();

  var exhausted = false;
  var i = 0;
  while (i < expr.cases.length && !exhausted) {
    let mc = expr.cases[i];
    cx.env = cx.copyOf(saved);
    // An irrefutable, guardless case consumes everything; later cases
    // are statically dead (and their blocks, having no predecessors,
    // would fail verification).
    var failB = -1;
    if (mc.guard != null || patternRefutable(cx, mc.pattern)) {
      failB = cx.b.newBlock();
    } else {
      exhausted = true;
    }
    if (lanes != null) {
      let laneVals = lanes as MultiValues;
      var armPattern = mc.pattern;
      if (armPattern is BindingPattern) {
        armPattern = armPattern.pattern;
      }
      if (mc.pattern is TuplePattern) {
        lowerInlineTupleArm(cx, mc.pattern as TuplePattern, laneVals, failB);
      } else if (armPattern is RecordPattern && laneVals.layout != null &&
          (laneVals.layout as InlineLayout).records != null) {
        let lanesOf = inlineRecordPatternLanes(cx, armPattern, laneVals);
        branchUnless(cx, lanesOf.cond, failB);
        for (var k = 0; k < armPattern.properties.length; k += 1) {
          let sub = armPattern.properties[k].value;
          if (patternRefutable(cx, sub)) {
            lowerPatternTest(cx, sub, lanesOf.values[k], failB);
          }
        }
        for (var k = 0; k < armPattern.properties.length; k += 1) {
          let sub = armPattern.properties[k].value;
          if (patternBinds(cx, sub)) {
            lowerPatternBindings(cx, sub, lanesOf.values[k]);
          }
        }
      } else if (patternBinds(cx, mc.pattern) || patternRefutable(cx, mc.pattern)) {
        // An inline multi-value has no single value to bind or test
        // against; only a wildcard (`case _`) is admissible here.
        bail(cx, 'match over inline union pattern');
      }
    } else {
      lowerPatternTest(cx, mc.pattern, disc, failB);
      lowerPatternBindings(cx, mc.pattern, disc);
    }
    if (mc.guard != null) {
      let cond = lowerExpr(cx, mc.guard as Expression, null);
      branchUnless(cx, cond, failB);
    }
    if (lanes != null) {
      // The arm consumed the source: the owners it took without a name
      // are released here, and its bindings release the rest.
      emitArmOwnerDrops(cx, lanes, mc.pattern);
    }
    let fieldsMode = m.fields;
    let slotsMode = m.slots;
    if (fieldsMode != null) {
      let values = lowerArmFields(cx, mc.body, fieldsMode, resultVt);
      if (cx.reachable) {
        emitPlainBranchDrops(cx, mc.pattern);
        emitMatchArmDrops(cx, expr, i);
        cx.mergeArmFields(m, values);
      }
    } else if (slotsMode != null) {
      let values = lowerArmSlots(cx, mc.body, slotsMode);
      if (cx.reachable) {
        emitPlainBranchDrops(cx, mc.pattern);
        emitMatchArmDrops(cx, expr, i);
        cx.mergeArmSlots(m, values);
      }
    } else {
      let value = lowerBranchValue(cx, mc.pattern, mc.body, isVoid, resultVt,
          m.returnTail);
      if (cx.reachable) {
        emitMatchArmDrops(cx, expr, i);
        cx.mergeArm(m, value, 'match arm type');
      }
    }
    if (failB >= 0) {
      cx.b.switchTo(failB);
      cx.reachable = true;
    }
    i += 1;
  }

  if (!exhausted) {
    cx.b.unreachable();
  }

  return finishArmsMerge(cx, m, saved);
}

/**
 * The compensating drops one match arm owes at its end: another arm
 * moved the binding, so releasing here makes it uniformly dead at the
 * merge — the same rule emitEdgeDrops applies to an if's arms, indexed
 * by case instead of by side. Plain calls, not a region.
 */
function emitMatchArmDrops(cx: LoweringContext, expr: MatchExpression, armIndex: i32): void {
  let dropsOpt = cx.func.model.edgeDropsOf(expr as Node);
  if (dropsOpt == null) { return; }
  for (let d in dropsOpt) {
    if (d.armIndex != armIndex) { continue; }
    let declType = cx.bindingTypeOf(d.symbolId);
    if (declType == null) {
      bail(cx, 'edge drop untyped binding');
    }
    let plan = dropPlanOf(cx, declType);
    let syms = growable<SymbolId>([d.symbolId]);
    let value = cx.envValues(syms)[0];
    emitDrop(cx, value, plan);
  }
}

/**
 * The compensating drops one arm of an `if` owes at its end: the other
 * arm moved the binding, so releasing here makes it uniformly dead at
 * the merge (ownership.md §"The branch-join rule"). Plain calls, not a
 * region — the drop belongs to one specific point on one edge.
 */
function emitEdgeDrops(cx: LoweringContext, stmt: IfStatement, onTrue: boolean) {
  let dropsOpt = cx.func.model.edgeDropsOf(stmt as Node);
  if (dropsOpt == null) { return; }
  for (let d in dropsOpt) {
    if (d.onTrueArm != onTrue) { continue; }
    let declType = cx.bindingTypeOf(d.symbolId);
    if (declType == null) {
      bail(cx, 'edge drop untyped binding');
    }
    let plan = dropPlanOf(cx, declType);
    let syms = growable<SymbolId>([d.symbolId]);
    let value = cx.envValues(syms)[0];
    emitDrop(cx, value, plan);
  }
}

/**
 * The drops an early exit owes (SemanticModel.exitDropsOf): bindings a
 * move elsewhere kept from their scope's release, still live as control
 * leaves here. Plain calls, as an arm's compensating drops are.
 */
export function emitExitDrops(cx: LoweringContext, drops: GrowableArray<SymbolId>): void {
  for (let symId in drops) {
    let declType = cx.bindingTypeOf(symId);
    if (declType == null) {
      bail(cx, 'exit drop untyped binding');
    }
    let plan = dropPlanOf(cx, declType);
    let value = cx.envValues(growable<SymbolId>([symId]))[0];
    emitDrop(cx, value, plan);
  }
}

export function emitNodeExitDrops(cx: LoweringContext, node: Node): void {
  if (!cx.reachable) {
    return;
  }
  let drops = cx.func.model.exitDropsOf(node);
  if (drops != null) {
    emitExitDrops(cx, drops);
  }
}

/** Whether the if has any drops owed on the given arm. */
function hasEdgeDrops(cx: LoweringContext, stmt: IfStatement, onTrue: boolean): boolean {
  let dropsOpt = cx.func.model.edgeDropsOf(stmt as Node);
  if (dropsOpt == null) { return false; }
  for (let d in dropsOpt) {
    if (d.onTrueArm == onTrue) { return true; }
  }
  return false;
}

export function lowerIf(cx: LoweringContext, stmt: IfStatement): void {
  var cond = -1;
  var letBinds: LetCondBinds | null = null;
  if (stmt.test is Expression) {
    cond = lowerExpr(cx, stmt.test as Expression, null);
  } else if (stmt.test is LetPatternCondition) {
    let binds = lowerLetCondition(cx, stmt.test as LetPatternCondition);
    letBinds = binds;
    cond = binds.cond;
  } else {
    bail(cx, 'if-let');
  }

  let alternate = stmt.alternate;
  if (alternate == null) {
    let mergeSyms = cx.assignedEnvSyms(stmt.consequent);
    let thenB = cx.b.newBlock();
    let mergeB = cx.b.newBlock();
    let mergeParams = cx.addParams(mergeB, mergeSyms);
    if (hasEdgeDrops(cx, stmt, false) || letBindsOwnSource(letBinds)) {
      // The false edge owes drops, and with no else there is no block
      // to hold them: route it through one.
      let falseB = cx.b.newBlock();
      cx.b.brIf(cond, thenB, noValues(), falseB, noValues());
      cx.b.switchTo(falseB);
      emitEdgeDrops(cx, stmt, false);
      if (letBinds != null) {
        emitNotTakenOwnerDrops(cx, letBinds);
      }
      cx.b.br(mergeB, cx.envValues(mergeSyms));
    } else {
      cx.b.brIf(cond, thenB, noValues(), mergeB, cx.envValues(mergeSyms));
    }

    let saved = cx.copyEnv();
    cx.b.switchTo(thenB);
    if (letBinds != null) {
      let needsFail = letBindsNeedFail(cx, letBinds);
      var failB = mergeB;
      if (needsFail) {
        // The merge takes args; pattern tests branch argless, so a
        // trampoline forwards the (unmodified) env.
        failB = cx.b.newBlock();
      }
      applyLetBinds(cx, letBinds, failB);
      if (needsFail) {
        let resumeB = cx.b.newBlock();
        cx.b.br(resumeB, noValues());
        cx.b.switchTo(failB);
        emitNotTakenOwnerDrops(cx, letBinds);
        cx.b.br(mergeB, cx.envValues(mergeSyms));
        cx.b.switchTo(resumeB);
      }
      emitTakenOwnerDrops(cx, letBinds);
    }
    lowerBranchStmt(cx, letPatternOf(stmt.test), stmt.consequent);
    if (cx.reachable) {
      emitEdgeDrops(cx, stmt, true);
      cx.b.br(mergeB, cx.envValues(mergeSyms));
    }
    cx.env = saved;
    cx.b.switchTo(mergeB);
    cx.applyParams(mergeSyms, mergeParams);
    cx.reachable = true;
    return;
  }

  let mergeSyms = cx.assignedEnvSyms(stmt.consequent);
  cx.appendAssignedEnvSyms(alternate, mergeSyms);
  let thenB = cx.b.newBlock();
  let elseB = cx.b.newBlock();
  cx.b.brIf(cond, thenB, noValues(), elseB, noValues());

  // The merge block is created lazily: if both branches terminate, no
  // merge exists (and an unreachable block would fail verification).
  let m = new MergeState(mergeSyms, true, null);

  let saved = cx.copyEnv();
  cx.b.switchTo(thenB);
  if (letBinds != null) {
    applyLetBinds(cx, letBinds, elseB);
    emitTakenOwnerDrops(cx, letBinds);
  }
  lowerBranchStmt(cx, letPatternOf(stmt.test), stmt.consequent);
  if (cx.reachable) {
    emitEdgeDrops(cx, stmt, true);
    cx.mergeArm(m, -1);
  }

  cx.env = cx.copyOf(saved);
  cx.reachable = true;
  cx.b.switchTo(elseB);
  if (letBinds != null) {
    emitNotTakenOwnerDrops(cx, letBinds);
  }
  lowerStmt(cx, alternate);
  if (cx.reachable) {
    emitEdgeDrops(cx, stmt, false);
    cx.mergeArm(m, -1);
  }

  cx.finishMerge(m, saved);
}

/**
 * If-expressions: like #lowerIf, plus one extra merge parameter carrying
 * the arm value. Arms may be plain expressions or value-blocks (a block
 * whose trailing expression statement is the value). Void-typed
 * if-expressions lower like statements and yield no value.
 */
export function lowerIfExpression(cx: LoweringContext, expr: IfExpression): i32 {
  let armFields = cx.takeResultFields();
  // A returned inline value: each arm returns its own (returnInlineTail),
  // so there is no value to merge.
  let returnTail = cx.takeReturnTail();
  // An inline value in slots: each arm passes its slots to the merge.
  let armSlots = cx.takeResultSlots();
  let nodeType = cx.substitutedNodeType(expr);
  if (nodeType == null) {
    bail(cx, 'untyped if expression');
  }
  let isVoid = baseType(nodeType) is VoidType;
  let resultVt = if (!isVoid && !returnTail && armSlots == null) cx.exprVt(expr) else null;
  if (expr.alternate == null && !isVoid) {
    bail(cx, 'value if without else');
  }

  var cond = -1;
  var letBinds: LetCondBinds | null = null;
  if (expr.test is Expression) {
    cond = lowerExpr(cx, expr.test as Expression, null);
  } else if (expr.test is LetPatternCondition) {
    // Pattern-test fail edges target the else arm — which exists
    // even for the void no-else form (it restores the saved env and
    // branches to the merge).
    let binds = lowerLetCondition(cx, expr.test as LetPatternCondition);
    letBinds = binds;
    cond = binds.cond;
  } else {
    bail(cx, 'if-let expression');
  }
  let mergeSyms = cx.assignedEnvSyms(expr.consequent);
  if (expr.alternate != null) {
    cx.appendAssignedEnvSyms(expr.alternate as Node, mergeSyms);
  }

  let thenB = cx.b.newBlock();
  let elseB = cx.b.newBlock();
  cx.b.brIf(cond, thenB, noValues(), elseB, noValues());

  let m = new MergeState(mergeSyms, isVoid, resultVt);
  m.returnTail = returnTail;
  if (!isVoid) {
    m.fields = armFields;
    m.slots = armSlots;
  }

  let saved = cx.copyEnv();
  cx.b.switchTo(thenB);
  if (letBinds != null) {
    applyLetBinds(cx, letBinds, elseB);
    emitTakenOwnerDrops(cx, letBinds);
  }
  mergeBranchArm(cx, m, letPatternOf(expr.test), expr.consequent, isVoid, resultVt,
      'if arm type');

  cx.env = cx.copyOf(saved);
  cx.reachable = true;
  cx.b.switchTo(elseB);
  if (letBinds != null) {
    emitNotTakenOwnerDrops(cx, letBinds);
  }
  if (expr.alternate != null) {
    mergeValueArm(cx, m, expr.alternate as Node, isVoid, resultVt, 'if arm type');
  } else {
    // Void if-expression without else: fall through to the merge.
    cx.mergeArm(m, -1);
  }

  return finishArmsMerge(cx, m, saved);
}

/** Lowers one arm and branches to the merge with its value or its fields. */
/** The pattern of an `if let` or `while let` test, or null. */
function letPatternOf(test: Node): Node | null {
  return if (let LetPatternCondition {pattern} = test) pattern as Node else null;
}

/**
 * The owners a pattern bound that its branch releases, each marked by the
 * checker (#495): released at the end of the branch unless it moved them.
 */
function patternBranchDrops(cx: LoweringContext, pattern: Node | null): ScopeDrops {
  let syms = new Array<SymbolId>();
  let plans = new Array<DropPlan>();
  if (pattern != null) {
    collectPatternDrops(cx, pattern, syms, plans);
  }
  return new ScopeDrops(syms, plans);
}

/**
 * The owner bindings of a pattern that the checker marked for release
 * (registerPatternOwners), with their drop plans: those taken out of a
 * tuple or a `scoped T` instance, at any depth.
 */
function collectPatternDrops(cx: LoweringContext, node: Node,
    syms: GrowableArray<SymbolId>, plans: GrowableArray<DropPlan>): void {
  match (node) {
    case BindingPattern {pattern as inner}: collectPatternDrops(cx, inner as Node, syms, plans)
    case TuplePattern {elements}: {
      for (let element in elements) {
        if (element != null) {
          collectPatternDrops(cx, element, syms, plans);
        }
      }
    }
    case RecordPattern {properties}: collectPropertyDrops(cx, properties, syms, plans)
    case ClassPattern {properties}: collectPropertyDrops(cx, properties, syms, plans)
    case _: {
      if ((node is Identifier || node is IdentifierPattern) &&
          cx.func.model.hasScopeExitDrop(node)) {
        let symOpt = cx.func.model.resolveDeclaration(node);
        if (symOpt == null) {
          bail(cx, 'pattern drop binding symbol');
        }
        let sym = symOpt as Symbol;
        let bindingOpt = cx.func.model.getBindingTypeById(sym.id);
        if (bindingOpt == null) {
          bail(cx, 'untyped pattern drop binding');
        }
        syms.push(sym.id);
        plans.push(dropPlanOf(cx, bindingOpt as Type));
      }
    }
  }
}

function collectPropertyDrops(cx: LoweringContext, properties: GrowableArray<BindingProperty>,
    syms: GrowableArray<SymbolId>, plans: GrowableArray<DropPlan>): void {
  for (let property in properties) {
    collectPatternDrops(cx, property.value, syms, plans);
  }
}

/**
 * A branch's statement behind one release region per owner its pattern
 * bound, so early exits and the unwind path release them too.
 */
function lowerBranchStmt(cx: LoweringContext, pattern: Node | null, body: Statement): void {
  let drops = patternBranchDrops(cx, pattern);
  if (drops.syms.length == 0) {
    lowerStmt(cx, body);
    if (!(body is BlockStatement)) {
      emitNodeExitDrops(cx, body as Node);
    }
    return;
  }
  lowerBranchStmtDrops(cx, cx.envValues(drops.syms), drops.plans, 0, body);
}

function lowerBranchStmtDrops(cx: LoweringContext, values: GrowableArray<i32>,
    plans: GrowableArray<DropPlan>, idx: i32, body: Statement): void {
  if (idx >= values.length) {
    lowerStmt(cx, body);
    if (!(body is BlockStatement)) {
      emitNodeExitDrops(cx, body as Node);
    }
    return;
  }
  let value = values[idx];
  let plan = plans[idx];
  lowerFinallyRegion(cx, cx.assignedEnvSyms(body as Node), true, null,
      (scope, resultVar, saved) => {
        lowerBranchStmtDrops(cx, values, plans, idx + 1, body);
        finishProtectedArm(cx, scope, resultVar, -1);
      },
      (fscope) => { emitDrop(cx, value, plan); });
}

/** A branch's value behind the release regions its pattern's owners need. */
function lowerBranchValue(cx: LoweringContext, pattern: Node | null, body: Node,
    isVoid: boolean, resultVt: ValType | null, tail: boolean = false): i32 {
  let drops = patternBranchDrops(cx, pattern);
  if (drops.syms.length == 0) {
    return lowerValueNode(cx, body, isVoid, resultVt, tail);
  }
  return lowerBranchValueDrops(cx, cx.envValues(drops.syms), drops.plans, 0, body,
      isVoid, resultVt, tail);
}

function lowerBranchValueDrops(cx: LoweringContext, values: GrowableArray<i32>,
    plans: GrowableArray<DropPlan>, idx: i32, body: Node, isVoid: boolean,
    resultVt: ValType | null, tail: boolean = false): i32 {
  if (idx >= values.length) {
    return lowerValueNode(cx, body, isVoid, resultVt, tail);
  }
  let value = values[idx];
  let plan = plans[idx];
  let carries = !isVoid && resultVt != null;
  let v = lowerFinallyRegion(cx, cx.assignedEnvSyms(body), !carries,
      if (carries) resultVt else null,
      (scope, resultVar, saved) => {
        let inner = lowerBranchValueDrops(cx, values, plans, idx + 1, body, isVoid,
            resultVt, tail);
        finishProtectedArm(cx, scope, resultVar, if (carries) inner else -1);
      },
      (fscope) => { emitDrop(cx, value, plan); });
  return if (carries) v else -1;
}

/**
 * The releases a branch owes, as plain calls at its end. For an arm whose
 * record a core returns as fields, where no region carries the fields;
 * an early exit from such an arm leaks its owners.
 */
function emitPlainBranchDrops(cx: LoweringContext, pattern: Node): void {
  let drops = patternBranchDrops(cx, pattern);
  let values = cx.envValues(drops.syms);
  for (var i = values.length - 1; i >= 0; i -= 1) {
    emitDrop(cx, values[i], drops.plans[i]);
  }
}

/** `mergeValueArm` for a branch whose pattern bound owners. */
function mergeBranchArm(cx: LoweringContext, m: MergeState, pattern: Node | null,
    node: Node, isVoid: boolean, resultVt: ValType | null, armKind: String): void {
  let fieldsMode = m.fields;
  if (fieldsMode != null) {
    let values = lowerArmFields(cx, node, fieldsMode, resultVt);
    if (cx.reachable) {
      if (pattern != null) {
        emitPlainBranchDrops(cx, pattern);
      }
      cx.mergeArmFields(m, values);
    }
    return;
  }
  let slotsMode = m.slots;
  if (slotsMode != null) {
    let values = lowerArmSlots(cx, node, slotsMode);
    if (cx.reachable) {
      if (pattern != null) {
        emitPlainBranchDrops(cx, pattern);
      }
      cx.mergeArmSlots(m, values);
    }
    return;
  }
  let value = lowerBranchValue(cx, pattern, node, isVoid, resultVt, m.returnTail);
  if (cx.reachable) {
    cx.mergeArm(m, value, armKind);
  }
}

function mergeValueArm(cx: LoweringContext, m: MergeState, node: Node,
    isVoid: boolean, resultVt: ValType | null, armKind: String): void {
  let fieldsMode = m.fields;
  if (fieldsMode != null) {
    let values = lowerArmFields(cx, node, fieldsMode, resultVt);
    if (cx.reachable) {
      cx.mergeArmFields(m, values);
    }
    return;
  }
  let slotsMode = m.slots;
  if (slotsMode != null) {
    let values = lowerArmSlots(cx, node, slotsMode);
    if (cx.reachable) {
      cx.mergeArmSlots(m, values);
    }
    return;
  }
  let value = lowerValueNode(cx, node, isVoid, resultVt, m.returnTail);
  if (cx.reachable) {
    cx.mergeArm(m, value, armKind);
  }
}

/**
 * One arm of an `if` or `match` whose record a core returns as its
 * fields (MergeState.fields). A record literal of exactly the shape is
 * lowered straight to its fields, a nested `if` or `match` merges its own
 * arms' fields, and any other arm's record has its fields read.
 */
function lowerArmFields(cx: LoweringContext, node: Node, d: RecordDispatchInfo,
    resultVt: ValType | null): GrowableArray<i32> {
  if (node is Expression) {
    let literal = exactLiteralFields(cx, node, d);
    if (literal != null) {
      emitNodeExitDrops(cx, node);
      return literal;
    }
    if (node is IfExpression || node is MatchExpression) {
      cx.resultFields = d;
    }
  }
  let record = lowerValueNode(cx, node, false, resultVt);
  cx.resultFields = null;
  if (!cx.reachable) {
    return new Array<i32>();
  }
  return cx.recordFieldsOf(d, record);
}

/**
 * One arm of an inline `if` or `match` lowered into slots
 * (MergeState.slots): an expression is lowered into them, which merges a
 * nested `if` or `match` the same way, and a block runs its statements
 * and lowers its tail into them. Empty when the arm ends unreachable.
 */
function lowerArmSlots(cx: LoweringContext, node: Node,
    slots: ResultSlots): GrowableArray<i32> {
  if (node is BlockStatement) {
    let body = node.body;
    if (body.length == 0) {
      bail(cx, 'empty inline arm block');
    }
    cx.precellBindings(body);
    // A release region carries one value out of the block, and an
    // inline value has several.
    if (varDropPlans(cx, body).length > 0) {
      bail(cx, 'inline arm block releasing a var');
    }
    for (var i = 0; i < body.length - 1; i += 1) {
      if (!cx.reachable) {
        return new Array<i32>();
      }
      if (opensScope(cx, body[i])) {
        bail(cx, 'inline arm block with a release region');
      }
      lowerStmt(cx, body[i]);
    }
    if (!cx.reachable) {
      return new Array<i32>();
    }
    let last = body[body.length - 1];
    if (let ExpressionStatement {expression} = last) {
      let values = lowerArmSlots(cx, expression, slots);
      emitNodeExitDrops(cx, node);
      return values;
    }
    lowerStmt(cx, last);
    if (cx.reachable) {
      bail(cx, 'inline arm block tail');
    }
    return new Array<i32>();
  }
  if (node is ThrowExpression) {
    throwStmt(cx, node);
    return new Array<i32>();
  }
  if (node is CallExpression) {
    let t = cx.func.model.getNodeType(node);
    if (t != null && unwrapAliases(t) is NeverType) {
      lowerStmt(cx, new ExpressionStatement(node, node.loc));
      return new Array<i32>();
    }
  }
  if (!(node is Expression)) {
    cx.fail('inline arm kind');
  }
  let values = lowerIntoSlots(cx, node, slots.layout, slots.slotVts);
  emitNodeExitDrops(cx, node);
  return values;
}

/**
 * finishMerge, and in fields mode the record rebuilt from the merged
 * fields and held as them, so the core returns the fields and the record
 * dies. In slots mode the merged slots are left in `cx.mergedSlots`.
 */
function finishArmsMerge(cx: LoweringContext, m: MergeState,
    saved: HashMap<SymbolId, i32>): i32 {
  let result = cx.finishMerge(m, saved);
  let fieldsMode = m.fields;
  if (fieldsMode != null && m.block >= 0) {
    return packHeldRecord(cx, fieldsMode, m.fieldParams as GrowableArray<i32>);
  }
  if (m.slots != null && m.block >= 0) {
    cx.mergedSlots = m.slotParams;
  }
  return result;
}

/**
 * Binds a catch clause's parameter to the in-flight exception, read
 * out of the payload global at the top of the handler block.
 */
function bindCatchParam(cx: LoweringContext, handler: CatchClause) {
  let paramIdent = handler.param;
  if (paramIdent == null) {
    return;
  }
  let symOpt = cx.func.model.resolveDeclaration(paramIdent);
  if (symOpt == null) {
    bail(cx, 'catch parameter symbol');
  }
  let payloadG = cx.wasm.exceptionPayload as WasmGlobal;
  let gRef = cx.b.addRef(new IrRefGlobal(payloadG));
  var payload = cx.b.emit(IrOp.global_get, payloadG.type, -1, gRef);
  if (payloadG.type is ValTypeRefNull) {
    let t = refTargetOf(payloadG.type) as WasmType;
    payload = cx.b.emit(IrOp.ref_as_non_null,
        new ValTypeRef(t) as ValType, payload, -1);
  }
  cx.bindPatternSym(symOpt, payload);
}

// `FinallyScope.codeVar` values: which exit the dispatch must replay.
let finallyExitNormal = 0;
let finallyExitReturn = 1;
let finallyExitBreak = 2;
let finallyExitContinue = 3;
let finallyExitThrow = 4;
let finallyExitCancel = 5;

/** Parks `code` as the pending exit and branches to the dispatch. */
function parkExit(cx: LoweringContext, scope: FinallyScope, code: i32) {
  let i32vt = new ValTypeI32() as ValType;
  cx.b.varSet(scope.codeVar, cx.b.constI32(code, i32vt));
  cx.b.br(scope.dispatch, noValues());
  cx.reachable = false;
}

/**
 * Routes a `return` out through the innermost pending finalizer, or
 * returns false when there is none and the caller should just return.
 * `value` is -1 for a valueless return; the value is conformed to the
 * return slot HERE, so replaying it is a bare `ret`.
 */
export function returnThroughFinally(cx: LoweringContext, value: i32): boolean {
  if (cx.finallyScopes.length == 0) {
    return false;
  }
  let scope = cx.finallyScopes[cx.finallyScopes.length - 1];
  if (value >= 0) {
    if (scope.returnVar < 0) {
      let slot = returnSlotVt(cx);
      if (slot == null) {
        bail(cx, 'return value through a finalizer without a return slot');
      }
      scope.returnVar = cx.b.newVar(slot);
    }
    cx.b.varSet(scope.returnVar, cx.conformToSlot(value,
        cx.b.body.varTypes[scope.returnVar], 'return through a finalizer'));
  }
  scope.exitsByReturn = true;
  parkExit(cx, scope, finallyExitReturn);
  return true;
}

/**
 * The multi-value sibling of `returnThroughFinally`: one variable per
 * result slot, allocated on the first multi-value return out of the
 * region. The replay arm reads them back and either parks them into
 * the next enclosing region or emits the ret_multi itself.
 */
export function returnMultiThroughFinally(cx: LoweringContext,
    values: GrowableArray<i32>): boolean {
  if (cx.finallyScopes.length == 0) {
    return false;
  }
  let scope = cx.finallyScopes[cx.finallyScopes.length - 1];
  var vars = scope.returnVars;
  if (vars == null) {
    let created = new Array<i32>();
    for (let slot in cx.func.signature.returns) {
      created.push(cx.b.newVar(slot));
    }
    scope.returnVars = created;
    vars = created;
  }
  let rv = vars as GrowableArray<i32>;
  for (var i = 0; i < values.length; i += 1) {
    cx.b.varSet(rv[i], cx.conformToSlot(values[i],
        cx.b.body.varTypes[rv[i]], 'multi-value return through a finalizer'));
  }
  scope.exitsByReturn = true;
  parkExit(cx, scope, finallyExitReturn);
  return true;
}

/**
 * The innermost finalizer a `break`/`continue` here owes, or null.
 * A loop opened INSIDE the region is left without leaving the region,
 * and `loopDepth` is what tells the two apart.
 */
function finallyForLoopExit(cx: LoweringContext): FinallyScope | null {
  if (cx.finallyScopes.length == 0) {
    return null;
  }
  let scope = cx.finallyScopes[cx.finallyScopes.length - 1];
  return if (scope.loopDepth >= cx.loops.length) scope else null;
}

/** Routes a `break` out through pending finalizers; false when none. */
export function breakThroughFinally(cx: LoweringContext): boolean {
  let scope = finallyForLoopExit(cx);
  if (scope == null) {
    return false;
  }
  scope.exitsByBreak = true;
  parkExit(cx, scope, finallyExitBreak);
  return true;
}

/** Routes a `continue` out through pending finalizers; false when none. */
export function continueThroughFinally(cx: LoweringContext): boolean {
  let scope = finallyForLoopExit(cx);
  if (scope == null) {
    return false;
  }
  scope.exitsByContinue = true;
  parkExit(cx, scope, finallyExitContinue);
  return true;
}

/** An arm of the protected part that completed normally: park the
 * value and join the dispatch. */
function finishProtectedArm(cx: LoweringContext, scope: FinallyScope,
    resultVar: i32, value: i32): void {
  if (!cx.reachable) {
    return;
  }
  if (resultVar >= 0) {
    cx.b.varSet(resultVar, cx.conformToSlot(value,
        cx.b.body.varTypes[resultVar], 'try arm type'));
  }
  scope.exitsNormally = true;
  parkExit(cx, scope, finallyExitNormal);
}

/**
 * `try` with a finalizer, in value or statement position.
 *
 * The finalizer is emitted ONCE. Every way out of the protected part
 * parks what it was doing in `codeVar` and branches to a single
 * dispatch block, which runs the finalizer and then replays it:
 *
 * ```
 * protector:  try_br(tag, protectedB, ehB)
 * protectedB: [inner try_br(tag, bodyB, catchB) when there is a catch]
 *             body/handler; completing normally parks the value and
 *             the NORMAL code; return/break/continue park theirs
 * ehB:        payloadVar = <payload global>; park THROW
 * dispatch:   <the finalizer>, then replay codeVar — fall through,
 *             ret, br to the loop's exit/continue target, or restore
 *             the payload global and rethrow
 * ```
 *
 * The dispatch must sit OUTSIDE the region, or a finalizer that threw
 * would re-enter itself, and it does: `ehB` is the try_br's handler
 * edge, which the emitter streams past the `end` of the try_table, so
 * a block it branches to is never inside. That is also the reason
 * there is one copy and not one per exit edge — a copy on the normal
 * path would sit inside the region.
 *
 * A `catch` handler nests INSIDE this region, in its own try_br, so a
 * handler that throws still runs the finalizer.
 *
 * The in-flight payload rides a variable rather than staying in the
 * global, because the finalizer may itself throw and catch, and that
 * overwrites the global before the rethrow reads it.
 *
 * When the module has cancellation infra, a second region on the
 * cancellation tag nests OUTSIDE the error region, its handler edge
 * parking the CANCEL code — a cancellation unwind is one more exit,
 * replayed as a rethrow of its own tag (docs/design/cancellation.md).
 * No payload rides with it: nothing may observe a cancellation. The
 * dispatch stays outside both regions for the same reason as before,
 * one level up: the cancel handler edge is a predecessor, and the
 * emitter streams it past the outer region's end.
 */
function lowerFinallyRegion(cx: LoweringContext, mirrored: GrowableArray<SymbolId>,
    isVoid: boolean, resultVt: ValType | null,
    lowerProtected: (scope: FinallyScope, resultVar: i32,
        saved: HashMap<SymbolId, i32>) => void,
    lowerFinalizer: (scope: FinallyScope) => void,
    finalizeOnNormal: boolean = true): i32 {
  if (cx.wasm.exceptionTag == null || cx.wasm.exceptionPayload == null) {
    bail(cx, 'exception infra missing');
  }
  let ownedVars = new Array<SymbolId>();
  for (let symId in mirrored) {
    if (cx.tryVars.has(symId)) { continue; }
    let vt = cx.bindingValType(symId);
    let varId = cx.b.newVar(vt);
    cx.b.varSet(varId, cx.conformToSlot(cx.env[symId], vt, 'try var type'));
    cx.tryVars[symId] = varId;
    ownedVars.push(symId);
  }

  let tag = cx.wasm.exceptionTag as WasmTag;
  let payloadG = cx.wasm.exceptionPayload as WasmGlobal;
  let i32vt = new ValTypeI32() as ValType;
  let codeVar = cx.b.newVar(i32vt);
  let resultVar = if (resultVt != null) cx.b.newVar(resultVt) else -1;
  let payloadVar = cx.b.newVar(payloadG.type);

  let dispatchB = cx.b.newBlock();
  let scope = new FinallyScope(dispatchB, codeVar, cx.loops.length);
  let protectedB = cx.b.newBlock();
  let ehB = cx.b.newBlock();
  let cancelTagOpt = cx.wasm.cancellationTag;
  var ehCancelB = -1;
  if (cancelTagOpt != null) {
    ehCancelB = cx.b.newBlock();
    let midB = cx.b.newBlock();
    cx.b.tryBr(cx.b.addRef(new IrRefTag(cancelTagOpt as WasmTag)),
        midB, noValues(), ehCancelB, noValues());
    cx.b.switchTo(midB);
  }
  cx.b.tryBr(cx.b.addRef(new IrRefTag(tag)), protectedB, noValues(),
      ehB, noValues());

  let saved = cx.copyEnv();

  // ---- the protected part ------------------------------------------
  cx.finallyScopes.push(scope);
  cx.b.switchTo(protectedB);
  lowerProtected(scope, resultVar, saved);
  cx.finallyScopes.pop();

  // ---- the region's handler edge ------------------------------------
  cx.env = cx.copyOf(saved);
  cx.reachable = true;
  cx.b.switchTo(ehB);
  let ehPayloadRef = cx.b.addRef(new IrRefGlobal(payloadG));
  cx.b.varSet(payloadVar,
      cx.b.emit(IrOp.global_get, payloadG.type, -1, ehPayloadRef));
  cx.b.varSet(codeVar, cx.b.constI32(finallyExitThrow, i32vt));
  cx.b.br(dispatchB, noValues());

  // ---- the cancel region's handler edge -----------------------------
  if (ehCancelB >= 0) {
    cx.env = cx.copyOf(saved);
    cx.reachable = true;
    cx.b.switchTo(ehCancelB);
    cx.b.varSet(codeVar, cx.b.constI32(finallyExitCancel, i32vt));
    cx.b.br(dispatchB, noValues());
  }

  // ---- the finalizer, once ------------------------------------------
  cx.env = cx.copyOf(saved);
  cx.reachable = true;
  cx.b.switchTo(dispatchB);
  for (let symId in mirrored) {
    cx.env[symId] = cx.b.varGet(cx.tryVars[symId]);
  }
  // A region whose finalizer serves only the exits that leave early —
  // a `var` owner's release region closed before a statement that
  // moves the binding, whose value is still the binding's when the
  // region completes — lets normal completion past the finalizer.
  var normalB = -1;
  if (!finalizeOnNormal && scope.exitsNormally) {
    normalB = cx.b.newBlock();
    let finB = cx.b.newBlock();
    cx.b.brIf(cx.b.binary(IrOp.i32_eq, i32vt, cx.b.varGet(codeVar),
            cx.b.constI32(finallyExitNormal, i32vt)),
        normalB, noValues(), finB, noValues());
    cx.b.switchTo(finB);
  }
  lowerFinalizer(scope);
  // The mirrors this try owns go out of scope here: an exit replayed
  // below belongs to whatever encloses this try. An enclosing try's
  // stay live — its handler is still reachable.
  for (let symId in ownedVars) {
    cx.tryVars.delete(symId);
  }
  if (!cx.reachable) {
    // The finalizer left on its own — returned, broke, or threw. What
    // was pending is discarded, which is what the language says.
    if (normalB < 0) {
      return -1;
    }
    // Except normal completion, which bypassed it.
    cx.b.switchTo(normalB);
    cx.reachable = true;
    return if (resultVar >= 0) cx.b.varGet(resultVar) else -1;
  }

  // ---- replay -------------------------------------------------------
  // Only the exits that actually occurred get an arm, so a plain
  // try/finally with no early exit is one br_if against a rethrow.
  let code = cx.b.varGet(codeVar);
  // No continuation when nothing completed the protected part
  // normally: every path through it threw or left, so the try is the
  // end of its own control flow and the caller stops lowering here.
  let contB = if (normalB >= 0) normalB
    else if (scope.exitsNormally) cx.b.newBlock()
    else -1;
  var elseB = cx.b.newBlock();
  if (contB >= 0 && normalB < 0) {
    cx.b.brIf(cx.b.binary(IrOp.i32_eq, i32vt, code,
            cx.b.constI32(finallyExitNormal, i32vt)),
        contB, noValues(), elseB, noValues());
  } else {
    cx.b.br(elseB, noValues());
  }

  if (scope.exitsByReturn) {
    cx.b.switchTo(elseB);
    let armB = cx.b.newBlock();
    let nextB = cx.b.newBlock();
    cx.b.brIf(cx.b.binary(IrOp.i32_eq, i32vt, code,
            cx.b.constI32(finallyExitReturn, i32vt)),
        armB, noValues(), nextB, noValues());
    cx.b.switchTo(armB);
    let multiVars = scope.returnVars;
    if (multiVars != null) {
      let parked = multiVars as GrowableArray<i32>;
      let values = new Array<i32>();
      for (let rv in parked) {
        values.push(cx.b.varGet(rv));
      }
      if (!returnMultiThroughFinally(cx, values)) {
        cx.b.retMulti(values);
      }
    } else {
      let value = if (scope.returnVar >= 0) cx.b.varGet(scope.returnVar) else -1;
      if (!returnThroughFinally(cx, value)) {
        if (value >= 0) {
          cx.b.ret(value);
        } else {
          cx.b.retVoid();
        }
      }
    }
    elseB = nextB;
  }

  if (scope.exitsByBreak || scope.exitsByContinue) {
    if (cx.loops.length == 0) {
      bail(cx, 'loop exit through a finalizer outside a loop');
    }
  }
  if (scope.exitsByBreak) {
    cx.b.switchTo(elseB);
    let armB = cx.b.newBlock();
    let nextB = cx.b.newBlock();
    cx.b.brIf(cx.b.binary(IrOp.i32_eq, i32vt, code,
            cx.b.constI32(finallyExitBreak, i32vt)),
        armB, noValues(), nextB, noValues());
    cx.b.switchTo(armB);
    if (!breakThroughFinally(cx)) {
      let loop = cx.loops[cx.loops.length - 1];
      cx.b.br(loop.exit, cx.envValues(loop.carried));
    }
    elseB = nextB;
  }
  if (scope.exitsByContinue) {
    cx.b.switchTo(elseB);
    let armB = cx.b.newBlock();
    let nextB = cx.b.newBlock();
    cx.b.brIf(cx.b.binary(IrOp.i32_eq, i32vt, code,
            cx.b.constI32(finallyExitContinue, i32vt)),
        armB, noValues(), nextB, noValues());
    cx.b.switchTo(armB);
    if (!continueThroughFinally(cx)) {
      let loop = cx.loops[cx.loops.length - 1];
      cx.b.br(loop.continueTarget, cx.envValues(loop.carried));
    }
    elseB = nextB;
  }

  // A pending cancellation continues on its own tag, payload-free.
  // Through an ENCLOSING finalizer it parks rather than rethrows —
  // the same routing a return uses — because in a split body this
  // dispatch block can be re-entered from the dispatcher and hoist
  // outside the enclosing region, where a rethrow would silently skip
  // it. Parking is placement-independent; only the outermost region
  // rethrows, into step()'s cancellation capture (or the caller).
  if (ehCancelB >= 0) {
    cx.b.switchTo(elseB);
    let armB = cx.b.newBlock();
    let nextB = cx.b.newBlock();
    cx.b.brIf(cx.b.binary(IrOp.i32_eq, i32vt, code,
            cx.b.constI32(finallyExitCancel, i32vt)),
        armB, noValues(), nextB, noValues());
    cx.b.switchTo(armB);
    if (cx.finallyScopes.length > 0) {
      let outer = cx.finallyScopes[cx.finallyScopes.length - 1];
      parkExit(cx, outer, finallyExitCancel);
    } else {
      cx.b.throwWithTag(noValues(),
          cx.b.addRef(new IrRefTag(cancelTagOpt as WasmTag)));
    }
    elseB = nextB;
  }

  // Whatever is left is the in-flight exception: put the payload back
  // where a handler expects it and rethrow.
  cx.b.switchTo(elseB);
  let rethrowRef = cx.b.addRef(new IrRefGlobal(payloadG));
  cx.b.emitVoid(IrOp.global_set, cx.b.varGet(payloadVar), rethrowRef);
  cx.b.throwWithTag(noValues(), cx.b.addRef(new IrRefTag(tag)));

  if (contB < 0) {
    cx.reachable = false;
    return -1;
  }
  cx.b.switchTo(contB);
  cx.reachable = true;
  return if (resultVar >= 0) cx.b.varGet(resultVar) else -1;
}

/**
 * `try` with a finalizer and/or a `cancel` clause, in value or
 * statement position — the finalizer-region shape above, with the
 * protected part being the body (and its `catch`, which nests INSIDE
 * the region in its own try_br so a handler that throws still runs
 * the finalizer).
 *
 * The `cancel` clause is a finalizer that runs only on the CANCEL
 * exit code (docs/design/cancellation.md): its region parks the
 * cancellation like any exit, runs the block at the one dispatch
 * copy, and the replay continues the unwind — parked outward through
 * any enclosing finalizer, so with both clauses present the cancel
 * block runs first (inner region), then the written finalizer (outer
 * region), then the unwind continues. It is NOT a region-plus-rethrow
 * on the handler edge: a rethrow relies on the handler's placement
 * inside the enclosing regions, and in a split body a re-entered
 * dispatch target gives these blocks extra predecessors that hoist
 * them outside — the park-and-replay shape is placement-independent.
 */
function lowerTryFinally(cx: LoweringContext, expr: TryExpression) {
  let nodeType = cx.substitutedNodeType(expr);
  if (nodeType == null) {
    bail(cx, 'untyped try expression');
  }
  // Void-ness is per specialization: a try whose generic type is a
  // bare `T` is a value at `T = i32` and void at `T = void`, the same
  // rule zero-width arguments follow.
  let isVoid = isVoidOrNeverType(nodeType);
  let resultVt = if (!isVoid) {
    let vt = typeToValTypeWithSub(cx.wasm, nodeType, cx.func);
    cx.requireSupported(vt, 'try result type');
    vt
  } else {
    null
  };

  // Mirrors for every enclosing local the region or the finalizer
  // assigns — the same variables a plain try/catch uses, for the same
  // reason: a handler edge carries the values live at its try's
  // entry, so the dispatch reads the live ones back out instead.
  let bodyAssigned = cx.assignedEnvSyms(expr.body as Node);
  let mirrored = cx.assignedEnvSyms(expr.body as Node);
  if (expr.handler != null) {
    cx.appendAssignedEnvSyms((expr.handler as CatchClause).body as Node, mirrored);
  }
  let clauseActive =
      expr.cancelBlock != null && cx.wasm.cancellationTag != null;
  if (clauseActive) {
    cx.appendAssignedEnvSyms(expr.cancelBlock as Node, mirrored);
  }
  if (expr.finalizer != null) {
    cx.appendAssignedEnvSyms(expr.finalizer as Node, mirrored);
  }

  let tag = cx.wasm.exceptionTag as WasmTag;
  let lowerArmsInto = (scope: FinallyScope, resultVar: i32,
      saved: HashMap<SymbolId, i32>) => {
    let handler = expr.handler;
    if (handler == null) {
      let bodyValue = lowerValueNode(cx, expr.body as Node, isVoid, resultVt);
      finishProtectedArm(cx, scope, resultVar, bodyValue);
      return;
    }
    let bodyB = cx.b.newBlock();
    let catchB = cx.b.newBlock();
    cx.b.tryBr(cx.b.addRef(new IrRefTag(tag)), bodyB, noValues(),
        catchB, noValues());

    cx.b.switchTo(bodyB);
    let bodyValue = lowerValueNode(cx, expr.body as Node, isVoid, resultVt);
    finishProtectedArm(cx, scope, resultVar, bodyValue);

    cx.env = cx.copyOf(saved);
    cx.reachable = true;
    cx.b.switchTo(catchB);
    for (let symId in bodyAssigned) {
      cx.env[symId] = cx.b.varGet(cx.tryVars[symId]);
    }
    bindCatchParam(cx, handler);
    let catchValue = lowerValueNode(cx, handler.body as Node, isVoid, resultVt);
    finishProtectedArm(cx, scope, resultVar, catchValue);
  };

  // The cancel clause's region: its "finalizer" runs the block only
  // when the parked exit is CANCEL, and passes every other exit
  // through untouched.
  let lowerCancelClauseRegion = () => lowerFinallyRegion(cx, mirrored, isVoid, resultVt,
      lowerArmsInto,
      (scope) => {
        let i32vt = new ValTypeI32() as ValType;
        let code = cx.b.varGet(scope.codeVar);
        let runB = cx.b.newBlock();
        let skipB = cx.b.newBlock();
        cx.b.brIf(cx.b.binary(IrOp.i32_eq, i32vt, code,
                cx.b.constI32(finallyExitCancel, i32vt)),
            runB, noValues(), skipB, noValues());
        cx.b.switchTo(runB);
        lowerValueNode(cx, expr.cancelBlock as Node, true, null);
        if (cx.reachable) {
          cx.b.br(skipB, noValues());
        }
        cx.b.switchTo(skipB);
        cx.reachable = true;
      });

  let finalizerOpt = expr.finalizer;
  if (finalizerOpt == null) {
    // An active cancel clause without a finalizer (lowerTryExpression
    // only routes here when at least one of the two is live).
    return lowerCancelClauseRegion();
  }
  let finalizer = finalizerOpt as Node;
  if (clauseActive) {
    return lowerFinallyRegion(cx, mirrored, isVoid, resultVt,
        (outerScope, resultVar, saved) => {
          let v = lowerCancelClauseRegion();
          finishProtectedArm(cx, outerScope, resultVar, v);
        },
        (fscope) => { lowerValueNode(cx, finalizer, true, null); });
  }
  return lowerFinallyRegion(cx, mirrored, isVoid, resultVt, lowerArmsInto,
      (fscope) => { lowerValueNode(cx, finalizer, true, null); });
}

/**
 * `shielded { ... }` (docs/design/cancellation.md §Shields): the body
 * runs with the current scope swapped to the shield scope — the
 * uncancellable root, so async work created inside binds to a scope
 * no cancellation reaches — and the split pass emits no checkpoint
 * for awaits inside the extent (`FunctionLowerer.shieldDepth` marks
 * their terminators; their resumes re-install the shield current scope).
 * The restore is a finalizer region, so every exit — normal, early,
 * error unwind, a raised cancellation crossing the region — puts the
 * current scope back, and level-triggering resumes at the closing brace.
 */
export function lowerShieldedRegion(cx: LoweringContext,
    stmt: ShieldedStatement): void {
  if (!cx.wasm.scopeCancellationActive) {
    // Under a closed scope-cancellation gate there are no checkpoints
    // to mask and no scopes to rebind: the region is only its body.
    // The flag, not the tag — generator disposal mints the tag alone,
    // and the scope functions resolved below exist only under the
    // flag.
    lowerValueNode(cx, stmt.body as Node, true, null);
    return;
  }
  let curOpt = cx.wasm.getStdlibFunc('async', 'currentScope');
  if (curOpt == null) {
    bail(cx, 'shielded without scope infra');
  }
  let currentScopeFn = curOpt as WasmFunction;
  if (currentScopeFn.signature.returns.length != 1) {
    bail(cx, 'currentScope does not return a scope');
  }
  let scopeVt = currentScopeFn.signature.returns[0];
  // The scope operations are symbol-keyed CancelScope methods, so no
  // user-callable function is involved.
  let targetOpt = refTargetOf(scopeVt);
  if (targetOpt == null || !(targetOpt is WasmStruct)) {
    bail(cx, 'scope type is not a class');
  }
  let sctOpt = (targetOpt as WasmStruct).classType;
  if (sctOpt == null) {
    bail(cx, 'scope struct has no class type');
  }
  let sKey = getClassKey(sctOpt as ClassType);
  let shieldKey = getClassMethodKey(sKey, ':shieldScope');
  let installKey = getClassMethodKey(sKey, ':install');
  if (!cx.wasm.classMethodMap.has(shieldKey) ||
      !cx.wasm.classMethodMap.has(installKey)) {
    bail(cx, 'shielded without scope methods');
  }
  let shieldFn = cx.wasm.classMethodMap[shieldKey];
  let installFn = cx.wasm.classMethodMap[installKey];

  let cur = cx.b.callFunc(cx.b.addRef(new IrRefFunc(currentScopeFn)),
      noValues(), scopeVt);
  let shieldArgs = growable<i32>([cur]);
  let shield = cx.b.callFunc(cx.b.addRef(new IrRefFunc(shieldFn)),
      shieldArgs, scopeVt);
  let installArgs = growable<i32>([shield]);
  let prev = cx.b.callFunc(cx.b.addRef(new IrRefFunc(installFn)),
      installArgs, scopeVt);

  let mirrored = cx.assignedEnvSyms(stmt.body as Node);
  lowerFinallyRegion(cx, mirrored, true, null,
      (scope, resultVar, saved) => {
        let fl = cx.host;
        if (fl != null) {
          fl.shieldDepth = fl.shieldDepth + 1;
        }
        lowerValueNode(cx, stmt.body as Node, true, null);
        if (fl != null) {
          fl.shieldDepth = fl.shieldDepth - 1;
        }
        finishProtectedArm(cx, scope, resultVar, -1);
      },
      (fscope) => {
        let restoreArgs = growable<i32>([prev]);
        let ignored = cx.b.callFunc(cx.b.addRef(new IrRefFunc(installFn)),
            restoreArgs, scopeVt);
      });
}

/**
 * try/catch, in value or statement position. The protector block
 * ends with try_br: normal flow enters the body; an exception thrown
 * anywhere in the body's dynamic extent lands at the catch block
 * with the payload in the exception global (streaming-compatible).
 * Both arms join like an if-expression. A finalizer takes the
 * separate shape in `lowerTryFinally`.
 *
 * Enclosing locals the body ASSIGNS cannot ride the protected edge —
 * it carries the state at try entry, so the handler would not see an
 * assignment the body completed before it threw. Each gets a mutable
 * variable (ir.md §5.1, one dedicated wasm local) that every
 * assignment mirrors into and the handler reads back; the binding
 * itself stays ordinary SSA, so nothing else about it gets slower.
 *
 * A suspension body is the exception: its locals do not survive a
 * suspension, so there the symbols are celled instead (RTA, gated on
 * the enclosing function being `async`/`gen`) and never reach here.
 */
export function lowerTryExpression(cx: LoweringContext, expr: TryExpression): i32 {
  // Under a closed cancellation gate a `cancel` clause is dead code:
  // nothing in the program can raise on the cancellation channel, so
  // the clause is elided rather than lowered.
  let cancelActive =
      expr.cancelBlock != null && cx.wasm.cancellationTag != null;
  if (expr.finalizer != null || cancelActive) {
    // A `cancel` clause takes the finalizer-region shape too — its
    // block is a finalizer that runs only on the CANCEL exit code —
    // so both clauses route to the same construction.
    return lowerTryFinally(cx, expr);
  }
  if (expr.handler == null) {
    if (expr.cancelBlock != null) {
      // A cancel-only try whose clause was elided: only the body
      // remains.
      let bodyType = cx.func.model.getNodeType(expr);
      if (bodyType == null) {
        bail(cx, 'untyped try expression');
      }
      let bodyIsVoid = isVoidOrNeverType(bodyType);
      let bodyVt = if (!bodyIsVoid) {
        let vt = typeToValTypeWithSub(cx.wasm, bodyType, cx.func);
        cx.requireSupported(vt, 'try result type');
        vt
      } else {
        null
      };
      return lowerValueNode(cx, expr.body as Node, bodyIsVoid, bodyVt);
    }
    bail(cx, 'try without handler');
  }
  if (cx.wasm.exceptionTag == null || cx.wasm.exceptionPayload == null) {
    bail(cx, 'exception infra missing');
  }
  let bodyAssigned = cx.assignedEnvSyms(expr.body as Node);
  if (bodyAssigned.length > 0 && inSuspensionBody(cx)) {
    // Celling is RTA's job and it gates on the same condition; a
    // symbol arriving here uncelled means the two disagree, and a
    // wasm local would silently lose its value across the suspension.
    bail(cx, 'uncelled assignment to enclosing local in suspension try body');
  }
  let nodeType = cx.substitutedNodeType(expr);
  if (nodeType == null) {
    bail(cx, 'untyped try expression');
  }
  // Void-ness is per specialization: a try whose generic type is a
  // bare `T` is a value at `T = i32` and void at `T = void`, the same
  // rule zero-width arguments follow.
  let isVoid = isVoidOrNeverType(nodeType);
  let resultVt = if (!isVoid) {
    let vt = typeToValTypeWithSub(cx.wasm, nodeType, cx.func);
    cx.requireSupported(vt, 'try result type');
    vt
  } else {
    null
  };

  let handler = expr.handler as CatchClause;
  // Both arms can assign, and both feed the join.
  let mergeSyms = cx.assignedEnvSyms(handler.body as Node);
  cx.appendAssignedEnvSyms(expr.body as Node, mergeSyms);

  // Mirror variables for what the body assigns, seeded with the value
  // at entry (the handler sees that when the body throws before
  // assigning). An enclosing try already mirroring a symbol keeps
  // ownership of its variable — one per symbol, always holding the
  // last value written.
  let ownedVars = new Array<SymbolId>();
  for (let symId in bodyAssigned) {
    if (cx.tryVars.has(symId)) { continue; }
    let vt = cx.bindingValType(symId);
    let varId = cx.b.newVar(vt);
    cx.b.varSet(varId, cx.conformToSlot(cx.env[symId], vt, 'try var type'));
    cx.tryVars[symId] = varId;
    ownedVars.push(symId);
  }

  let tagRef = cx.b.addRef(new IrRefTag(cx.wasm.exceptionTag as WasmTag));
  let bodyB = cx.b.newBlock();
  let catchB = cx.b.newBlock();
  let tryInst = cx.b.tryBr(tagRef, bodyB, noValues(), catchB, noValues());

  let m = new MergeState(mergeSyms, isVoid, resultVt);

  let saved = cx.copyEnv();
  cx.b.switchTo(bodyB);
  let bodyValue = lowerValueNode(cx, expr.body as Node, isVoid, resultVt);
  if (cx.reachable) {
    cx.mergeArm(m, bodyValue, 'try arm type');
  }

  cx.env = cx.copyOf(saved);
  cx.reachable = true;
  cx.b.switchTo(catchB);
  // The protected edge carried the entry values; read the body's
  // assignments back out of their variables instead.
  for (let symId in bodyAssigned) {
    cx.env[symId] = cx.b.varGet(cx.tryVars[symId]);
  }
  bindCatchParam(cx, handler);
  let catchValue = lowerValueNode(cx, handler.body as Node, isVoid, resultVt);
  if (cx.reachable) {
    cx.mergeArm(m, catchValue, 'try arm type');
  }

  // Past the join both arms' assignments arrive as ordinary merge
  // params, so the mirrors this try owns go out of scope. An enclosing
  // try's stay live — its handler is still reachable.
  for (let symId in ownedVars) {
    cx.tryVars.delete(symId);
  }
  // Record where the region ends. When the catch diverges the join has
  // one predecessor and dominance alone would count it as protected —
  // this is what tells emission and the split passes otherwise
  // (ir.zena, `tryJoin`).
  if (m.block >= 0) {
    cx.b.body.tryJoin[tryInst] = m.block;
  }
  return cx.finishMerge(m, saved);
}

/**
 * The classes whose `:dispose` can release a value of type `t`: one
 * for a lone owner, every member for a union owner (`Own<A | B>`, or a
 * union of plain disposables) — the release then dispatches on the
 * dynamic class in emitDisposeDispatch.
 *
 * A handle is erased, so `Own<Descriptor>` releases through
 * `Descriptor`'s member; the checker has already established that the
 * members exist, and that a `Borrow<R>` never reaches here.
 */
export function disposeOwnerClasses(cx: LoweringContext, t: Type): GrowableArray<ClassType> {
  // Peel the handle the way the checker did when it found the `:dispose`.
  // Neither `baseType` nor `unwrapAliases` gets here: the handles are
  // *generic* aliases, so `Own<T> = T` has the bare type parameter as its
  // target and following it lands on `T` rather than on the resource.
  // A `T | null` binding releases through `T` (null skips at runtime).
  var target = soleNonNullMember(t) ?? t;
  while (isOwnershipWrapper(target)) {
    let inner = ownershipHandleInner(target);
    if (inner == null) { break; }
    target = inner;
  }
  let base = unwrapAliases(target);
  let result = new Array<ClassType>();
  match (base) {
    case ClassType as ct: { result.push(ct); }
    case UnionType {types}: {
      for (let m in types) {
        let mb = unwrapAliases(m);
        if (!(mb is ClassType)) {
          bail(cx, 'using operand is not a class');
        }
        result.push(mb);
      }
    }
    case _: { bail(cx, 'using operand is not a class'); }
  }
  return result;
}

/**
 * Emits one release: `value.[Disposable.dispose]()`.
 *
 * Dispatch follows the ordinary method-call rule rather than a shortcut —
 * an overridden `:dispose` reached through a base-typed binding has to
 * run the subclass's release, and a `resource class` hierarchy is exactly
 * where that happens.
 */
function emitDispose(cx: LoweringContext, value: i32, ownerClass: ClassType) {
  let ct = ownerClass;
  // `:dispose`, or a future's `[release]` (releaseMemberOf).
  let member = releaseMemberOf(ct);
  let cKey = getClassKey(ct);
  let classInfo = if (let (true, info) = cx.wasm.classInfos.get(cKey)) info else null;
  if (classInfo == null) {
    bail(cx, 'using receiver class not discovered');
  }

  var method: WasmFunction | null = null;
  var curr: ClassType | null = ct;
  while (curr != null && method == null) {
    var g: ClassType | null = curr;
    while (g != null && method == null) {
      let key = getClassMethodKey(getClassKey(g), member);
      if (let (true, m) = cx.wasm.classMethodMap.get(key)) {
        method = m;
      }
      g = g.genericSource;
    }
    curr = curr.superType;
  }
  if (method == null) {
    // The checker required the member, so this means RTA pruned it —
    // the dependency `checkUsingStatement` records is what roots it.
    bail(cx, 'using dispose method not found ' + (cKey as String));
  }
  let sig = method.signature;
  if (sig.params.length != 1) {
    bail(cx, 'using dispose arity');
  }

  let receiver = cx.conformToSlot(value, sig.params[0], 'dispose receiver type');
  let args = growable<i32>([receiver]);

  if (!cx.exactValues.has(value) &&
      cx.memberProvidedBySubclass(classInfo, member)) {
    if (cx.vtableSlotIndex(classInfo, member) < 0) {
      bail(cx, 'overridden dispose without vtable slot');
    }
    cx.lowerVtableCall(receiver, classInfo, member, sig, args, null);
    return;
  }
  cx.b.callFunc(cx.b.addRef(new IrRefFunc(method)), args, null);
}

/**
 * One release, dispatched on the dynamic class when the static owner
 * is a union: test each member in order and dispose as that member.
 * When `owners` is every member the value can be, the last needs no
 * test — emitDispose's receiver conformance cast enforces it; when it
 * is not (DropPlan.partial), the last is tested too and a value that
 * is none of them releases nothing. Relatives in the union stay correct
 * in any order, because emitDispose dispatches virtually when a
 * subclass overrides `:dispose`.
 */
function emitDisposeDispatch(cx: LoweringContext, value: i32, owners: GrowableArray<ClassType>,
    exhaustive: boolean = true) {
  if (owners.length == 1 && exhaustive) {
    emitDispose(cx, value, owners[0]);
    return;
  }
  let endB = cx.b.newBlock();
  for (var i = 0; i < owners.length; i += 1) {
    if (exhaustive && i == owners.length - 1) {
      emitDispose(cx, value, owners[i]);
      cx.b.br(endB, noValues());
    } else {
      let cKey = getClassKey(owners[i]);
      let info = if (let (true, ci) = cx.wasm.classInfos.get(cKey)) ci else null;
      if (info == null) {
        bail(cx, 'using receiver class not discovered');
      }
      let targetVt = new ValTypeRef(info.structType) as ValType;
      let cond = cx.b.emit(IrOp.ref_test, new ValTypeI32() as ValType, value,
          cx.b.internType(targetVt));
      let doB = cx.b.newBlock();
      let nextB = cx.b.newBlock();
      cx.b.brIf(cond, doB, noValues(), nextB, noValues());
      cx.b.switchTo(doB);
      emitDispose(cx, value, owners[i]);
      cx.b.br(endB, noValues());
      cx.b.switchTo(nextB);
    }
  }
  if (!exhaustive) {
    // None of them: nothing to release.
    cx.b.br(endB, noValues());
  }
  cx.b.switchTo(endB);
}

/**
 * Emits a release, guarded when the value can be null at runtime: a
 * nullable binding holds nothing when it holds null, and the dispose is
 * skipped rather than trapped — the same rule TC39 `using` adopted.
 */
export function emitMaybeDispose(cx: LoweringContext, value: i32, owners: GrowableArray<ClassType>): void {
  let vt = cx.valueTypeOf(value);
  if (!isRefValType(vt)) {
    bail(cx, 'dispose operand kind');
  }
  if (vt is ValTypeRef) {
    // The one non-nullable reference form; everything else — refnull,
    // and the abstract eqref/anyref/structref a union of unrelated
    // classes rides in — admits null at runtime regardless of the
    // static type, and the dispatch ladder's receiver casts are
    // nullable and would NOT trap on one.
    emitDisposeDispatch(cx, value, owners);
    return;
  }
  let isNull = cx.b.emit(
      IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1);
  let skipB = cx.b.newBlock();
  let doB = cx.b.newBlock();
  cx.b.brIf(isNull, skipB, noValues(), doB, noValues());
  cx.b.switchTo(doB);
  emitDisposeDispatch(cx, value, owners);
  cx.b.br(skipB, noValues());
  cx.b.switchTo(skipB);
}

/**
 * How a value is released at a drop: a dispose of the resource it owns,
 * or, for a tuple holding owners (#495), a release of each owner element.
 */
export final class DropPlan {
  /** The classes a dispose dispatches over, for a resource owner. */
  owners: GrowableArray<ClassType> | null;
  /** The tuple type whose owner elements are released, for a tuple. */
  tuple: Type | null;
  /**
   * Whether the value may be none of `owners`: a union of a `scoped T`
   * container and members that hold nothing, such as `Some<Own<R>> |
   * None`. Every owner is then tested, and a value that is none of
   * them releases nothing.
   */
  var partial: boolean = false;

  new(this.owners, this.tuple);
}

/** The drop plan for a value of type `t` (see `DropPlan`). */
export function dropPlanOf(cx: LoweringContext, t: Type): DropPlan {
  let core = soleNonNullMember(t) ?? t;
  if (tupleHoldsOwner(core)) {
    return new DropPlan(null, t);
  }
  let containers = releasingUnionMembers(core);
  if (containers != null) {
    let plan = new DropPlan(containers, null);
    plan.partial = true;
    return plan;
  }
  return new DropPlan(disposeOwnerClasses(cx, t), null);
}

/**
 * The members of a union that are `scoped T` containers of an owner,
 * when it has any; null for anything else.
 */
function releasingUnionMembers(t: Type): GrowableArray<ClassType> | null {
  let resolved = unwrapAliases(t);
  if (!(resolved is UnionType)) {
    return null;
  }
  let found = new Array<ClassType>();
  for (let member in resolved.types) {
    let m = unwrapAliases(member);
    if (m is ClassType && !m.isResource && holdsOwner(m)) {
      found.push(m);
    }
  }
  return if (found.length > 0) found else null;
}

/**
 * Releases what a value of `t` owns, as its implicit drop would: the
 * `release_owned` intrinsic, which a generic body calls for a value
 * only its use site knew to release (`Future`'s `[release]`), and a
 * store that replaces the value of a `scoped T` field. A
 * specialization is keyed by handle, so `t`'s handles are its
 * instantiation's own: an owner, a tuple holding one or a `scoped T`
 * container of one is released, and a borrow, an unmanaged value or a
 * first-class value is not (holdsOwner).
 */
export function emitReleaseOwned(cx: LoweringContext, value: i32, t: Type): void {
  if (!holdsOwner(t)) { return; }
  // A drop guards only a lone `X | null`, so a union of null and
  // several others, such as the slot of a `Result` that holds an owner,
  // skips null here.
  if (let UnionType {types} = t) {
    let rest = new Array<Type>();
    for (let m in types) {
      if (!(m is NullType)) { rest.push(m); }
    }
    if (rest.length > 1 && rest.length < types.length) {
      let isNull = cx.b.emit(IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1);
      let skip = cx.b.newBlock();
      let release = cx.b.newBlock();
      cx.b.brIf(isNull, skip, noValues(), release, noValues());
      cx.b.switchTo(release);
      emitDrop(cx, cx.stripNonNull(value), dropPlanOf(cx, cx.wasm.interner.union(rest)));
      cx.b.br(skip, noValues());
      cx.b.switchTo(skip);
      return;
    }
  }
  emitDrop(cx, value, dropPlanOf(cx, t));
}

/**
 * What a store into a `scoped T` field does with the value it
 * replaces, at this specialization (owner-futures.md, "Replacing a
 * value"). An owner is released, as a store into an owner field
 * releases one. A `Scoped<T>` must be consumed, so a store over one
 * calls `$scopedValueDropped`, which throws. Anything else, a borrow
 * or an unmanaged value included, is not the field's to release. `t`
 * is the field's type at this specialization; the store follows.
 */
export function emitScopedFieldReplace(cx: LoweringContext, t: Type, receiver: i32,
    fieldRef: i32, fieldVt: ValType): void {
  if (holdsOwner(t)) {
    let old = cx.b.emit(IrOp.struct_get, fieldVt, receiver, fieldRef);
    emitReleaseOwned(cx, old, t);
    return;
  }
  if (!typeContainsScoped(t)) { return; }
  let old = cx.b.emit(IrOp.struct_get, fieldVt, receiver, fieldRef);
  let isNull = cx.b.emit(IrOp.ref_is_null, new ValTypeI32() as ValType, old, -1);
  let empty = cx.b.newBlock();
  let full = cx.b.newBlock();
  cx.b.brIf(isNull, empty, noValues(), full, noValues());
  cx.b.switchTo(full);
  // Reachability roots it for every such store (noteScopedFieldStore).
  let dropped = cx.wasm.getStdlibFunc('core', '$scopedValueDropped');
  if (dropped == null) {
    cx.fail('scoped field store: $scopedValueDropped was not reached');
  }
  cx.b.callFunc(cx.b.addRef(new IrRefFunc(dropped)), noValues(), null);
  cx.b.unreachable();
  cx.b.switchTo(empty);
}

/** Releases `value` by its plan. */
export function emitDrop(cx: LoweringContext, value: i32, plan: DropPlan): void {
  let owners = plan.owners;
  if (owners != null) {
    if (plan.partial) {
      // A null, or a member that holds nothing, fails every test.
      emitDisposeDispatch(cx, value, owners, false);
    } else {
      emitMaybeDispose(cx, value, owners);
    }
    return;
  }
  emitTupleDrop(cx, value, plan.tuple as Type);
}

/** Whether `t` is a tuple, or a union of tuples, with an owner element. */
function tupleHoldsOwner(t: Type): boolean {
  let resolved = unwrapAliases(t);
  if (let UnionType {types} = resolved) {
    for (let member in types) {
      if (tupleHoldsOwner(member)) {
        return true;
      }
    }
    return false;
  }
  let elements = match (resolved) {
    case TupleType {elementTypes}: elementTypes
    case InlineTupleType {elementTypes}: elementTypes
    case _: null
  };
  if (elements == null) {
    return false;
  }
  for (let el in elements) {
    if (holdsOwner(el)) {
      return true;
    }
  }
  return false;
}

/**
 * Releases the owners a tuple value holds, last element first, as a
 * resource releases its fields in reverse declaration order. A union of
 * tuples branches on the literal at position 0 to the member the value
 * is; a nested tuple releases its own owners the same way.
 */
function emitTupleDrop(cx: LoweringContext, value: i32, t: Type): void {
  // A nullable tuple holds nothing when it is null.
  let core = soleNonNullMember(t);
  if (core != null) {
    let isNull = cx.b.emit(
        IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1);
    let skipB = cx.b.newBlock();
    let doB = cx.b.newBlock();
    cx.b.brIf(isNull, skipB, noValues(), doB, noValues());
    cx.b.switchTo(doB);
    emitTupleDrop(cx, value, core);
    cx.b.br(skipB, noValues());
    cx.b.switchTo(skipB);
    return;
  }
  let sourceOpt = unboxTuples(cx, value, t);
  if (sourceOpt == null) {
    bail(cx, 'drop of a tuple that is not tuples');
  }
  emitSlotsDropExcept(cx, sourceOpt, null, null);
}

/**
 * Releases the owners a tuple's slots hold: its boxed form read out, or
 * an inline result discarded as a statement.
 */
export function emitSlotsDrop(cx: LoweringContext, source: MultiValues): void {
  emitSlotsDropExcept(cx, source, null, null);
}

/**
 * `emitSlotsDrop` for the members `allowed` keeps (every member when
 * null), leaving out the owners at the paths `kept` holds or under them,
 * which something else releases: the owners a pattern bound to names.
 */
export function emitSlotsDropExcept(cx: LoweringContext, source: MultiValues,
    allowed: GrowableArray<boolean> | null, kept: HashSet<String> | null): void {
  let layout = source.layout;
  if (layout == null) {
    bail(cx, 'drop of a tuple with no slots');
  }
  let members = layout.members;
  // The members the value can be, and those of them with an owner to
  // release.
  var reaching = 0;
  let releasing = new Array<i32>();
  for (var m = 0; m < members.length; m += 1) {
    if (allowed != null && !allowed[m]) {
      continue;
    }
    reaching += 1;
    if (memberReleases(layout, m, kept)) {
      releasing.push(m);
    }
  }
  if (reaching == 1) {
    if (releasing.length == 1) {
      emitTupleMemberDrop(cx, source, releasing[0], kept);
    }
    return;
  }
  let tag = source.values[layout.slotOf(0, 0)];
  let i32Vt = new ValTypeI32() as ValType;
  let join = cx.b.newBlock();
  for (let m in releasing) {
    let cond = match (members[m].elementTypes[0]) {
      case BoolLiteralType {value}:
          if (value) tag else cx.b.emit(IrOp.i32_eqz, i32Vt, tag, -1)
      case NumLiteralType {raw}: {
        if (let (true, parsed) = parseI64OrU64Literal(raw)) {
          cx.b.binary(IrOp.i32_eq, i32Vt, tag, cx.b.constI32(parsed as i32, i32Vt))
        } else {
          bail(cx, 'unparseable tuple tag')
        }
      }
      case _: bail(cx, 'tuple tag type')
    };
    let hit = cx.b.newBlock();
    let next = cx.b.newBlock();
    cx.b.brIf(cond, hit, noValues(), next, noValues());
    cx.b.switchTo(hit);
    emitTupleMemberDrop(cx, source, m, kept);
    cx.b.br(join, noValues());
    cx.b.switchTo(next);
  }
  cx.b.br(join, noValues());
  cx.b.switchTo(join);
}

/** Whether member `m` has an owner outside `kept` to release. */
function memberReleases(layout: InlineLayout, m: i32,
    kept: HashSet<String> | null): boolean {
  let elements = layout.members[m].elementTypes;
  for (var p = 0; p < elements.length; p += 1) {
    if (holdsOwner(elements[p]) &&
        !isKeptPath(kept, layout.memberPaths[m][p])) {
      return true;
    }
  }
  return false;
}

/** Whether `path` is in `kept` or under a path in it. */
function isKeptPath(kept: HashSet<String> | null, path: String): boolean {
  if (kept == null) {
    return false;
  }
  for (let k in kept) {
    if (path == k || path.startsWith(`${k}.`)) {
      return true;
    }
  }
  return false;
}

/** Releases the owner elements of member `m`, last first, but those in `kept`. */
function emitTupleMemberDrop(cx: LoweringContext, source: MultiValues, m: i32,
    kept: HashSet<String> | null): void {
  let layout = source.layout as InlineLayout;
  let elements = layout.members[m].elementTypes;
  for (var p = elements.length - 1; p >= 0; p -= 1) {
    let el = elements[p];
    if (!holdsOwner(el) ||
        isKeptPath(kept, layout.memberPaths[m][p])) {
      continue;
    }
    let slot = layout.slotOf(m, p);
    if (slot < 0) {
      continue;
    }
    emitDrop(cx, source.values[slot], dropPlanOf(cx, el));
  }
}

/**
 * A `using` statement and everything after it in its block.
 *
 * The rest of the block is the region the binding is live over, so it is
 * lowered here rather than by the caller's loop: `using` is not a
 * statement that finishes, it is a statement that scopes what follows.
 * Nested `using`s recurse, which is what makes releases run in reverse
 * declaration order without a list to reverse.
 *
 * The release is a `finally`, and shares its lowering: one copy, in a
 * dispatch block OUTSIDE the region, reached by normal completion, the
 * handler edge, and every `return`/`break`/`continue` that leaves.
 * Putting the copy on the normal path INSIDE the region instead would
 * be wrong rather than merely fatter — a `dispose` that threw would
 * land in its own handler and release a second time. `dispose` is
 * specified not to throw (ownership.md §Disposable), but nothing
 * enforces that, and this shape does not depend on it.
 *
 * The value is acquired BEFORE the region opens: a release is owed only
 * once acquisition has succeeded.
 */
/**
 * A `var` owner binding of a statement list with the release intervals
 * the checker recorded for it: [start, end) pairs of statement indices,
 * end -1 standing for the list's end (ownership.md §Implicit drop).
 */
final class VarDropPlan {
  symId: SymbolId;
  drop: DropPlan;
  intervals: GrowableArray<i32>;
  new(this.symId, this.drop, this.intervals);
}

function varDropPlans(cx: LoweringContext, body: GrowableArray<Statement>): GrowableArray<VarDropPlan> {
  let plans = new Array<VarDropPlan>();
  for (let s in body) {
    if (!(s is VariableDeclaration)) { continue; }
    let pat = (s as VariableDeclaration).pattern as Node;
    let intervals = cx.func.model.ownDropIntervalsOf(pat);
    if (intervals == null || intervals.length == 0) { continue; }
    let symOpt = cx.func.model.resolveDeclaration(pat);
    if (symOpt == null) {
      bail(cx, 'var owner binding symbol');
    }
    let bt = cx.func.model.getBindingTypeById(symOpt.id);
    if (bt == null) {
      bail(cx, 'untyped var owner binding');
    }
    plans.push(new VarDropPlan(symOpt.id, dropPlanOf(cx, bt), intervals));
  }
  return plans;
}

/**
 * The recorded end of the plan's interval covering statement `i`: an
 * exclusive index, or -1 for the block's end; -2 when none covers it.
 */
function varDropIntervalEnd(plan: VarDropPlan, i: i32, len: i32): i32 {
  var k = 0;
  while (k + 1 < plan.intervals.length) {
    let a = plan.intervals[k];
    let raw = plan.intervals[k + 1];
    let b = if (raw < 0) len else raw;
    if (a <= i && i < b) { return raw; }
    k += 2;
  }
  return -2;
}

/**
 * Releases what a `var` owner binding holds at a region's exit. A store
 * inside the region mirrored the binding into the region's variable,
 * which the dispatch block read back into the environment before the
 * finalizer runs; a binding no statement in the region assigned still
 * holds its entry value.
 */
function emitVarDropRelease(cx: LoweringContext, plan: VarDropPlan): void {
  let syms = growable<SymbolId>([plan.symId]);
  emitDrop(cx, cx.envValues(syms)[0], plan.drop);
}

/**
 * One release region of a `var` owner binding, over statements
 * [start, end). A region reaching the block's end releases on every
 * exit, normal completion included; one closed earlier — before a
 * statement that moves the binding, or where an enclosing region ends
 * — releases only on the exits that leave early, since on normal
 * completion the value is still the binding's.
 *
 * With `acquire`, the region also covers the acquisition of `body[end]`,
 * a statement that scopes the rest of its block (lowerRangeAcquiring),
 * and returns what it acquired, or null when the region cannot complete.
 */
function lowerVarDropRegion(cx: LoweringContext, plan: VarDropPlan,
    body: GrowableArray<Statement>, start: i32, end: i32, toBlockEnd: boolean,
    acquire: boolean, scopeExit: Node | null = null): ScopeAcquisition | null {
  let covered = if (acquire) end + 1 else end;
  let mirrored = new Array<SymbolId>();
  for (var k = start; k < covered; k += 1) {
    cx.appendAssignedEnvSyms(body[k] as Node, mirrored);
  }
  // A region closed before the block's end leaves the block's later
  // statements in scope of what its own statements declared. The
  // protected part does not dominate the code after the region, so
  // those bindings leave it through variables set on normal completion,
  // the one way out that continues after it; so does an acquisition.
  let declared = new Array<Symbol>();
  if (!toBlockEnd) {
    for (var k = start; k < covered; k += 1) {
      let s = body[k];
      if (s is VariableDeclaration) {
        collectDeclaredSyms(cx, s.pattern as Node, declared);
      } else if (s is UsingStatement && s.pattern != null) {
        collectDeclaredSyms(cx, s.pattern as Node, declared);
      }
    }
  }
  let carried = new Array<Symbol>();
  // Per carried binding, its variable, or -1 for a zero-width binding,
  // whose environment entry holds no value and is restored as it was.
  let carriedVars = new Array<i32>();
  let carriedEntries = new Array<i32>();
  let acquiredVars = new Array<i32>();
  var acquiredPlans: GrowableArray<DropPlan> | null = null;
  cx.openVarDrops.add(plan.symId as i32);
  lowerFinallyRegion(cx, mirrored, true, null,
      (scope, resultVar, saved) => {
        let acquired = lowerRangeAcquiring(cx, body, start, end, acquire, scopeExit);
        if (cx.reachable) {
          for (let sym in declared) {
            if (let (true, value) = cx.env.get(sym.id)) {
              let vt = carriedValType(cx, sym);
              carried.push(sym);
              carriedEntries.push(value);
              if (vt is ValTypeVoid) {
                carriedVars.push(-1);
                continue;
              }
              let varId = cx.b.newVar(vt);
              cx.b.varSet(varId, cx.conformToSlot(value, vt, 'carried binding type'));
              carriedVars.push(varId);
            }
          }
          if (acquired != null) {
            for (let value in acquired.values) {
              let varId = cx.b.newVar(cx.valueTypeOf(value));
              cx.b.varSet(varId, value);
              acquiredVars.push(varId);
            }
            acquiredPlans = acquired.plans;
          }
        }
        finishProtectedArm(cx, scope, resultVar, -1);
      },
      (fscope) => { emitVarDropRelease(cx, plan); },
      toBlockEnd);
  cx.openVarDrops.delete(plan.symId as i32);
  if (!cx.reachable) {
    return null;
  }
  for (var c = 0; c < carried.length; c += 1) {
    let varId = carriedVars[c];
    cx.env[carried[c].id] = if (varId >= 0) cx.b.varGet(varId) else carriedEntries[c];
  }
  if (!acquire) {
    return null;
  }
  let plans = acquiredPlans;
  if (plans == null) {
    bail(cx, 'var owner region lost its acquisition');
  }
  let values = new Array<i32>();
  for (let varId in acquiredVars) {
    values.push(cx.b.varGet(varId));
  }
  return new ScopeAcquisition(values, plans);
}

/**
 * The type of the variable that carries `sym` out of a region: its
 * binding's, or its cell's when a closure captures it mutably.
 */
function carriedValType(cx: LoweringContext, sym: Symbol): ValType {
  let bindingType = cx.func.model.getBindingType(sym);
  if (bindingType == null) {
    bail(cx, 'untyped carried binding');
  }
  let vt = typeToValTypeWithSub(cx.wasm, bindingType, cx.func);
  if (cx.isCelled(sym.id)) {
    return new ValTypeRef(getCellStruct(cx.wasm, vt));
  }
  return vt;
}

/**
 * The bindings a declaration's pattern introduces, as `IdentifierPattern`s,
 * and those of any default inside it, which nothing after the
 * declaration reads; carrying one costs a variable and nothing else.
 */
function collectDeclaredSyms(cx: LoweringContext, pattern: Node, out: GrowableArray<Symbol>): void {
  walkEnter(pattern, (n) => {
    if (n is IdentifierPattern) {
      let sym = cx.func.model.resolveDeclaration(n);
      if (sym != null) {
        out.push(sym);
      }
    }
  });
}

/**
 * Statements [start, end) of a statement list, with the release regions
 * the checker recorded. A `using` or an implicit-drop `let` scopes the
 * rest of the range. A `var` owner binding's intervals each open a
 * region over the statements they cover — closed before a statement
 * that moves the binding, since between the move and a store that
 * revives it the binding holds a value whoever took it will release,
 * and reopened after one that revives it — whose finalizer releases
 * what the binding holds at the exit. An interval ending inside the
 * range closes there and lowering continues after it. The bindings
 * whose region is open are on the context, so a nested list (a
 * `using` region's rest) opens no second region for one.
 */
export function lowerStatementRange(cx: LoweringContext, body: GrowableArray<Statement>,
    start: i32, end: i32, scopeExit: Node | null = null): void {
  lowerRangeAcquiring(cx, body, start, end, false, scopeExit);
}

/**
 * `lowerStatementRange`, and when `acquire` is set, then the acquisition
 * of `body[end]`, a statement that scopes the rest of its block, whose
 * region the caller opens: what it acquired, or null when the range
 * cannot complete.
 *
 * Regions must nest, and a `var` owner's interval can close inside the
 * block, before the statement that moves the binding, while a region
 * opened inside the interval runs to the end of the range. So an
 * interval closing before the range's end closes after the acquisition
 * of the first such statement in it instead, and the rest of the range
 * lowers inside that statement's region, where the interval reopens.
 */
function lowerRangeAcquiring(cx: LoweringContext, body: GrowableArray<Statement>,
    start: i32, end: i32, acquire: boolean,
    scopeExit: Node | null = null): ScopeAcquisition | null {
  let plans = varDropPlans(cx, body);
  var remainingExit = scopeExit;
  var i = start;
  while (i < end) {
    if (!cx.reachable) { return null; }
    var openedEnd = -1;
    for (let plan in plans) {
      if (cx.openVarDrops.has(plan.symId as i32)) { continue; }
      let raw = varDropIntervalEnd(plan, i, body.length);
      if (raw == -2) { continue; }
      let b = if (raw < 0) body.length else raw;
      let regionEnd = if (b < end) b else end;
      let opener = if (regionEnd < end) firstScopeOpener(cx, body, i, regionEnd) else -1;
      if (opener >= 0) {
        let acquired = lowerVarDropRegion(cx, plan, body, i, opener, false, true);
        if (acquired == null) { return null; }
        lowerScopeDrops(cx, acquired.values, acquired.plans, 0, body, opener + 1, end,
            remainingExit);
        return null;
      }
      // An interval that covers the acquisition takes it in; one closing
      // at it, before a statement that moves the binding, does not.
      let acquiresHere = acquire && regionEnd == end && b > end;
      let atRangeEnd = !acquire && regionEnd == end;
      let regionExit = if (atRangeEnd) remainingExit else null;
      if (atRangeEnd) {
        remainingExit = null;
      }
      let acquired = lowerVarDropRegion(cx, plan, body, i, regionEnd,
          raw < 0 && regionEnd == b, acquiresHere, regionExit);
      if (acquiresHere) { return acquired; }
      openedEnd = regionEnd;
      break;
    }
    if (openedEnd >= 0) {
      i = openedEnd;
      continue;
    }
    let s = body[i];
    if (opensScope(cx, s)) {
      // A `using`, or an implicit-drop binding, which scopes the rest of
      // its block the way `using` does (ownership.md §Implicit drop).
      let acquired = acquireScope(cx, s);
      lowerScopeDrops(cx, acquired.values, acquired.plans, 0, body, i + 1, end,
          remainingExit);
      return null;
    }
    lowerStmt(cx, s);
    i += 1;
  }
  if (!acquire) {
    if (remainingExit != null) {
      emitNodeExitDrops(cx, remainingExit);
    }
    return null;
  }
  if (!cx.reachable) { return null; }
  return acquireScope(cx, body[end]);
}

/**
 * The first statement of `body[start..end)` that scopes the rest of its
 * block, or -1.
 */
function firstScopeOpener(cx: LoweringContext, body: GrowableArray<Statement>,
    start: i32, end: i32): i32 {
  for (var k = start; k < end; k += 1) {
    if (opensScope(cx, body[k])) { return k; }
  }
  return -1;
}

/** Whether a statement scopes the rest of its block: a `using`, or an implicit-drop binding. */
function opensScope(cx: LoweringContext, s: Statement): boolean {
  return s is UsingStatement ||
      (s is VariableDeclaration && declScopeDrop(cx, s));
}

/** What a statement that scopes the rest of its block acquired, and how each value is released. */
final class ScopeAcquisition {
  values: GrowableArray<i32>;
  plans: GrowableArray<DropPlan>;
  new(this.values, this.plans);
}

/**
 * Lowers the acquisition of a statement `opensScope` accepts — a
 * `using`'s value, or an implicit-drop declaration — without opening the
 * region it is released over: a release is owed only once acquisition
 * has succeeded.
 */
function acquireScope(cx: LoweringContext, s: Statement): ScopeAcquisition {
  if (s is UsingStatement) {
    let declType = cx.substitutedNodeType(s.init);
    if (declType == null) {
      bail(cx, 'untyped using initializer');
    }
    let plan = new DropPlan(disposeOwnerClasses(cx, declType), null);
    let value = lowerExpr(cx, s.init, null);
    let pattern = s.pattern;
    if (pattern != null) {
      let pat = pattern as Node;
      if (!(pat is IdentifierPattern)) {
        bail(cx, 'using destructuring pattern');
      }
      let symOpt = cx.func.model.resolveDeclaration(pat);
      if (symOpt == null) {
        bail(cx, 'using binding symbol');
      }
      cx.bindPatternSym(symOpt, value);
    }
    return new ScopeAcquisition(growable<i32>([value]), growable<DropPlan>([plan]));
  }
  let vd = s as VariableDeclaration;
  let drops = scopeDropsOf(cx, vd);
  varDecl(cx, vd);
  return new ScopeAcquisition(cx.envValues(drops.syms), drops.plans);
}

export function lowerUsingRegion(cx: LoweringContext, stmt: UsingStatement,
    rest: GrowableArray<Statement>, restStart: i32, restEnd: i32): void {
  let acquired = acquireScope(cx, stmt);
  lowerScopeDrops(cx, acquired.values, acquired.plans, 0, rest, restStart, restEnd);
}

/** Whether the checker marked this declaration for release at scope exit. */
export function declScopeDrop(cx: LoweringContext, vd: VariableDeclaration): boolean {
  let pattern = vd.pattern as Node;
  if (pattern is IdentifierPattern) {
    return cx.func.model.hasScopeExitDrop(pattern);
  }
  return patternHasDrop(cx, pattern);
}

/** Whether some owner binding of a destructuring pattern is released at scope exit. */
function patternHasDrop(cx: LoweringContext, node: Node): boolean {
  return match (node) {
    case BindingPattern {pattern as inner}: patternHasDrop(cx, inner as Node)
    case TuplePattern {elements}: elements.some((element) =>
        element != null && patternHasDrop(cx, element))
    case RecordPattern {properties}: properties.some((property) =>
        patternHasDrop(cx, property.value))
    case ClassPattern {properties}: properties.some((property) =>
        patternHasDrop(cx, property.value))
    case _: (node is Identifier || node is IdentifierPattern) &&
        cx.func.model.hasScopeExitDrop(node)
  };
}

/**
 * The dispose glue's field list: the affine fields the lowered
 * function's class declares itself, in declaration order, with their
 * (instantiated) member types. Empty for anything but a `:dispose` of
 * a class with such fields.
 */
export function collectAffineDropFields(cx: LoweringContext,
    names: GrowableArray<String>, types: GrowableArray<Type>): void {
  let ctOpt = cx.func.classType;
  if (ctOpt == null) { return; }
  let ct = ctOpt;
  let declOpt = cx.func.classDecl;
  if (declOpt == null || !(declOpt is ClassDeclaration)) { return; }
  // A field the dispose body moved out is the caller's (or a sink's)
  // to release; the glue skips it. The checker verified the move
  // happens on every path, which is what keeps this skip static.
  var movedOut: GrowableArray<String> | null = null;
  let nodeOpt = cx.func.node;
  if (nodeOpt != null) {
    movedOut = cx.func.model.movedOutFieldsOf(nodeOpt);
  }
  for (let member in declOpt.body) {
    if (!(member is FieldDefinition)) { continue; }
    if (member.isStatic || member.isAbstract || member.isDeclare) { continue; }
    let nameNode = member.name;
    if (!(nameNode is Identifier)) { continue; }
    let fName = nameNode.name;
    if (!ct.members.has(fName)) { continue; }
    let fType = ct.members[fName].type;
    let core = soleNonNullMember(fType) ?? fType;
    if (ownershipHandleOf(core) != OwnershipHandle.Own) { continue; }
    if (movedOut != null) {
      var skipped = false;
      for (let m in movedOut) {
        if (m == fName) { skipped = true; }
      }
      if (skipped) { continue; }
    }
    names.push(fName);
    types.push(fType);
  }
}

/**
 * Stores `ResourceState.Dropped` into the receiver's lifecycle flag —
 * the `#state` field `Resource` declares, found under the root class's
 * private-field key. Emitted at the top of every resource
 * `[Disposable.dispose]`, written or synthesized, so ending the
 * object's life is a property of
 * the consuming dispose itself rather than of any one release site's
 * glue: implicit drop, `using`, field glue and an explicit consuming
 * call all mark it, and a later `adopt` reports "dropped" instead of
 * blaming a phantom owner. `Dropped == 3` is pinned by zena:ownership's
 * `ResourceState` (as `Owned == 0` is pinned by `struct.new_default`).
 */
export function emitMarkDropped(cx: LoweringContext): void {
  let ctOpt = cx.func.classType;
  if (ctOpt == null) {
    bail(cx, 'dispose prologue without class');
  }
  var root = ctOpt as ClassType;
  while (root.superType != null) {
    root = root.superType as ClassType;
  }
  let info = if (let (true, ci) = cx.wasm.classInfos.get(getClassKey(ctOpt))) ci else null;
  if (info == null) {
    bail(cx, 'dispose prologue class not discovered');
  }
  let st = info.structType;
  let key = getClassPrivateFieldKey(getClassKey(root), '#state');
  if (st.fieldIndexByName(key) < 0) {
    bail(cx, 'resource lifecycle field missing');
  }
  let dropped = cx.b.constI32(3, new ValTypeI32() as ValType);
  let pair = growable<i32>([cx.thisValue, dropped]);
  let fieldRef = cx.b.addRef(new IrRefField(st, key));
  let pairIdx = cx.b.pushExtra(pair);
  cx.b.emitVoid(IrOp.struct_set, pairIdx, fieldRef);
}

/**
 * A release member the checker synthesized (ClassType.hasSyntheticRelease):
 * a resource class's `:dispose`, or a `scoped T` class's `[release]`.
 */
export function lowerSyntheticRelease(cx: LoweringContext): IrBody {
  let ctOpt = cx.func.classType;
  if (ctOpt == null) {
    bail(cx, 'synthetic release without class');
  }
  if (ctOpt.isResource) {
    return lowerSyntheticDispose(cx);
  }
  return lowerScopedRelease(cx, ctOpt);
}

/**
 * The synthesized `[release]` of a `scoped T` class: what each field
 * holding `T` owns is released as `releaseOwned` would release it
 * (emitReleaseOwned), last field first. Only the drop of an instance
 * at an owner calls it.
 */
function lowerScopedRelease(cx: LoweringContext, ct: ClassType): IrBody {
  let sig = cx.func.signature;
  if (sig.params.length != 1) {
    bail(cx, 'synthetic release shape');
  }
  let entry = cx.b.newBlock();
  let thisParam = cx.b.blockParam(entry, sig.params[0]);
  cx.b.switchTo(entry);
  cx.thisValue = thisParam;

  let cKey = getClassKey(ct);
  let info = if (let (true, ci) = cx.wasm.classInfos.get(cKey)) ci else null;
  if (info == null) {
    bail(cx, 'synthetic release class not discovered');
  }
  let st = info.structType;
  // A release reached through a vtable slot takes the receiver at the
  // slot's erased type, as an override does, so it is cast here.
  if (!(sig.params[0] is ValTypeRef)) {
    let castVt = new ValTypeRef(st) as ValType;
    cx.thisValue = cx.b.emit(IrOp.ref_cast, castVt, thisParam, cx.b.internType(castVt));
  }
  let names = scopedFieldNames(ct);
  let types = scopedFieldTypesAt(cx.wasm, ct);
  for (var i = names.length - 1; i >= 0; i -= 1) {
    // A `void` field has no slot, and holds nothing.
    if (isVoidOrNeverType(types[i])) {
      continue;
    }
    let slot = fieldSlotName(ct, cKey, names[i]);
    let fieldIdx = st.fieldIndexByName(slot);
    if (fieldIdx < 0) {
      bail(cx, 'synthetic release field missing');
    }
    let fieldRef = cx.b.addRef(new IrRefField(st, slot));
    let value = cx.b.emit(IrOp.struct_get, st.fields[fieldIdx].type, cx.thisValue, fieldRef);
    emitReleaseOwned(cx, value, types[i]);
  }
  cx.b.retVoid();
  return cx.b.finish();
}

/**
 * The struct slot of `ct`'s field `name`: through a renamed setter's
 * backing field, and keyed by the class when it is private.
 */
function fieldSlotName(ct: ClassType, cKey: ClassKey, name: String): String {
  var slot = name;
  if (ct.fieldSetterNames != null) {
    let mapping = ct.fieldSetterNames as HashMap<String, String>;
    if (let (true, mapped) = mapping.get(slot)) {
      slot = mapped;
    }
  }
  if (slot.length > 0 && slot.sliceBytes(0, 1) == "#") {
    slot = getClassPrivateFieldKey(cKey, slot);
  }
  return slot;
}

/**
 * The synthesized `:dispose` of a resource class whose only release
 * action is its owner fields: no written body, just the field release
 * regions around nothing (ownership.md §Affine fields). The signature
 * is the consuming receiver alone.
 */
export function lowerSyntheticDispose(cx: LoweringContext): IrBody {
  let sig = cx.func.signature;
  if (sig.params.length != 1) {
    bail(cx, 'synthetic dispose shape');
  }
  let entry = cx.b.newBlock();
  let thisParam = cx.b.blockParam(entry, sig.params[0]);
  cx.b.switchTo(entry);
  cx.thisValue = thisParam;
  emitMarkDropped(cx);

  let names = new Array<String>();
  let types = new Array<Type>();
  collectAffineDropFields(cx, names, types);
  if (names.length == 0) {
    bail(cx, 'synthetic dispose without owner fields');
  }
  let emptyBody = new Array<Statement>();
  let emptySyms = new Array<Symbol>();
  lowerFieldDropRegions(cx, names, types, 0, emptySyms, emptyBody, 0, 0);
  if (cx.reachable) {
    cx.b.retVoid();
  }
  return cx.b.finish();
}

/**
 * Release regions for a resource's affine fields, wrapped around its
 * `:dispose` body: the user's release action runs inside, and each
 * field releases after it, in reverse declaration order — first field
 * outermost, exactly like parameters (ownership.md §Affine fields).
 * Past the last field, owned parameters take their regions, then the
 * statements. The finalizer reads the field when it runs: a `var`
 * field may have been taken (left null) or revived by the body.
 */
export function lowerFieldDropRegions(cx: LoweringContext,
    names: GrowableArray<String>, types: Array<Type>, idx: i32,
    paramSyms: GrowableArray<Symbol>,
    body: GrowableArray<Statement>, start: i32, end: i32,
    scopeExit: Node | null = null): void {
  if (idx >= names.length) {
    lowerParamDropRegions(cx, paramSyms, 0, body, start, end, scopeExit);
    return;
  }
  let ctOpt = cx.func.classType;
  if (ctOpt == null) {
    bail(cx, 'field drop without class');
  }
  let ct = ctOpt;
  let cKey = getClassKey(ct);
  let info = if (let (true, ci) = cx.wasm.classInfos.get(cKey)) ci else null;
  if (info == null) {
    bail(cx, 'field drop class not discovered');
  }
  let st = info.structType;
  let lookupName = fieldSlotName(ct, cKey, names[idx]);
  let fieldIdx = st.fieldIndexByName(lookupName);
  if (fieldIdx < 0) {
    bail(cx, 'field drop slot missing');
  }
  let fieldRef = cx.b.addRef(new IrRefField(st, lookupName));
  let fieldVt = st.fields[fieldIdx].type;
  let owners = disposeOwnerClasses(cx, types[idx]);

  let mirrored = new Array<SymbolId>();
  for (var k = start; k < end; k += 1) {
    cx.appendAssignedEnvSyms(body[k] as Node, mirrored);
  }
  lowerFinallyRegion(cx, mirrored, true, null,
      (scope, resultVar, saved) => {
        lowerFieldDropRegions(cx, names, types, idx + 1, paramSyms,
            body, start, end, scopeExit);
        finishProtectedArm(cx, scope, resultVar, -1);
      },
      () => {
        let value = cx.b.emit(IrOp.struct_get, fieldVt, cx.thisValue, fieldRef);
        emitMaybeDispose(cx, value, owners);
      });
}

/**
 * Release regions for owned parameters the body never moves: the
 * function received ownership, so the function releases at exit —
 * through the same shared finally region every other release uses.
 * Nested first-parameter-outermost, so release order is the reverse of
 * declaration order, matching how sibling bindings release. Past the
 * last parameter, the body statements lower with the same
 * using/scope-drop dispatch #lowerStatementList applies.
 */
export function lowerParamDropRegions(cx: LoweringContext,
    dropSyms: GrowableArray<Symbol>, idx: i32,
    body: GrowableArray<Statement>, start: i32, end: i32,
    scopeExit: Node | null = null): void {
  if (idx >= dropSyms.length) {
    lowerStatementRange(cx, body, start, end, scopeExit);
    return;
  }
  let declType = cx.bindingTypeOf(dropSyms[idx].id);
  if (declType == null) {
    bail(cx, 'param drop untyped binding');
  }
  let plan = dropPlanOf(cx, declType);
  let syms = growable<SymbolId>([dropSyms[idx].id]);
  let value = cx.envValues(syms)[0];

  let mirrored = new Array<SymbolId>();
  for (var k = start; k < end; k += 1) {
    cx.appendAssignedEnvSyms(body[k] as Node, mirrored);
  }

  lowerFinallyRegion(cx, mirrored, true, null,
      (scope, resultVar, saved) => {
        lowerParamDropRegions(cx, dropSyms, idx + 1, body, start, end, scopeExit);
        finishProtectedArm(cx, scope, resultVar, -1);
      },
      (fscope) => { emitDrop(cx, value, plan); });
}

/**
 * The rest of a block behind one release region per dropped binding,
 * the first binding's outermost, so the bindings release in reverse
 * order.
 *
 * An implicit-drop declaration is `using` without the keyword. The
 * checker marked the binding as an owned resource nothing moves,
 * reassigns, captures or escapes (ownership.md §Implicit drop), so the
 * rest of the block is the region the value is live over, and the
 * release is the same shared finally region `using` compiles to — one
 * dispose, outside the region, reached by normal completion, the
 * handler edge, and every early exit.
 */
function lowerScopeDrops(cx: LoweringContext, values: GrowableArray<i32>,
    plans: GrowableArray<DropPlan>, idx: i32,
    rest: GrowableArray<Statement>, restStart: i32, restEnd: i32,
    scopeExit: Node | null = null): void {
  if (idx >= values.length) {
    lowerStatementRange(cx, rest, restStart, restEnd, scopeExit);
    return;
  }
  let mirrored = new Array<SymbolId>();
  for (var k = restStart; k < restEnd; k += 1) {
    cx.appendAssignedEnvSyms(rest[k] as Node, mirrored);
  }
  let value = values[idx];
  let plan = plans[idx];
  lowerFinallyRegion(cx, mirrored, true, null,
      (scope, resultVar, saved) => {
        lowerScopeDrops(cx, values, plans, idx + 1, rest, restStart, restEnd, scopeExit);
        finishProtectedArm(cx, scope, resultVar, -1);
      },
      (fscope) => { emitDrop(cx, value, plan); });
}

/** The bindings a declaration releases at its block's exit, and how. */
final class ScopeDrops {
  syms: GrowableArray<SymbolId>;
  plans: GrowableArray<DropPlan>;
  new(this.syms, this.plans);
}

/**
 * What a declaration releases at its block's exit: its binding (typed by
 * the initializer), or the owner bindings of a destructuring pattern
 * (#495), each marked by the checker.
 */
function scopeDropsOf(cx: LoweringContext, vd: VariableDeclaration): ScopeDrops {
  let syms = new Array<SymbolId>();
  let plans = new Array<DropPlan>();
  let pattern = vd.pattern as Node;
  if (!(pattern is IdentifierPattern)) {
    collectPatternDrops(cx, pattern, syms, plans);
  } else if (cx.func.model.hasScopeExitDrop(pattern)) {
    let declType = cx.substitutedNodeType(vd.init);
    if (declType == null) {
      bail(cx, 'untyped implicit-drop binding');
    }
    let symOpt = cx.func.model.resolveDeclaration(pattern);
    if (symOpt == null) {
      bail(cx, 'implicit-drop binding symbol');
    }
    syms.push(symOpt.id);
    // A tuple binding holds the boxed form even when its initializer is
    // an inline result, and the glue reads the boxed value.
    let bindingType = cx.func.model.getBindingTypeById(symOpt.id);
    plans.push(if (bindingType != null && tupleHoldsOwner(bindingType)) {
      dropPlanOf(cx, bindingType)
    } else {
      dropPlanOf(cx, declType)
    });
  }
  return new ScopeDrops(syms, plans);
}

export function lowerWhile(cx: LoweringContext, stmt: WhileStatement): void {
  if (!(stmt.test is Expression) && !(stmt.test is LetPatternCondition)) {
    bail(cx, 'while-let');
  }
  let carried = cx.assignedEnvSyms(stmt.body);
  cx.appendAssignedEnvSyms(stmt.test, carried);

  let header = cx.b.newBlock();
  let headerParams = cx.addParams(header, carried);
  cx.b.br(header, cx.envValues(carried));

  cx.b.switchTo(header);
  cx.applyParams(carried, headerParams);
  // The loop's invariants hold each time it is about to test.
  lowerContractClauses(cx, stmt.invariants);
  // while-let re-evaluates the pattern source and rebinds its
  // elements every iteration, exactly like the expression form
  // re-evaluates the condition.
  var cond = -1;
  var letBinds: LetCondBinds | null = null;
  if (stmt.test is LetPatternCondition) {
    let binds = lowerLetCondition(cx, stmt.test as LetPatternCondition);
    letBinds = binds;
    cond = binds.cond;
  } else {
    cond = lowerExpr(cx, stmt.test as Expression, null);
  }

  let bodyB = cx.b.newBlock();
  let exitB = cx.b.newBlock();
  let exitParams = cx.addParams(exitB, carried);
  // Branch on the exit test: the emitter writes a branch to the loop's
  // exit as a bare br_if, and the simplifier folds this eqz into a
  // comparison condition (`i < n` exits on `i >= n`), so the loop
  // tests once instead of testing and negating.
  let exitTest = cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), cond, -1);
  if (letBindsOwnSource(letBinds)) {
    // The exit edge releases the source the pattern did not match.
    let releaseB = cx.b.newBlock();
    cx.b.brIf(exitTest, releaseB, noValues(), bodyB, noValues());
    cx.b.switchTo(releaseB);
    emitNotTakenOwnerDrops(cx, letBinds as LetCondBinds);
    cx.b.br(exitB, cx.envValues(carried));
  } else {
    cx.b.brIf(exitTest, exitB, cx.envValues(carried), bodyB, noValues());
  }

  cx.loops.push(new LoopCtx(header, exitB, carried));
  cx.b.switchTo(bodyB);
  if (letBinds != null) {
    let needsFail = letBindsNeedFail(cx, letBinds);
    var failB = exitB;
    if (needsFail) {
      // The exit takes carried args; tests branch argless — forward
      // through a trampoline.
      failB = cx.b.newBlock();
    }
    applyLetBinds(cx, letBinds, failB);
    if (needsFail) {
      let resumeB = cx.b.newBlock();
      cx.b.br(resumeB, noValues());
      cx.b.switchTo(failB);
      emitNotTakenOwnerDrops(cx, letBinds);
      cx.b.br(exitB, cx.envValues(carried));
      cx.b.switchTo(resumeB);
    }
    emitTakenOwnerDrops(cx, letBinds);
  }
  lowerBranchStmt(cx, letPatternOf(stmt.test), stmt.body);
  if (cx.reachable) {
    cx.b.br(header, cx.envValues(carried));
  }
  cx.loops.pop();

  cx.b.switchTo(exitB);
  cx.applyParams(carried, exitParams);
  cx.reachable = true;
}

/**
 * The variable a `for` header declares when it lives in a heap cell, or
 * null. Such a variable is a fresh variable on each iteration (see
 * lowerFor), so the loop carries its cell rather than its value.
 */
function celledHeaderSym(cx: LoweringContext, init: VariableDeclaration): Symbol | null {
  let pat = init.pattern;
  if (!(pat is IdentifierPattern)) { return null; }
  let sym = cx.func.model.resolveDeclaration(pat);
  if (sym == null || !cx.isCelled(sym.id)) { return null; }
  return sym;
}

export function lowerFor(cx: LoweringContext, stmt: ForStatement): void {
  // C-style for. The init runs in the current block; the update gets its
  // own block so `continue` can route through it.
  var headerCell: Symbol | null = null;
  if (stmt.init != null) {
    let init = stmt.init as Node;
    if (init is VariableDeclaration) {
      varDecl(cx, init);
      headerCell = celledHeaderSym(cx, init);
    } else if (init is ExpressionStatement) {
      lowerExpr(cx, init.expression, null);
    } else if (init is Expression) {
      lowerExpr(cx, init, null);
    } else {
      bail(cx, 'for-init kind');
    }
  }

  let carried = new Array<SymbolId>();
  let test = stmt.test;
  if (test != null) {
    cx.appendAssignedEnvSyms(test, carried);
  }
  let update = stmt.update;
  if (update != null) {
    cx.appendAssignedEnvSyms(update, carried);
  }
  cx.appendAssignedEnvSyms(stmt.body, carried);
  if (headerCell != null) {
    carried.push(headerCell.id);
  }

  let header = cx.b.newBlock();
  let headerParams = cx.addParams(header, carried);
  cx.b.br(header, cx.envValues(carried));

  cx.b.switchTo(header);
  cx.applyParams(carried, headerParams);
  lowerContractClauses(cx, stmt.invariants);
  let cond = if (stmt.test != null) {
    lowerExpr(cx, stmt.test as Expression, null)
  } else {
    cx.b.constI32(1, new ValTypeI32())
  };

  let bodyB = cx.b.newBlock();
  let updateB = cx.b.newBlock();
  let updateParams = cx.addParams(updateB, carried);
  let exitB = cx.b.newBlock();
  let exitParams = cx.addParams(exitB, carried);
  // Branch on the exit test: the emitter writes a branch to the loop's
  // exit as a bare br_if, and the simplifier folds this eqz into a
  // comparison condition (`i < n` exits on `i >= n`), so the loop
  // tests once instead of testing and negating.
  let exitTest = cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), cond, -1);
  cx.b.brIf(exitTest, exitB, cx.envValues(carried), bodyB, noValues());

  cx.loops.push(new LoopCtx(updateB, exitB, carried));
  cx.b.switchTo(bodyB);
  lowerStmt(cx, stmt.body);
  if (cx.reachable) {
    cx.b.br(updateB, cx.envValues(carried));
  }
  cx.loops.pop();

  cx.b.switchTo(updateB);
  cx.reachable = true;
  cx.applyParams(carried, updateParams);
  if (headerCell != null) {
    // A closure made during this iteration keeps this iteration's
    // variable. The next iteration gets a fresh cell that starts from
    // the value this one ended with, and the update runs on it — the
    // per-iteration binding of a JavaScript `for (let …)`.
    let symId = headerCell.id;
    let cell = cx.env[symId];
    cx.env[symId] = cx.cellAlloc(cx.bindingValType(symId), cx.cellRead(cell));
  }
  if (stmt.update != null) {
    lowerExpr(cx, stmt.update as Expression, null);
  }
  cx.b.br(header, cx.envValues(carried));

  cx.b.switchTo(exitB);
  cx.applyParams(carried, exitParams);
  cx.reachable = true;
}

/**
 * `left ?? right`: the left evaluates once; null short-circuits to
 * the right operand. When the left is an optional member access,
 * the chain fuses: a null object jumps straight to the right
 * operand — the SSA form of the streaming backend's short-circuit
 * flag — which also covers primitive members (the miss produces no
 * value that needs representing).
 */
/** The two-arm bool-discriminated inline-tuple union (`Iterator.next`
 * / `Map.get` / Result shapes) that `??` may coalesce. */
function isProtocolUnion(t: Type): boolean {
  if (!(t is UnionType)) { return false; }
  let types = t.types;
  if (types.length != 2) { return false; }
  var sawTrue = false;
  var sawFalse = false;
  for (let member in types) {
    if (!(member is InlineTupleType)) { return false; }
    if (member.elementTypes.length < 1) { return false; }
    let head = member.elementTypes[0];
    if (!(head is BoolLiteralType)) { return false; }
    if (head.value) {
      if (member.elementTypes.length < 2) { return false; }
      sawTrue = true;
    } else {
      sawFalse = true;
    }
  }
  return sawTrue && sawFalse;
}

/** The shared merge tail of the coalesce lowerings. The caller has
 * branched, saved the env, switched to the ok block and computed
 * `okValue` there; the right arm evaluates lazily; both meet at one
 * merge. */
function coalesceMerge(cx: LoweringContext, bin: BinaryExpression,
    overallVt: ValType, mergeSyms: GrowableArray<SymbolId>,
    rightB: i32, okValue: i32, saved: HashMap<SymbolId, i32>) {
  // The ok arm comes out of the left operand's maybe-form — a tuple
  // slot or an optional record field — so it is not an expression the
  // post-expression adaptation hook can reach. The checker records the
  // pack it needs on the `??` node; apply it to the arm here. The
  // right arm is an expression and carries its own.
  var ok = coalesceArmAdaptation(cx, bin, okValue);
  if (!isValSubtype(cx.valueTypeOf(ok), overallVt)) {
    ok = cx.coerceTo(ok, overallVt);
  }
  let mergeB = cx.b.newBlock();
  let mergeParams = cx.addParams(mergeB, mergeSyms);
  let resultParam = cx.b.blockParam(mergeB, overallVt);
  let okArgs = cx.envValues(mergeSyms);
  okArgs.push(ok);
  cx.b.br(mergeB, okArgs);

  cx.env = cx.copyOf(saved);
  cx.reachable = true;
  cx.b.switchTo(rightB);
  var rightValue = lowerExpr(cx, bin.right, overallVt);
  if (cx.reachable) {
    if (!isValSubtype(cx.valueTypeOf(rightValue), overallVt)) {
      rightValue = cx.coerceTo(rightValue, overallVt);
    }
    let rightArgs = cx.envValues(mergeSyms);
    rightArgs.push(rightValue);
    cx.b.br(mergeB, rightArgs);
  }

  cx.env = saved;
  cx.b.switchTo(mergeB);
  cx.applyParams(mergeSyms, mergeParams);
  cx.reachable = true;
  return resultParam;
}

/** `opts.f ?? d` on an OPTIONAL record field: the presence-tested
 * read — the slot value when the bit is set, the default otherwise. */
function lowerPresenceCoalesce(cx: LoweringContext, bin: BinaryExpression,
    member: MemberExpression, rec: RecordType) {
  let overallVt = cx.exprVt(bin);
  let mergeSyms = cx.assignedEnvSyms(bin.right);
  let name = member.property.name;
  let recv = lowerExpr(cx, member.object, null);
  let dispatchOpt = recordDispatchOfValue(cx, recv);
  if (dispatchOpt == null) {
    bail(cx, 'presence coalesce operand');
  }
  let dispatch = dispatchOpt as RecordDispatchInfo;
  let bit = dispatch.optionalBitOf(name);
  if (bit < 0) {
    bail(cx, 'presence coalesce bit');
  }
  let present = maskBitTest(cx, dispatch, bit, recv);
  let rightB = cx.b.newBlock();
  let okB = cx.b.newBlock();
  cx.b.brIf(present, okB, noValues(), rightB, noValues());

  let saved = cx.copyEnv();
  cx.b.switchTo(okB);
  var okValue = cx.recordDispatchRead(dispatch, name, recv);
  let okVt = cx.valueTypeOf(okValue);
  if (okVt is ValTypeRefNull && !propTypeAdmitsNull(rec, name)) {
    okValue = cx.reassertNonNull(okValue,
        new ValTypeRef((okVt as ValTypeRefNull).target) as ValType);
  }
  return coalesceMerge(cx, bin, overallVt, mergeSyms, rightB, okValue,
      saved);
}

/** A protocol inline-tuple union under `??`: the true arm's payload
 * when the flag is set, the default otherwise (a Result-shaped
 * union's error slot plays no part). */
function lowerProtocolCoalesce(cx: LoweringContext, bin: BinaryExpression) {
  let overallVt = cx.exprVt(bin);
  let mergeSyms = cx.assignedEnvSyms(bin.right);
  let source = multiSource(cx, bin.left);
  // The payload is the `true` member's.
  let layout = source.layout;
  let allowed = if (layout != null) {
    let keep = new Array<boolean>();
    for (let member in layout.members) {
      let head = member.elementTypes[0];
      keep.push(head is BoolLiteralType && head.value);
    }
    keep
  } else {
    null
  };
  let values = source.positions(cx, allowed);
  if (values.length < 2) {
    bail(cx, 'protocol coalesce arity');
  }
  let flag = values[0];
  if (!(cx.valueTypeOf(flag) is ValTypeI32)) {
    bail(cx, 'protocol coalesce flag');
  }
  let rightB = cx.b.newBlock();
  let okB = cx.b.newBlock();
  cx.b.brIf(flag, okB, noValues(), rightB, noValues());
  let saved = cx.copyEnv();
  cx.b.switchTo(okB);
  return coalesceMerge(cx, bin, overallVt, mergeSyms, rightB,
      values[1], saved);
}

export function lowerNullishCoalesce(cx: LoweringContext, bin: BinaryExpression): i32 {
  // Optional record field: `opts.f ?? d` is the licensed guarded read.
  if (bin.left is MemberExpression) {
    let member = bin.left as MemberExpression;
    if (!member.optional && !member.isSymbolAccess) {
      let objT = cx.substitutedNodeType(member.object);
      if (objT != null) {
        let nn = unwrapAliases(getNonNullableType(objT));
        if (nn is RecordType && nn.isOptional(member.property.name)) {
          return lowerPresenceCoalesce(cx, bin, member, nn);
        }
      }
    }
  }
  // Protocol inline-tuple union: payload-or-default.
  let leftSem = cx.substitutedNodeType(bin.left);
  if (leftSem != null && isProtocolUnion(leftSem)) {
    return lowerProtocolCoalesce(cx, bin);
  }

  let overallVt = cx.exprVt(bin);
  let mergeSyms = cx.assignedEnvSyms(bin.right);

  // Fuse `chain ?? default` only when the chain produces a non-reference
  // (primitive) value that cannot be represented as `T | null`. When the
  // chain produces a reference, lowering `bin.left` normally yields a
  // nullable reference and the null check below covers both a null
  // receiver and a null member/result (#1031).
  if (hasOptionalChainStep(bin.left) && !isRefValType(cx.exprVt(bin.left))) {
    return lowerOptionalChainCoalesce(cx, bin, overallVt);
  }
  let testValue = lowerExpr(cx, bin.left, null);
  if (!cx.reachable) {
    return -1;
  }
  let testVt = cx.valueTypeOf(testValue);
  if (!isNullableAnyRef(testVt) && !(testVt is ValTypeExternRef)) {
    return cx.conformToSlot(
        coalesceArmAdaptation(cx, bin, testValue), overallVt, 'nullish arm type');
  }

  let isNull = cx.b.emit(
      IrOp.ref_is_null, new ValTypeI32() as ValType, testValue, -1);
  let rightB = cx.b.newBlock();
  let okB = cx.b.newBlock();
  cx.b.brIf(isNull, rightB, noValues(), okB, noValues());

  let saved = cx.copyEnv();
  cx.b.switchTo(okB);
  let okValue = cx.conformToSlot(
      coalesceArmAdaptation(cx, bin, cx.stripNonNull(testValue)),
      overallVt, 'nullish arm type');
  let mergeB = cx.b.newBlock();
  let mergeParams = cx.addParams(mergeB, mergeSyms);
  let resultParam = cx.b.blockParam(mergeB, overallVt);
  let okArgs = cx.envValues(mergeSyms);
  okArgs.push(okValue);
  cx.b.br(mergeB, okArgs);

  cx.env = cx.copyOf(saved);
  cx.reachable = true;
  cx.b.switchTo(rightB);
  let rightValue = lowerExpr(cx, bin.right, overallVt);
  if (cx.reachable) {
    let conformedRight = cx.conformToSlot(rightValue, overallVt, 'nullish arm type');
    let rightArgs = cx.envValues(mergeSyms);
    rightArgs.push(conformedRight);
    cx.b.br(mergeB, rightArgs);
  }

  cx.env = saved;
  cx.b.switchTo(mergeB);
  cx.applyParams(mergeSyms, mergeParams);
  cx.reachable = true;
  return resultParam;
}

/**
 * for-in via the iterator protocol, mirroring streaming: class
 * iterables pack into their Iterable (or Iterator) fat pointer here
 * — skipping any checker-recorded top-level adaptation, like the
 * array fast paths — and `:iterator` is dispatched through the
 * vtable slot. The loop header calls next(), a multi-value
 * (i32 ok, element) return, through the ITERATOR value's own
 * fat-pointer type; the element downcasts to the binding's type
 * exactly where streaming ref.casts its element temp.
 *
 * The synchronous protocol is tried first, then the asynchronous one
 * — `AsyncIterator` used as is, `AsyncIterable` asked through
 * `:asyncIterator` — in the order the checker resolved the loop's
 * element type, so a class that is both iterates the same way in a
 * `for` and a `for await`. Only a `for await` reaches the asynchronous
 * half; the checker rejects a plain `for` over it.
 */
export function lowerForInIterator(cx: LoweringContext, stmt: ForInStatement, iterableType: Type): void {
  let resolved = cx.resolveSelfType(iterableType);
  var iter = -1;
  // The interface iterated, for the shape of its `next()` steps.
  var protocol: InterfaceType | null = null;
  if (resolved is ClassType) {
    let ct = eraseTypeParameters(cx.wasm, resolved) as ClassType;
    let recv = lowerExprRaw(cx, stmt.iterable);
    let iteratorIface = findImplementedWellKnownInterface(cx.wasm, ct, WellKnown.IteratorInterface);
    let iterableIface = findImplementedWellKnownInterface(cx.wasm, ct, WellKnown.IterableInterface);
    let asyncIteratorIface = findImplementedWellKnownInterface(cx.wasm, ct, WellKnown.AsyncIteratorInterface);
    let asyncIterableIface = findImplementedWellKnownInterface(cx.wasm, ct, WellKnown.AsyncIterableInterface);
    if (iteratorIface != null) {
      // The iterable IS an iterator: no :iterator call.
      iter = cx.packInterface(recv, ct, iteratorIface);
      protocol = iteratorIface;
    } else if (iterableIface != null) {
      let packed = cx.packInterface(recv, ct, iterableIface);
      iter = cx.slotCallSingle(packed, ':iterator');
      protocol = iterableIface;
    } else if (asyncIteratorIface != null) {
      iter = cx.packInterface(recv, ct, asyncIteratorIface);
      protocol = asyncIteratorIface;
    } else if (asyncIterableIface != null) {
      let packed = cx.packInterface(recv, ct, asyncIterableIface);
      iter = cx.slotCallSingle(packed, ':asyncIterator');
      protocol = asyncIterableIface;
    } else {
      bail(cx, 'for-in iterable kind');
    }
  } else if (resolved is InterfaceType) {
    let recv = lowerExpr(cx, stmt.iterable, null);
    if (isWellKnown(WellKnown.IteratorInterface, resolved.name,
        resolved.sourcePath) ||
        isWellKnown(WellKnown.AsyncIteratorInterface, resolved.name,
        resolved.sourcePath)) {
      iter = recv;
      protocol = resolved;
    } else if (findExtendedWellKnownInterface(cx.wasm, resolved, WellKnown.IterableInterface) != null) {
      iter = cx.slotCallSingle(recv, ':iterator');
      protocol = findExtendedWellKnownInterface(cx.wasm, resolved, WellKnown.IterableInterface);
    } else if (findExtendedWellKnownInterface(cx.wasm, resolved, WellKnown.AsyncIterableInterface) != null) {
      iter = cx.slotCallSingle(recv, ':asyncIterator');
      protocol = findExtendedWellKnownInterface(cx.wasm, resolved, WellKnown.AsyncIterableInterface);
    } else {
      bail(cx, 'for-in iterable kind');
    }
  } else {
    bail(cx, 'for-in iterable kind');
  }

  // A tuple element arrives unboxed when the step says so.
  var unboxedElem: InlineTupleType | null = null;
  if (protocol != null) {
    let iterIface = iteratorInterfaceOf(cx.wasm, protocol);
    if (iterIface != null) {
      unboxedElem = stepInlineValue(cx.wasm, iterIface);
    }
  }

  var pat = stmt.pattern;
  if (pat is BindingPattern) {
    pat = pat.pattern;
  }
  var symOpt: Symbol | null = null;
  if (pat is IdentifierPattern || pat is Identifier) {
    symOpt = cx.func.model.resolveDeclaration(pat);
    if (symOpt == null) {
      bail(cx, 'for-in pattern');
    }
  } else if (!patternBinds(cx, pat)) {
    bail(cx, 'for-in pattern');
  }
  var bindVtOpt: ValType | null = null;
  if (symOpt != null) {
    let bindingType = cx.func.model.getBindingType(symOpt);
    if (bindingType == null) {
      bail(cx, 'untyped binding');
    }
    let vt = typeToValTypeWithSub(cx.wasm, bindingType, cx.func);
    if (!(vt is ValTypeVoid)) {
      // A zero-width element (`Iterable<void>`) binds nothing; the
      // protocol still drives the loop. Anything else unsupported
      // stays a loud bail.
      cx.requireSupported(vt, 'for-in element type');
    }
    bindVtOpt = vt;
  }

  // Generator disposal (docs/design/cancellation.md, "Abandonment and
  // generator disposal"): when the gate is open, every exit from a
  // protocol loop — exhaustion, break, return, throw, a cancellation —
  // runs a finalizer that disposes the iterator if it is a generator
  // frame. The finalizer region is the same one `using` rides;
  // `emitGeneratorDispose` is a no-op-shaped state write for anything
  // already finished and for non-generator iterators.
  if (cx.wasm.generatorDisposalActive && cx.wasm.generatorFrameSuper != null) {
    let mirrored = new Array<SymbolId>();
    cx.appendAssignedEnvSyms(stmt.body as Node, mirrored);
    lowerFinallyRegion(cx, mirrored, true, null,
        (scope, resultVar, saved) => {
          lowerForInIteratorLoop(cx, stmt, iter, symOpt, bindVtOpt, pat, unboxedElem);
          finishProtectedArm(cx, scope, resultVar, -1);
        },
        (fscope) => { emitGeneratorDispose(cx, iter); });
    return;
  }
  lowerForInIteratorLoop(cx, stmt, iter, symOpt, bindVtOpt, pat, unboxedElem);
}

/**
 * A `for await`'s Pending arm: suspend on the step's future, then enter
 * the body with the value it settles with (`Some`), or leave the loop
 * (`None`). A failed future resumes by throwing, as any `await` does
 * (async-iteration.md, "Consuming"). The future's type is the one the
 * checker recorded on the statement.
 */
function lowerPendingStep(cx: LoweringContext, stmt: ForInStatement,
    future: i32, zeroWidthElem: boolean, bodyB: i32, exitB: i32,
    exitArgs: GrowableArray<i32>, unboxedElem: InlineTupleType | null,
    laneVts: GrowableArray<ValType>) {
  let pendingT = cx.substitutedNodeType(stmt);
  if (pendingT == null || !(pendingT is ClassType)) {
    bail(cx, 'for await without a pending future type');
  }
  let futureCt = pendingT as ClassType;
  let someOpt = pendingSomeClass(futureCt);
  if (someOpt == null) {
    bail(cx, 'for await pending future is not Future<Option<T>>');
  }
  let someKey = getClassKey(someOpt as ClassType);
  var someSt: WasmStruct | null = null;
  if (let (true, info) = cx.wasm.classInfos.get(someKey)) {
    someSt = info.structType;
  }
  if (someSt == null) {
    bail(cx, 'for await Some class not discovered');
  }
  let st = someSt as WasmStruct;
  let settled = suspendOnFuture(cx, futureCt, cx.stripNonNull(future));

  let someVt = new ValTypeRef(st) as ValType;
  let isSome = cx.b.emit(IrOp.ref_test, new ValTypeI32(), settled,
      cx.b.internType(someVt));
  let someB = cx.b.newBlock();
  cx.b.brIf(isSome, someB, noValues(), exitB, exitArgs);
  cx.b.switchTo(someB);
  let intoBody = new Array<i32>();
  if (!zeroWidthElem) {
    let casted = cx.b.emit(IrOp.ref_cast, someVt, settled,
        cx.b.internType(someVt));
    let fieldIdx = st.fieldIndexByName('value');
    if (fieldIdx < 0) {
      bail(cx, 'for await Some has no value field');
    }
    let fieldRef = cx.b.addRef(new IrRefField(st, 'value'));
    let value = cx.b.emit(IrOp.struct_get, st.fields[fieldIdx].type,
        casted, fieldRef);
    if (unboxedElem != null) {
      // The future settles with the tuple boxed; the body takes its
      // values as a Ready step gives them.
      let boxed = new TupleType(unboxedElem.elementTypes, null, 0);
      let unboxed = unboxTuples(cx, cx.stripNonNull(value), boxed);
      if (unboxed == null || unboxed.values.length != laneVts.length) {
        bail(cx, 'for await unboxed element');
      }
      for (var i = 0; i < laneVts.length; i += 1) {
        intoBody.push(cx.conformToSlot(unboxed.values[i], laneVts[i],
            'for await element'));
      }
    } else {
      intoBody.push(value);
    }
  }
  cx.b.br(bodyB, intoBody);
}

/**
 * Binds a for-in pattern to an element that arrived unboxed, one value
 * per slot of `layout`: a tuple pattern takes the values directly, and a
 * name or any other pattern takes the tuple boxed.
 */
function bindUnboxedElement(cx: LoweringContext, lanes: GrowableArray<i32>,
    layout: InlineLayout, symOpt: Symbol | null, bindVtOpt: ValType | null,
    pat: Node): void {
  // The step's slots are nullable where its Done member leaves them
  // empty; a Ready step fills them.
  let values = new Array<i32>();
  for (var i = 0; i < lanes.length; i += 1) {
    values.push(cx.conformToSlot(lanes[i], layout.slots[i], 'for-in element'));
  }
  let source = new MultiValues(values, layout);
  if (pat is TuplePattern && tupleRestIndexOf(pat) < 0) {
    let nodes = new Array<Node | null>();
    let parts = source.patternValues(cx, pat.elements, null, nodes);
    for (var i = 0; i < nodes.length; i += 1) {
      let el = nodes[i];
      if (el != null && patternBinds(cx, el)) {
        lowerPatternBindings(cx, el, parts[i]);
      }
    }
    return;
  }
  var boxed = boxInline(cx, source);
  if (symOpt != null) {
    if (bindVtOpt != null) {
      boxed = cx.conformToSlot(boxed, bindVtOpt as ValType, 'for-in element type');
    }
    cx.bindPatternSym(symOpt, boxed);
  } else {
    lowerPatternBindings(cx, pat, boxed);
  }
}

/** The protocol loop itself: next() in the header, bind, body, back. */
function lowerForInIteratorLoop(cx: LoweringContext, stmt: ForInStatement,
    iter: i32, symOpt: Symbol | null, bindVtOpt: ValType | null,
    pat: Node, unboxedElem: InlineTupleType | null): void {
  let carried = cx.assignedEnvSyms(stmt.body);
  let header = cx.b.newBlock();
  let headerParams = cx.addParams(header, carried);
  cx.b.br(header, cx.envValues(carried));

  cx.b.switchTo(header);
  cx.applyParams(carried, headerParams);
  lowerContractClauses(cx, stmt.invariants);
  let results = cx.slotCallMulti(iter, 'next');
  // A zero-width element drops its `next()` value lane. Two protocols:
  // the boolean two-arm form `inline (true, T) | inline (false, _)` has
  // a flag plus the value (one lane when the value is zero-width); a
  // Step — `inline (0,_,_) | inline (1,V,_)`, or an AsyncStep with the
  // third arm `inline (2,_,F)` — adds a future lane (async-iteration.md),
  // so its `next()` returns one more result. A synchronous Step's
  // future lane is `nullref`: it is there so the step fits an
  // AsyncStep slot, and no arm ever fills it.
  // A tuple element the step unboxes (`(1, inline T, _)`) takes one
  // lane per slot of its inline form.
  let zeroWidthElem = unboxedElem == null && bindVtOpt != null &&
      (bindVtOpt as ValType) is ValTypeVoid;
  let elemLayout = if (unboxedElem != null) {
    inlineLayoutOf(cx.wasm, unboxedElem as Type)
  } else {
    null
  };
  let valueLanes = if (elemLayout != null) {
    elemLayout.slots.length
  } else if (zeroWidthElem) {
    0
  } else {
    1
  };
  let booleanLanes = 1 + valueLanes;
  let stepLanes = 2 + valueLanes;
  if (results.length < 1 || !(cx.valueTypeOf(results[0]) is ValTypeI32)) {
    bail(cx, 'iterator next shape');
  }
  let isStep = results.length == stepLanes;
  if (!isStep && (elemLayout != null || results.length != booleanLanes)) {
    bail(cx, 'iterator next shape');
  }
  let disc = results[0];
  let lanes = new Array<i32>();
  for (var i = 0; i < valueLanes; i += 1) {
    lanes.push(results[1 + i]);
  }
  var elem = if (elemLayout == null && !zeroWidthElem) lanes[0] else -1;

  let bodyB = cx.b.newBlock();
  let exitB = cx.b.newBlock();
  let exitParams = cx.addParams(exitB, carried);
  // Only a `for await` over an asynchronous iterator can see a Pending
  // step; the checker records the future's type on the statement for
  // exactly that loop, and rejects a plain `for` over one
  // (async-iteration.md, "Consuming"). Every other loop reads the
  // discriminant as a boolean: Done leaves, anything else has a value.
  let awaitsPending = isStep && stmt.isAwait &&
      cx.substitutedNodeType(stmt) != null;
  if (awaitsPending) {
    // The body is reached from two places — a Ready step, and a Pending
    // step whose future settled with a value — so the element arrives
    // as the body block's parameter.
    let elemIntoBody = new Array<i32>();
    let laneVts = new Array<ValType>();
    for (var i = 0; i < lanes.length; i += 1) {
      elemIntoBody.push(lanes[i]);
      laneVts.push(cx.valueTypeOf(lanes[i]));
      lanes[i] = cx.b.blockParam(bodyB, laneVts[i]);
    }
    if (elemLayout == null && !zeroWidthElem) {
      elem = lanes[0];
    }
    // Step discriminant: 0 = Done (exit), 1 = Ready (bind + body),
    // 2 = Pending. `disc != 0` splits Done from the rest; a second test
    // splits Ready from Pending.
    let restB = cx.b.newBlock();
    cx.b.brIf(disc, restB, noValues(), exitB, cx.envValues(carried));
    cx.b.switchTo(restB);
    let two = cx.b.constI32(2, new ValTypeI32() as ValType);
    let isPending = cx.b.binary(IrOp.i32_eq, new ValTypeI32(), disc, two);
    let pendingB = cx.b.newBlock();
    cx.b.brIf(isPending, pendingB, noValues(), bodyB, elemIntoBody);
    cx.b.switchTo(pendingB);
    lowerPendingStep(cx, stmt, results[results.length - 1], zeroWidthElem,
        bodyB, exitB, cx.envValues(carried), unboxedElem, laneVts);
  } else {
    cx.b.brIf(disc, bodyB, noValues(), exitB, cx.envValues(carried));
  }

  cx.b.switchTo(bodyB);
  if (elemLayout != null) {
    bindUnboxedElement(cx, lanes, elemLayout, symOpt, bindVtOpt, pat);
  } else if (zeroWidthElem) {
    // bindPatternSym stores the zero-width binding (NO_VALUE) itself.
    cx.bindPatternSym(symOpt as Symbol, -1);
  } else if (symOpt != null) {
    let bindVt = bindVtOpt as ValType;
    if (!isValSubtype(cx.valueTypeOf(elem), bindVt)) {
      // The slot's element type is erased/nullable relative to the
      // binding: runtime downcast, streaming's ref.cast on elemTemp.
      if (!isRefValType(bindVt)) {
        bail(cx, 'for-in element type');
      }
      elem = cx.b.emit(IrOp.ref_cast, bindVt, elem, cx.b.internType(bindVt));
    }
    cx.bindPatternSym(symOpt, elem);
  } else {
    // Destructuring patterns split the (boxed tuple) element.
    lowerPatternBindings(cx, pat, cx.stripNonNull(elem));
  }

  // No separate update block: the protocol loop has no per-iteration
  // update work, so `continue` and the body's fallthrough both branch
  // straight to the header. A dedicated update block whose only
  // content was that branch became an unreachable block — a verifier
  // error — whenever the body always left the loop (an unconditional
  // `break` or `return`).
  cx.loops.push(new LoopCtx(header, exitB, carried));
  // An element the iteration binds is the body's to consume or release,
  // as an `if let`'s binding is its branch's: the region releases the
  // owners the body does not move, on every way out of the iteration.
  lowerBranchStmt(cx, stmt.pattern as Node, stmt.body);
  if (cx.reachable) {
    cx.b.br(header, cx.envValues(carried));
  }
  cx.loops.pop();

  cx.b.switchTo(exitB);
  cx.applyParams(carried, exitParams);
  cx.reachable = true;
}

/**
 * for-in over a BoundedRange or FromRange: a loop counting up from the
 * range's start, the same loop `for (var i = start; i < end; i += 1)`
 * compiles to. The Iterable implementations in zena:core/range.zena
 * define the same sequence for every other use of a range.
 *
 * A range literal written in the loop header (`for (let i in 0..n)`)
 * never exists as a value: its bounds are lowered in place, start
 * first, as constructing it would. Any other range value has its
 * `start` and `end` fields read once, before the first iteration.
 *
 * A FromRange has no end. Its loop stops after binding the largest
 * i32 instead of wrapping around, matching FromRangeIterator.
 */
function lowerForInRange(cx: LoweringContext, stmt: ForInStatement,
    rangeCt: ClassType, bounded: boolean) {
  let i32Vt = new ValTypeI32() as ValType;
  var start = -1;
  var end = -1;
  let iterableExpr = stmt.iterable;
  if (iterableExpr is RangeExpression) {
    let startExpr = iterableExpr.start;
    let endExpr = iterableExpr.end;
    if (startExpr == null || (bounded && endExpr == null)) {
      bail(cx, 'for-in range literal shape');
    }
    start = lowerExpr(cx, startExpr as Expression, i32Vt);
    if (bounded) {
      end = lowerExpr(cx, endExpr as Expression, i32Vt);
    }
  } else {
    let cKey = getClassKey(rangeCt);
    var infoOpt: ClassCodegenInfo | null = null;
    if (let (true, info) = cx.wasm.classInfos.get(cKey)) {
      infoOpt = info;
    }
    if (infoOpt == null) {
      bail(cx, 'for-in range not discovered');
    }
    let {structType} = infoOpt;
    let range = lowerExprRaw(cx, iterableExpr);
    if (!isValSubtype(cx.valueTypeOf(range),
        new ValTypeRefNull(structType) as ValType)) {
      bail(cx, 'for-in range struct mismatch');
    }
    let startRef = cx.b.addRef(new IrRefField(structType, 'start'));
    start = cx.b.emit(IrOp.struct_get,
        cx.fieldValType(structType, 'start'), range, startRef);
    if (bounded) {
      let endRef = cx.b.addRef(new IrRefField(structType, 'end'));
      end = cx.b.emit(IrOp.struct_get,
          cx.fieldValType(structType, 'end'), range, endRef);
    }
  }

  var pat = stmt.pattern;
  if (pat is BindingPattern) {
    pat = pat.pattern;
  }
  var symOpt: Symbol | null = null;
  if (pat is IdentifierPattern || pat is Identifier) {
    symOpt = cx.func.model.resolveDeclaration(pat);
    if (symOpt == null) {
      bail(cx, 'for-in pattern');
    }
  } else if (!patternBinds(cx, pat)) {
    bail(cx, 'for-in pattern');
  }

  // The counter rides as the header block's first param, ahead of the
  // loop-carried env symbols (which work as in #lowerFor). First is
  // where #lowerFor's own counter lands, since the loop variable is
  // declared before the body's assignments, so both loops get the
  // same locals.
  let carried = cx.assignedEnvSyms(stmt.body);
  let header = cx.b.newBlock();
  let counter = cx.b.blockParam(header, i32Vt);
  let headerParams = cx.addParams(header, carried);
  let initArgs = new GrowableArray<i32>();
  initArgs.push(start);
  for (let v in cx.envValues(carried)) {
    initArgs.push(v);
  }
  cx.b.br(header, initArgs);

  cx.b.switchTo(header);
  cx.applyParams(carried, headerParams);
  lowerContractClauses(cx, stmt.invariants);
  let exitB = cx.b.newBlock();
  let exitParams = cx.addParams(exitB, carried);
  if (bounded) {
    let done = cx.b.binary(IrOp.i32_ge_s, i32Vt, counter, end);
    let bodyB = cx.b.newBlock();
    cx.b.brIf(done, exitB, cx.envValues(carried), bodyB, noValues());
    cx.b.switchTo(bodyB);
  }
  if (symOpt != null) {
    cx.bindPatternSym(symOpt, counter);
  } else {
    lowerPatternBindings(cx, pat, counter);
  }

  let updateB = cx.b.newBlock();
  let updateParams = cx.addParams(updateB, carried);
  cx.loops.push(new LoopCtx(updateB, exitB, carried));
  lowerStmt(cx, stmt.body);
  if (cx.reachable) {
    cx.b.br(updateB, cx.envValues(carried));
  }
  cx.loops.pop();

  cx.b.switchTo(updateB);
  cx.reachable = true;
  cx.applyParams(carried, updateParams);
  if (!bounded) {
    let max = cx.b.constI32(2147483647, i32Vt);
    let atMax = cx.b.binary(IrOp.i32_eq, i32Vt, counter, max);
    let stepB = cx.b.newBlock();
    cx.b.brIf(atMax, exitB, cx.envValues(carried), stepB, noValues());
    cx.b.switchTo(stepB);
  }
  let one = cx.b.constI32(1, i32Vt);
  let next = cx.b.binary(IrOp.i32_add, i32Vt, counter, one);
  let backArgs = new GrowableArray<i32>();
  backArgs.push(next);
  for (let v in cx.envValues(carried)) {
    backArgs.push(v);
  }
  cx.b.br(header, backArgs);

  cx.b.switchTo(exitB);
  cx.applyParams(carried, exitParams);
  cx.reachable = true;
}

/**
 * for-in over FixedArray/ImmutableArray values (a wasm array) and
 * Array instances (#buffer + #length struct): an index loop mirroring
 * the streaming fast paths, with the element re-bound each iteration.
 * The general iterator protocol needs multi-value next() calls, which
 * ZIR does not carry yet, so everything else bails.
 *
 * TODO(coverage): a Array whose `:iterator` is NOT overridden away
 * from the default (CHA query, same machinery as devirt) can compile
 * to a `.length` + `[]` indexed loop against the interface — no
 * multi-value needed — and M3 inlining then collapses those calls to
 * the #buffer/#length fast path. That splits the remaining bails into
 * genuinely-custom iterators (which do need multi-value) and
 * default-iterator sequences (which don't).
 */
export function lowerForIn(cx: LoweringContext, stmt: ForInStatement): void {
  // `for await` over a synchronous iterable never awaits: every step is
  // Ready, so the loop is exactly a `for` (async-iteration.md, "a `for
  // await` over [a sync iterator] never awaits"). The Pending arm — the
  // only place `for` and `for await` differ — arises only for an
  // iterator whose `next()` returns the three-arm Step, and is lowered
  // in `lowerForInIteratorLoop`, so `isAwait` needs no treatment here.
  var iterableType = cx.substitutedNodeType(stmt.iterable);
  if (iterableType == null) {
    bail(cx, 'untyped for-in iterable');
  }
  // A scoped iterator drives the same loop as the iterator it wraps;
  // the wrapper is erased (peel via typeArguments, as everywhere).
  if (isScopedType(iterableType)) {
    let scopedIt = scopedInner(iterableType);
    if (scopedIt != null) { iterableType = scopedIt; }
  }

  if (!(iterableType is ArrayType)) {
    let resolvedRange = cx.resolveSelfType(iterableType);
    if (resolvedRange is ClassType) {
      let bounded = isWellKnown(WellKnown.BoundedRangeClass,
          resolvedRange.name, resolvedRange.sourcePath);
      let targs = resolvedRange.typeArguments;
      let isI32Range = targs == null || (targs.length == 1 && targs[0] is I32Type);
      if (isI32Range && (bounded || isWellKnown(WellKnown.FromRangeClass,
              resolvedRange.name, resolvedRange.sourcePath))) {
        lowerForInRange(cx, stmt, resolvedRange, bounded);
        return;
      }
    }
  }

  // FIXED = a bare wasm array value; ARRAY = the Array class struct.
  var isFixed = false;
  var isArray = false;
  var elemType: Type | null = null;
  var arrayCt: ClassType | null = null;
  if (iterableType is ArrayType) {
    isFixed = true;
    elemType = iterableType.elementType;
  } else {
    let resolved = cx.resolveSelfType(iterableType);
    if (resolved is ClassType) {
      var canonical = resolved;
      while (canonical.genericSource != null) {
        canonical = canonical.genericSource as ClassType;
      }
      if (isWellKnown(WellKnown.FixedArrayClass, canonical.name,
              canonical.sourcePath) ||
          isWellKnown(WellKnown.ImmutableArrayClass, canonical.name,
              canonical.sourcePath)) {
        isFixed = true;
      } else if (isWellKnown(WellKnown.GrowableArrayClass, canonical.name,
                     canonical.sourcePath)) {
        isArray = true;
        arrayCt = resolved;
      }
      let targs = resolved.typeArguments;
      if ((isFixed || isArray) && targs != null && targs.length > 0) {
        elemType = targs[0];
      }
    }
  }
  if ((!isFixed && !isArray) || elemType == null) {
    lowerForInIterator(cx, stmt, iterableType);
    return;
  }

  // The fast paths consume the raw value; skip the top-level Iterable
  // adaptation the checker may have recorded on the iterable (the
  // streaming generator's ignoreAdaptations flag).
  let iterable = lowerExprRaw(cx, stmt.iterable);

  var buf = -1;
  var len = -1;
  if (isFixed) {
    buf = iterable;
    len = cx.b.emit(IrOp.array_len, new ValTypeI32(), buf, -1);
  } else {
    let ct = arrayCt as ClassType;
    let cKey = getClassKey(ct);
    var infoOpt: ClassCodegenInfo | null = null;
    if (let (true, info) = cx.wasm.classInfos.get(cKey)) {
      infoOpt = info;
    }
    if (infoOpt == null) {
      bail(cx, 'for-in Array not discovered');
    }
    let {structType} = infoOpt;
    if (!isValSubtype(cx.valueTypeOf(iterable),
        new ValTypeRefNull(structType) as ValType)) {
      bail(cx, 'for-in receiver struct mismatch');
    }
    let bufferName = getClassPrivateFieldKey(cKey, '#buffer');
    let lengthName = getClassPrivateFieldKey(cKey, '#length');
    let lenRef = cx.b.addRef(new IrRefField(structType, lengthName));
    len = cx.b.emit(IrOp.struct_get,
        cx.fieldValType(structType, lengthName), iterable, lenRef);
    let bufRef = cx.b.addRef(new IrRefField(structType, bufferName));
    buf = cx.b.emit(IrOp.struct_get,
        cx.fieldValType(structType, bufferName), iterable, bufRef);
  }
  let bufTarget = asWasmArray(cx.valueTypeOf(buf));
  if (bufTarget == null) {
    bail(cx, 'for-in buffer kind');
  }
  let storedVt = bufTarget.elementType;
  // Packed elements (narrow integers) are stored as wasm i8/i16 but
  // unpack to a full i32, so the loop variable is an i32 and the read
  // picks its extension from the semantic element type — the wasm array
  // type cannot distinguish u8 from i8.
  let packed = storedVt is ValTypeI8 || storedVt is ValTypeI16;
  let elemVt = if (packed) new ValTypeI32() as ValType else storedVt;
  var packedSigned = false;
  if (packed) {
    let iterT = cx.substitutedNodeType(stmt.iterable);
    if (iterT != null) {
      let elemT = arrayElementType(iterT);
      if (elemT != null) {
        packedSigned = isSignedNarrowIntType(elemT);
      }
    }
  }
  cx.requireSupported(elemVt, 'for-in element type');

  var pat = stmt.pattern;
  if (pat is BindingPattern) {
    pat = pat.pattern;
  }
  var symOpt: Symbol | null = null;
  if (pat is IdentifierPattern || pat is Identifier) {
    symOpt = cx.func.model.resolveDeclaration(pat);
    if (symOpt == null) {
      bail(cx, 'for-in pattern');
    }
  } else if (!patternBinds(cx, pat)) {
    bail(cx, 'for-in pattern');
  }

  // Index loop. The index rides as an extra header/update block param
  // (it is not an env symbol); loop-carried env symbols work as in
  // #lowerFor.
  let carried = cx.assignedEnvSyms(stmt.body);
  let header = cx.b.newBlock();
  let headerParams = cx.addParams(header, carried);
  let idxParam = cx.b.blockParam(header, new ValTypeI32());
  let initArgs = cx.envValues(carried);
  initArgs.push(cx.b.constI32(0, new ValTypeI32()));
  cx.b.br(header, initArgs);

  cx.b.switchTo(header);
  cx.applyParams(carried, headerParams);
  lowerContractClauses(cx, stmt.invariants);
  let done = cx.b.binary(IrOp.i32_ge_s, new ValTypeI32(), idxParam, len);

  let bodyB = cx.b.newBlock();
  let exitB = cx.b.newBlock();
  let exitParams = cx.addParams(exitB, carried);
  cx.b.brIf(done, exitB, cx.envValues(carried), bodyB, noValues());

  cx.b.switchTo(bodyB);
  let getOp = if (packed) {
    if (packedSigned) IrOp.array_get_s else IrOp.array_get_u
  } else {
    IrOp.array_get
  };
  let elem = cx.b.emit(getOp, elemVt, buf, idxParam);
  if (symOpt != null) {
    cx.bindPatternSym(symOpt, elem);
  } else {
    lowerPatternBindings(cx, pat, elem);
  }

  let updateB = cx.b.newBlock();
  let updateParams = cx.addParams(updateB, carried);
  cx.loops.push(new LoopCtx(updateB, exitB, carried));
  lowerStmt(cx, stmt.body);
  if (cx.reachable) {
    cx.b.br(updateB, cx.envValues(carried));
  }
  cx.loops.pop();

  cx.b.switchTo(updateB);
  cx.reachable = true;
  cx.applyParams(carried, updateParams);
  let one = cx.b.constI32(1, new ValTypeI32());
  let next = cx.b.binary(IrOp.i32_add, new ValTypeI32(), idxParam, one);
  let backArgs = cx.envValues(carried);
  backArgs.push(next);
  cx.b.br(header, backArgs);

  cx.b.switchTo(exitB);
  cx.applyParams(carried, exitParams);
  cx.reachable = true;
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19