Skip to content

v0.7.10

Choose a tag to compare

@github-actions github-actions released this 29 Apr 17:51
· 14 commits to main since this release
v0.7.10
ba4ae9a

v0.7.10 — general function bodies (visit_funcbody_stmt + visit_func_stmt + scope_ops)

Date: 2026-04-30
Spec: notes/Spec/1500/1553_gocopy_v0710_visit_funcbody.md
Architecture: notes/Spec/1500/1541_gocopy_v07_architecture.md
Roadmap: notes/Spec/1500/1542_gocopy_v07x_roadmap.md
CPython parity: Python/codegen.c (3.14.0)
codegen_function_body (the inner body emit core), codegen_nameop
(the central Name-resolution dispatcher; mirrored by
compiler/codegen/scope_ops.go::resolveNameOp),
codegen_visit_stmt (the body-statement dispatcher; mirrored by
visit_func_stmt.go::visitFuncStmt), codegen_visit_expr (the
expression dispatcher; mirrored by visit_func_stmt.go::visitFuncExpr).
Python/flowgraph.c (3.14.0) optimize_load_fast and
insert_superinstructions (whose effects the visitor still
precomputes at emit time — LOAD_FAST_BORROW move/borrow
classification and the LFLBLFLB super-instruction fusion — and
that v0.7.10.x will lift out into real post-emit passes).

v0.7.9 closed phase B's closure step. v0.7.10 is the first cut at
the surface phase B was always pointing at: general function
bodies
. The v0.7.8 / v0.7.9 visitors only knew the leaf shapes
def f(arg): return arg and def f(x): def g(): return x; return g. v0.7.10 extends visit_FunctionDef to multi-statement
bodies — sequences of Assign / AugAssign / If / Return /
bare-Call statements over BinOp / UnaryOp / Compare /
Call / Attribute / Subscript / Tuple / IfExp /
Constant / Name expressions, with elif chains and terminating
if-else as the last statement.

The v0.6 classifier path's 1,516-line compiler/func_body.go
recursive expression compiler is NOT retired here. The
v0.7.10 visitor catches a large subset of the surface that file
covers — enough to lift visitor parity from 142/246 to 205/246 —
but several shapes (is None / is not None, the wider
predicate tree, the long tail of statement combinations) remain
classifier-owned. Spec 1553 expected v0.7.10 to land the full
retirement; in practice the per-shape gating became its own
problem and the cleanup is being moved into the v0.7.10.x
systematic series (see "Out of scope" below).

After v0.7.10 the visitor owns 205 of 246 fixtures byte-identical.
The remaining 41 still fall through to the classifier.
compiler/func_body.go stays alive; its funeral moves to the
v0.7.10.x roadmap.

The 246/246 oracle stays byte-identical.

Added

  • compiler/codegen/visit_funcbody_stmt.go (820 LOC) — the
    general-function-body visitor entry point. visitFuncBodyDef
    pairs the FunctionDef AST with its child symtable.Scope by
    source order, pushes a child compileUnit, walks body
    statements via visitFuncStmt, pops via popChildUnit, and
    emits LOAD_CONST funcCode + MAKE_FUNCTION + STORE_NAME in
    the parent. Validators (validateFuncBodyAssignRHS,
    validateFuncBodyReturnValue, validateFuncBodyIf,
    validateFuncBodyTerminatingIf) gate the AST shape up front
    so emit code can assume coverage. Body-shape emitters
    (emitFuncBodyAssign, emitFuncBodyAugAssign,
    emitFuncBodyReturn, emitFuncBodyIf,
    emitFuncBodyTerminatingIf, emitFuncBodyTernaryReturn,
    emitFuncBodyEarlyReturnIf, emitFuncBodyIfAssign) cover
    early-return-if, if-assign, elif chains, terminating if-else,
    and ternary-in-return. LOAD_FAST_BORROW move/borrow
    classification is precomputed via last-use scanning over the
    body; the LFLBLFLB super-instruction is fused at emit time
    via fuseLflblflbTail.
  • compiler/codegen/visit_func_stmt.go (490 LOC) — the
    per-statement and per-expression dispatchers visitFuncStmt /
    visitFuncExpr. Statement arms cover *ast.Assign,
    *ast.AugAssign, *ast.If, *ast.Return, *ast.ExprStmt
    (bare call). Expression arms cover *ast.Constant,
    *ast.Name, *ast.BinOp, *ast.UnaryOp (with the not-Compare
    specialization that flips COMPARE_OP oparg+16 instead of
    emitting UNARY_NOT), *ast.Compare (value-context), *ast.Call
    (positional, Name callee + method call), *ast.Attribute,
    *ast.Subscript, *ast.Tuple, *ast.IfExp. The first-int
    CPython quirk (int 0..255 first-emitted to a function body
    lands at co_consts[0]) is implemented in
    emitFuncBodyConstLoadFirstInt.
  • compiler/codegen/scope_ops.go (163 LOC) — scope-driven
    Name-resolution helpers mirroring CPython's codegen_nameop.
    resolveNameOp reads the unit's symtable.Scope and dispatches
    to nameOpFast (LOAD_FAST / STORE_FAST), nameOpDeref
    (LOAD_DEREF / STORE_DEREF), nameOpGlobal (LOAD_GLOBAL /
    STORE_GLOBAL), or nameOpModule (LOAD_NAME / STORE_NAME).
    The visitor consults this rather than hand-classifying names
    per-emit-site.
  • compiler/codegen/source_scan.go (286 LOC) — column-span
    scanner helpers (scanCallEnd, scanBackOpen, scanForward,
    astExprEndCol, ...) lifted from the classifier path so the
    visitor can compute end columns without round-tripping through
    the AST. The classifier still owns its own copy until the
    v0.7.10.x cleanup.

