Skip to content

v0.7.10.12

Choose a tag to compare

@github-actions github-actions released this 30 Apr 05:19
· 2 commits to main since this release
v0.7.10.12
383f9b1

v0.7.10.12 — while + for + nested-def + nonlocal in funcbody

Date: 2026-04-30
Spec: notes/Spec/1500/1570_gocopy_v07112_while_for_nested.md
Roadmap: notes/Spec/1500/1558_gocopy_v0710x_release_roadmap.md

v0.7.10.12 closes the funcbody surface gap left by v0.7.10.11
by promoting the four remaining stmt shapes spec 1558 §v0.7.10.12
bundled: while, for, nested def (with implicit closure
when the inner captures), and nonlocal (cell/free-var closure
pipeline). All four were previously rejected by visitFuncBodyDef
and fell through to the legacy visitFunctionDef path.

  • while — while name: target = const →
    LOAD test + TO_BOOL + POP_JUMP_IF_FALSE → exit, body emits
    NOT_TAKEN + body-Assign + JUMP_BACKWARD → top, exit emits
    LOAD_CONST None + RETURN_VALUE
  • for — for tgt in iter: bodyTgt = const →
    LOAD iter + GET_ITER + FOR_ITER → exit, body emits
    STORE tgt + body-Assign + JUMP_BACKWARD → top, exit emits
    END_FOR + POP_ITER + LOAD_CONST None + RETURN_VALUE
  • nested def — zero-arg inner def whose body is either
    [Return(rhs)] or [Nonlocal(...), Assign(t = rhs)].
    Closure cluster: optional cell-tuple build + LOAD_CONST
    innerCode + MAKE_FUNCTION + optional SET_FUNCTION_ATTRIBUTE 8
    • STORE for inner name.
  • nonlocal — nonlocal x; x = ... inside an inner def
    promotes x to a cell on the outer scope and a free on the
    inner. The inner emits LOAD_DEREF / STORE_DEREF; the outer
    emits MAKE_CELL prologue + LOAD_FAST (push cell to closure
    tuple) instead of LOAD_FAST_BORROW.

Symtable: LocalsPlus slot ordering — locals before cells

compiler/symtable/scope.go::LocalsPlusNames previously walked
OrderedNames once and appended each name to its slot group in
source-definition order. CPython's compute_localsplus_info
(Python/compile.c) instead emits all plain locals first,
then all non-param cells
, irrespective of source order. The
test fixture is fixture 048 (outer: x = 1; def inner: nonlocal x; x = 2; return inner): x (cell, defined line 2) appeared
before inner (local, defined line 3), so the old walk produced
LocalsPlusNames [x, inner] while CPython produces
[inner, x]. Fixed by walking OrderedNames twice — first
emitting plain SymLocal && !SymCell names, then emitting all
SymCell names — both walks preserve source order within each
group.

resolveNameOp: SymCell|SymFree wins over SymLocal

compiler/codegen/scope_ops.go::resolveNameOp previously fell
into the SymParam | SymLocal arm before the
SymCell | SymFree arm, so a nonlocal x; x = 2 binding (which
has both SymLocal from the assign and SymFree from the
nonlocal directive) wrongly dispatched to STORE_FAST. CPython's
compiler_nameop (Python/codegen.c) classifies CELL/FREE
ahead of LOCAL — the deref must run against the parent's cell,
not a fresh fast slot. Fixed by reordering: cell/free check
first, param/local check second, global/builtin check third.

Closure tuple build uses LOAD_FAST, not LOAD_DEREF

