Repository navigation
v0.7.10.11
v0.7.10.11 — raise + assert + global + AnnAssign in funcbody
Date: 2026-04-30
Spec: notes/Spec/1500/1569_gocopy_v07111_raise.md
Roadmap: notes/Spec/1500/1558_gocopy_v0710x_release_roadmap.md
v0.7.10.11 opens visitFuncBodyDef to four of the five stmt
shapes spec 1558 §v0.7.10.11 bundled: raise, assert,
global, and AnnAssign. nonlocal defers to v0.7.10.12
because it needs the cell/free-var closure pipeline (cellvar
mark on outer scope, STORE_DEREF/LOAD_DEREF on inner).
- raise — bare /
raise X/raise X from Y - global —
global x; x = 1→ STORE_GLOBAL,
co_varnames=() - AnnAssign —
x: int = 5emits as Assign;
x: int(no value) emits no instructions per PEP 526
function-scope rules - assert —
assert Xandassert X, "msg"→
TO_BOOL + POP_JUMP_IF_TRUE + LOAD_COMMON_CONSTANT 0 +
(optional msg load + CALL 0) + RAISE_VARARGS 1 +
implicit LOAD_CONST None + RETURN_VALUE
CFG terminator fix
compiler/flowgraph/cfg.go::isTerminator was missing
RAISE_VARARGS. CPython 3.14 Python/flowgraph.c classifies it
under IS_SCOPE_EXIT_OPCODE, so a basic block that ends with
RAISE_VARARGS must not get a fall-through edge. Without this,
assert X produced co_stacksize=2 instead of CPython's 1
because Block 1 (which ends in RAISE_VARARGS) propagated
depth=1 into the end block via a spurious fall-through, and the
end block's LOAD_CONST None then peaked at 2.
isScopeExit in optimize_load_fast.go extended in lockstep
to keep the two predicates aligned.
Symtable: global wins over local
compiler/symtable/build.go::analyze now strips SymLocal from
any name that also has SymGlobal set. This mirrors
CPython's symtable.c::analyze_block rule "global wins over
local": for global x; x = 1, visitTarget had unconditionally
added SymLocal for the assignment target, leaving x in
co_varnames. Stripping SymLocal when SymGlobal is set
forces the analyze pass to keep x in co_names (and out of
co_varnames) — the shape CPython emits.
*ast.Assert was also added to visitStmt (was rejected as
unsupported); it visits Test and Msg into the current scope.
Stack size minimum
Bare raise emits only RESUME 0 and RAISE_VARARGS 0,
both zero-effect, so StackDepth reports 0. CPython's
Objects/codeobject.c::_PyCode_Validate (lines 517-519)
unconditionally bumps every code object's stacksize to ≥ 1
before serialization:
if (con->stacksize == 0) {
con->stacksize = 1;
}Ported 1:1 in compiler/assemble/assemble.go after the
StackDepth(cfg) call. Fixture 040 (bare raise) is the
trigger; without this bump it would ship co_stacksize=0
while CPython's reference .pyc carries co_stacksize=1.
Assert codegen
Mirrors CPython's codegen_assert (Python/codegen.c:2932):
test block: visit Test + TO_BOOL + POP_JUMP_IF_TRUE → end
raise block: NOT_TAKEN + LOAD_COMMON_CONSTANT 0 [+ Msg load + CALL 0] + RAISE_VARARGS 1
end block: LOAD_CONST None + RETURN_VALUE
All raise-block instructions emit at LOC(test) per
flowgraph.c::propagate_line_numbers (NO_LOCATION inherits
prev_location). LOAD_COMMON_CONSTANT 0 is the 3.14
replacement for 3.13's LOAD_ASSERTION_ERROR — oparg 0 is
CONSTANT_ASSERTIONERROR.
Added opcodes
bytecode.RAISE_VARARGS = 104— HasArg, ArgRaw,
OpTerminator, StackVar (oparg-dependent pop).bytecode.LOAD_COMMON_CONSTANT = 81— HasArg, ArgRaw,
StackEff +1.
Added
tests/fixtures/funcbody/040_stmt_raise_bare.py—
def f():\n raise. Pins RESUME + RAISE_VARARGS 0,
co_consts=[None], single linetable entry, and the
stacksize=1 bump.tests/fixtures/funcbody/041_stmt_raise_with_value.py—
def f():\n raise ValueError. LOAD_GLOBAL +
RAISE_VARARGS 1,co_names=('ValueError',).tests/fixtures/funcbody/042_stmt_raise_from.py—
def f():\n raise ValueError from KeyError. LOAD_GLOBAL × 2- RAISE_VARARGS 2.
tests/fixtures/funcbody/043_stmt_global.py—
def f(): global x; x = 1; return x. STORE_GLOBAL +
LOAD_GLOBAL,co_names=('x',),co_varnames=().tests/fixtures/funcbody/044_stmt_annassign_value.py—
def f(): x: int = 5; return x. Annotation dropped;
LOAD_SMALL_INT 5 + STORE_FAST 0 + LOAD_FAST 0 + RETURN_VALUE.tests/fixtures/funcbody/045_stmt_annassign_no_value.py—
def f(): x: int; return 1. Emits no instructions for
x: int;co_varnames=().tests/fixtures/funcbody/046_stmt_assert.py—
def f(): assert x. Three-block CFG with
LOAD_COMMON_CONSTANT and the implicit RETURN_NONE epilogue,
co_stacksize=1.tests/fixtures/funcbody/047_stmt_assert_msg.py—
def f(): assert x, "msg". Adds LOAD_CONST 'msg' + CALL 0,
co_stacksize=2.
Changed
bytecode/opcode.go— add RAISE_VARARGS, LOAD_COMMON_CONSTANT.bytecode/opmeta.go— register both opcodes.compiler/assemble/assemble.go— port
_PyCode_Validateminimum-stacksize-1 bump.compiler/flowgraph/cfg.go— extendisTerminatorwith
RAISE_VARARGS so a raise-ending block has no fall-through.compiler/flowgraph/optimize_load_fast.go— extend
isScopeExitwith RAISE_VARARGS to match
CPython'sIS_SCOPE_EXIT_OPCODE.compiler/symtable/build.go— accept*ast.Assert;
stripSymLocalfrom globals inanalyzeso
global x; x = 1excludesxfromco_varnames.compiler/codegen/visit_funcbody_stmt.go— extend the
last-stmt classifier with*ast.Raiseand*ast.Assert;
extend the non-last-stmt classifier with*ast.AnnAssign,
*ast.Global,*ast.Nonlocal. Add validators
(validateFuncBodyRaise,validateFuncBodyAssert,
validateFuncBodyAnnAssign,validateFuncBodyGlobalNonlocal)
and emitters (emitFuncBodyRaise,emitFuncBodyAssert,
emitFuncBodyAnnAssign). Global / Nonlocal are no-ops at
emit time.emitFuncBodyAssignnow routes through
emitNameStoresoglobal x; x = 1resolves to
STORE_GLOBAL.CHANGELOG.md— v0.7.10.11 entry added.
Tests
- Oracle (
bash tests/run.sh): 283/283 fixtures byte-identical
(was 275/275). - Visitor parity (
go test ./compiler/ -run TestVisitorParity):
byte-identical=243 not-implemented=40 mismatched=0 errored=0
(was 235/40/0/0). - All existing tests stay green.
Out of scope (deferred)
- nonlocal — needs cell/free-var closure pipeline
(cellvar mark on outer + STORE_DEREF/LOAD_DEREF on inner).
Rolls into v0.7.10.12 alongside while/for/nested-def. - Mid-body raise / assert — needs
eliminate_unreachable_codeflowgraph pass to drop the
trailing implicit RETURN_NONE. Stays on the legacy
visitFunctionDefpath. - raise of richer expression —
raise Exception("msg"),
raise mod.Err. Same gate-out — only bare-Name accepted. - assert of richer expression —
assert x == 1,
assert all(...). Only bare-Name Test accepted. - AnnAssign with non-Name annotation or value —
x: List[int] = ...and friends. Only Name-annotated,
Name-or-Constant-valued AnnAssigns accepted.