Changed

  • compiler/codegen/visit_stmt.go::visitStmt — the
    *ast.FunctionDef arm now tries visitFuncBodyDef first; on
    ErrNotImplemented it falls through to visitClosureDef,
    then to the v0.7.8 visitFunctionDef. The three visitors'
    AST predicates are layered: visitFuncBodyDef accepts the
    multi-statement general body, visitClosureDef accepts the
    exact closure shape, visitFunctionDef accepts the leaf
    single-stmt-Return shape. Each visitor only sees its own
    shape.
  • compiler/symtable/scope.go::Scope.Resolve — narrowed to walk
    parent function scopes only (skipping module and class
    scopes), matching CPython's free-variable rule that
    module-level assignments do not promote nested-function reads
    to free variables (those resolve as LOAD_GLOBAL). Earlier
    callers were already doing this filter inline; the helper
    centralizes it.
  • bytecode/opcode.go, bytecode/opmeta.go — opcode metadata
    rows added for the bare-call statement path the visitor now
    exercises directly.
  • compiler/codegen/codegen.go — package doc updated to record
    the v0.7.10 promotion. codegen.Build is still empty (returns
    ErrUnsupported unconditionally) and the legacy fallback path
    still drives any shape the visitor rejects.

Removed

  • Nothing. compiler/func_body.go (1,516 LOC) stays alive for
    the long tail of shapes the v0.7.10 visitor doesn't yet catch.
    Its funeral moves into the v0.7.10.x systematic series.

Tests

  • 246/246 fixtures byte-identical (bash tests/run.sh).
  • TestVisitorParity: byte-identical=205 not-implemented=41 mismatched=0 errored=0 skipped=0. Up from 142/246 at v0.7.9.
    The 63-fixture lift covers the multi-stmt assign+return /
    binop / unary / compare / call / attribute / subscript /
    tuple / ternary-in-return / early-return-if / if-assign /
    elif-chain / terminating-if-else shapes.
  • go build ./... clean. go vet ./... clean.
  • go test ./... clean.

Out of scope — moves to v0.7.10.x systematic series

The original v0.7.10 spec (1553) called for full retirement of
compiler/func_body.go. The per-shape gating approach the
visitor took to climb from 142 to 205 fixtures was working but
ad hoc: every additional shape needed its own validator +
emitter pair, the LOAD_FAST_BORROW move/borrow rewrite was
precomputed at emit time rather than as a post-emit pass, and
the LFLBLFLB super-instruction was fused inline rather than
through CPython's insert_superinstructions. The v0.7.10.x
series replaces these with a CPython-faithful pipeline:

  • is None / is not None predicates (fixtures 191-192) —
    classifier-only at v0.7.10. The visitor's first-int quirk
    needs the symtable's noneCheckFunc flag to suppress int-pool
    registration; lands once the symtable surface grows that flag.
  • Wider predicate tree — BoolOp, walrus (NamedExpr),
    the long tail of Compare operator combinations, comprehension
    bodies. Targeted in v0.7.10.x once the architecture spec
    (slot 1557) defines the canonical visitor surface.
  • Optimizer canonicalization — the visitor today emits
    optimized bytecode directly (LOAD_FAST_BORROW chosen at
    emit time, LFLBLFLB fused at emit time, first-int quirk
    hand-implemented). v0.7.10.x will lift these into post-emit
    passes that mirror CPython's optimize_load_fast and
    insert_superinstructions. The visitor will emit unoptimized
    IR and the optimizer will canonicalize.
  • compiler/func_body.go retirement — moves to the tail of
    v0.7.10.x. Once the visitor surface covers every shape the
    classifier touches, the file becomes unreachable and joins the
    v0.7.26 funeral.
  • gopapy parser changes — out of scope, as always.

The v0.7.10.x roadmap (slot 1558) sequences these into per-release
chunks. Specs 1556 (comprehensive funcbody fixtures), 1557
(CPython-faithful pipeline architecture), and 1558 (release
roadmap) follow this changelog.