Repository navigation
v0.7.10.8
v0.7.10.8 — positional + kw-only defaults via codegen_default_arguments
Date: 2026-04-30
Spec: notes/Spec/1500/1566_gocopy_v07108_defaults.md
Roadmap: notes/Spec/1500/1558_gocopy_v0710x_release_roadmap.md
v0.7.10.8 opens visitFuncBodyDef to function signatures that
supply default values:
- positional defaults (
def f(a, b=1),def f(a, b="x"),
def f(a, b=DEFAULT)) - keyword-only defaults (
def f(*, b=1))
Defaults are restricted to *ast.Constant (any constantValue-
friendly payload) or *ast.Name (1..15 chars, col ≤ 255). Wider
expression shapes still gate out. Parameter annotations stay gated
at v0.7.10.10.
To match CPython byte-for-byte, this release also ports the
fold_tuple_of_constants flowgraph pass: a LOAD_CONST/LOAD_SMALL_INT
chain followed by BUILD_TUPLE n collapses into a single
LOAD_CONST (c1, ..., cn). Without this pass the visitor's
LOAD_CONST 0 + LOAD_CONST 1 + BUILD_TUPLE 2 for default tuples
would not match CPython's LOAD_CONST 3 ((1, 2)) shape.
The pass also runs at module scope (runVisitorShadow in
compiler/visitor.go), since the defaults emit happens in the
parent module unit, not inside popChildUnit.
Added
tests/fixtures/funcbody/021_default_const_int.py—
def f(a, b=1): return b. Pins int default + folded(1,)tuple.tests/fixtures/funcbody/022_default_const_str.py—
def f(a, b="x"): return b. Pins str default + folded('x',).tests/fixtures/funcbody/023_default_name.py—
def f(a, b=DEFAULT): return b. Pins LOAD_NAME default — fold
refuses (LOAD_NAME isn't const-loading).tests/fixtures/funcbody/024_default_kwonly.py—
def f(*, b=1): return b. Pins kwonly default + BUILD_MAP +
SET_FUNCTION_ATTRIBUTE 2.compiler/flowgraph/fold_tuple_of_constants.go— 1:1 port of
Python/flowgraph.c:1436fold_tuple_of_constants+
:1293get_const_value+ :1350get_const_loading_instrs+
:1412instr_make_load_const. Walks each block, collapses
matching BUILD_TUPLE chains, returns the (extended) consts pool.compiler/flowgraph/fold_tuple_of_constants_test.go— 8
unit tests (LOAD_CONST chain, LOAD_SMALL_INT chain, single
element, mixed-loader refuse, too-few-loaders, no-BUILD_TUPLE,
nil seq, two BUILD_TUPLEs in one block).
Changed
compiler/codegen/visit_funcbody_stmt.go— gates loosened to
accept Defaults and non-nil KwOnlyDef. Validation extended over
default values (Constant | Name only). Defaults emit pattern:
positional values →BUILD_TUPLE K, then kwonly key/value pairs
→BUILD_MAP K, thenLOAD_CONST funcCode + MAKE_FUNCTION + SET_FUNCTION_ATTRIBUTE 2 (kwdefaults) + SET_FUNCTION_ATTRIBUTE 1 (defaults) + STORE_NAME. Default value loaders use the value's
AST span; the BUILD_TUPLE / BUILD_MAP / MAKE_FUNCTION / SET_*
instructions all shareouterLocso they collapse into one
linetable entry per CPython.compiler/codegen/scope.go::popChildUnit—FoldTupleOfConstants
inserted betweenOptimizeLoadConstandRemoveUnusedConsts.compiler/visitor.go::runVisitorShadow— same three-pass
pipeline now runs at module scope so the defaults emit folds
identically to a child function unit.compiler/visitor_parity_test.go— pipeline aligned with
production:OptimizeLoadConst + FoldTupleOfConstants + RemoveUnusedConstsrun beforeoptimize.Run.CHANGELOG.md— v0.7.10.8 entry added.
Tests
- Oracle (
bash tests/run.sh): 269/269 fixtures byte-identical
(was 265/265). - Visitor parity (
go test ./compiler/ -run TestVisitorParity):
byte-identical=229 not-implemented=40 mismatched=0 errored=0
(was 224/41/0/0). - 8 new flowgraph unit tests for
FoldTupleOfConstantspass. - All existing tests stay green.
Out of scope (deferred)
- Defaults richer than
Constant | Name(e.g.,
def f(a, b=g())) — defer. - Defaults at function scope (closures with default args) — defer.
- Parameter annotations — v0.7.10.10.
- Type params — same release as annotations.