compiler_make_closure (Python/codegen.c) bypasses
compiler_nameop when building the closure tuple operand: it
loads the cell object itself (not the cell's value), so for
a cell-in-current-scope it emits LOAD_FAST against the
LocalsPlus slot, not LOAD_DEREF. (LOAD_FAST then promotes to
LOAD_FAST_BORROW via optimize_load_fast.) For a free-in-
current-scope (i.e., a captured name passed through to a
nested scope), it emits LOAD_DEREF. emitFuncBodyInnerDef
mirrors this: it inspects outer.Scope.Symbols[free].Flags
and picks LOAD_FAST or LOAD_DEREF directly, not via
emitNameLoad (which would always pick LOAD_DEREF for cells).

Inner def Loc spans through body end

CPython's LOC(s) for a FunctionDef stmt is the full def-block
span — from def keyword (line, col) through the end of the
last body statement (endLine, endCol). gocopy's gopapy AST
doesn't carry end_lineno/end_col_offset on FunctionDef, so
innerDefBodyEnd recovers the span from the last body
statement's RHS via astExprEndCol. The MAKE_FUNCTION /
SET_FUNCTION_ATTRIBUTE / STORE cluster in the outer scope all
carry this span; the linetable encoder emits a LONG entry for
the multi-line case.

visitFuncBodyDef MAKE_CELL + COPY_FREE_VARS prologue

The visitor now walks childScope.Cells and emits MAKE_CELL N
(at NO_LOCATION) for each cell, then if len(childScope.Frees) > 0 emits COPY_FREE_VARS N (also NO_LOCATION) — both before
RESUME 0 at firstlineno. Mirrors CPython's
compiler_function_body setup phase.

Added

  • tests/fixtures/funcbody/048_stmt_nonlocal.py —
    def outer: x = 1; def inner: nonlocal x; x = 2; return inner.
    Pins MAKE_CELL on outer slot 0 (x), STORE_DEREF in inner,
    LOAD_FAST + BUILD_TUPLE 1 + MAKE_FUNCTION +
    SET_FUNCTION_ATTRIBUTE 8 closure cluster, and the
    LocalsPlusNames [inner, x] slot ordering.
  • tests/fixtures/funcbody/049_stmt_while.py —
    def f: x = 0; while x: x = 0. Pins three-block while CFG
    (top + body + exit), TO_BOOL + POP_JUMP_IF_FALSE, NOT_TAKEN
    head on body, JUMP_BACKWARD → top, implicit
    LOAD_CONST None + RETURN_VALUE on exit.
  • tests/fixtures/funcbody/050_stmt_for.py —
    def f(xs): for x in xs: y = 1. Pins entry GET_ITER, top
    FOR_ITER → exit, body STORE_FAST tgt + body-Assign +
    JUMP_BACKWARD → top, exit END_FOR + POP_ITER + LOAD_CONST
    None + RETURN_VALUE.
  • tests/fixtures/funcbody/051_stmt_nested_def.py —
    def outer: def inner: return 1; return inner. Pins the
    no-closure inner-def path: outer has no cells/frees, inner
    is plain (no COPY_FREE_VARS), MAKE_FUNCTION cluster carries
    no SET_FUNCTION_ATTRIBUTE 8.

Changed

  • compiler/symtable/scope.go::LocalsPlusNames — split the
    OrderedNames walk into two passes (plain locals first,
    then cells), so non-param cells emit after plain locals
    regardless of source order.
  • compiler/codegen/scope_ops.go::resolveNameOp — reorder
    arms so SymCell|SymFree checks before SymParam|SymLocal.
  • compiler/codegen/visit_funcbody_stmt.go — extend the
    last-stmt classifier with *ast.While and *ast.For;
    extend the non-last-stmt classifier with *ast.FunctionDef
    (validated via validateFuncBodyInnerDef). Add MAKE_CELL
    and COPY_FREE_VARS prologue emit before RESUME. Wire
    last-stmt dispatch to emitFuncBodyWhile /
    emitFuncBodyFor and non-last dispatch to
    emitFuncBodyInnerDef.
  • compiler/codegen/visit_funcbody_loop.go — new file:
    validateFuncBodyWhile, validateFuncBodyFor,
    emitFuncBodyWhile, emitFuncBodyFor. Both emitters
    return the body's last source line/col so the outer
    FunctionDef Loc spans through the loop body end (not just
    the while/for header).
  • compiler/codegen/visit_funcbody_innerdef.go — new file:
    validateFuncBodyInnerDef, emitFuncBodyInnerDef,
    innerDefBodyEnd. Inner consts get a None plant if
    nothing else is added during body emit. Closure tuple
    build uses LOAD_FAST for cells / LOAD_DEREF for frees.
  • CHANGELOG.md — v0.7.10.12 entry added.

Tests

  • Oracle (bash tests/run.sh): 287/287 fixtures byte-identical
    (was 283/283).
  • Visitor parity (go test ./compiler/ -run TestVisitorParity):
    byte-identical=247 not-implemented=40 mismatched=0 errored=0
    (was 243/40/0/0).
  • All existing tests stay green.

Out of scope (deferred)

  • break / continue inside while/for — needs the
    label-stack pipeline compiler_visit_stmt_break /
    compiler_visit_stmt_continue and with_loops block-
    scope tracking. Rolls into v0.7.10.13.
  • non-empty while-else / for-else — needs the orelse
    visit + label binding after the loop's exit.
  • multi-stmt while/for body — only single-Assign body
    accepted; multi-stmt requires a generic body-visit loop.
  • non-Name while test / non-Name for iter — Constant /
    Compare / Call tests defer to richer codegen_jump_if and
    codegen_visit_expr support.
  • Tuple for-target — for x, y in items needs UNPACK_*
    instructions and lvalue tuple handling.
  • Inner def with arguments — only zero-arg inner defs
    accepted.
  • Inner def with its own cells (3-deep capture) — when
    inner is itself an outer for a grandchild scope. Defers
    to a later cycle.
  • Inner def with decorators / annotations / type params —
    same gate-outs as outer-def support already had.