Repository navigation
v0.7.10
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 childsymtable.Scopeby
source order, pushes a childcompileUnit, walks body
statements viavisitFuncStmt, pops viapopChildUnit, and
emitsLOAD_CONST funcCode + MAKE_FUNCTION + STORE_NAMEin
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_BORROWmove/borrow
classification is precomputed via last-use scanning over the
body; theLFLBLFLBsuper-instruction is fused at emit time
viafuseLflblflbTail.compiler/codegen/visit_func_stmt.go(490 LOC) — the
per-statement and per-expression dispatchersvisitFuncStmt/
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 flipsCOMPARE_OPoparg+16 instead of
emittingUNARY_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 atco_consts[0]) is implemented in
emitFuncBodyConstLoadFirstInt.compiler/codegen/scope_ops.go(163 LOC) — scope-driven
Name-resolution helpers mirroring CPython'scodegen_nameop.
resolveNameOpreads the unit'ssymtable.Scopeand dispatches
tonameOpFast(LOAD_FAST / STORE_FAST),nameOpDeref
(LOAD_DEREF / STORE_DEREF),nameOpGlobal(LOAD_GLOBAL /
STORE_GLOBAL), ornameOpModule(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.FunctionDefarm now triesvisitFuncBodyDeffirst; on
ErrNotImplementedit falls through tovisitClosureDef,
then to the v0.7.8visitFunctionDef. The three visitors'
AST predicates are layered:visitFuncBodyDefaccepts the
multi-statement general body,visitClosureDefaccepts the
exact closure shape,visitFunctionDefaccepts 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 asLOAD_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.Buildis still empty (returns
ErrUnsupportedunconditionally) 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 Nonepredicates (fixtures 191-192) —
classifier-only at v0.7.10. The visitor's first-int quirk
needs the symtable'snoneCheckFuncflag to suppress int-pool
registration; lands once the symtable surface grows that flag.- Wider predicate tree —
BoolOp, walrus (NamedExpr),
the long tail ofCompareoperator 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_BORROWchosen at
emit time,LFLBLFLBfused at emit time, first-int quirk
hand-implemented). v0.7.10.x will lift these into post-emit
passes that mirror CPython'soptimize_load_fastand
insert_superinstructions. The visitor will emit unoptimized
IR and the optimizer will canonicalize. compiler/func_body.goretirement — 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.