mem: List(T) mutations gain optional alloc: Allocator = context.allocator
The chess panel-text regression (text vanished after the first move on macOS) had a single root cause: GlyphCache's entries List, hash table, and shaped_buf grew through `context.allocator` — which during render is the per-frame arena. On the next arena reset the backing died, and subsequent glyph lookups read garbage / wrote into freshly-allocated view-tree memory. Fix is shaped as the user proposed: `List(T)`'s mutations take an optional trailing `alloc: Allocator = context.allocator` argument. No allocator stored on the container, no init ceremony, every existing `list.append(item)` callsite keeps working unchanged. Long-lived owners now write `list.append(item, self.parent_allocator)` and the arena-leak bug becomes impossible to write accidentally. Default-arg substitution previously only fired for identifier callees (`expandCallDefaults` at lower.zig:7978). Extended to the generic struct-method dispatch path (`list.append(...)` lands here) via a new `appendDefaultArgs` helper that lowers fd.params[i].default_expr in the caller's scope and appends to the lowered args slice. Long-lived owners updated to capture `parent_allocator: Allocator` at init and use it for every internal growth: - GlyphCache (the chess bug) — entries, shaped_buf, hash_keys, hash_vals, atlas bitmap. - DockInteraction — drops the existing `push Context` workaround in `ensure_capacity` for the explicit-arg form. - StateStore — entries list + per-entry data buffer. - Gles3Gpu, MetalGPU — shaders, buffers, textures (atlas-grow during render would otherwise leak resources into the frame arena). Also kept: an operator-precedence fix in pipeline.sx (`(self.frame_index & 1) == 0` instead of `self.frame_index & 1 == 0`, which parses as `self.frame_index & (1 == 0)` = always 0). That was a stealth single-arena-only bug that masked the GlyphCache one for a long time. Docs: - specs.md §11 documents `param: T = expr` default parameter values. The parser already supported it — formalised in the spec now. - current/CHECKPOINT-MEM.md logs the change. - CLAUDE.md REJECTED PATTERNS gains a "Long-lived containers growing through context.allocator" section with the `parent_allocator` capture template and the list of existing examples to mirror. 155/155 example tests pass — zero-diff against snapshots since every existing callsite still resolves to `context.allocator`.
This commit is contained in:
41
specs.md
41
specs.md
@@ -1061,6 +1061,45 @@ main :: () {
|
||||
}
|
||||
```
|
||||
|
||||
#### Default Parameter Values
|
||||
|
||||
A parameter can declare a default value with `name: type = expr`. When a
|
||||
caller omits the trailing positional argument, the compiler substitutes
|
||||
the default expression at the call site:
|
||||
|
||||
```sx
|
||||
greet :: (name: string, prefix: string = "Hello") {
|
||||
print("{} {}!\n", prefix, name);
|
||||
}
|
||||
|
||||
greet("world"); // prints "Hello world!"
|
||||
greet("world", "Good morning"); // prints "Good morning world!"
|
||||
```
|
||||
|
||||
The default expression is captured as an AST node at parse time and
|
||||
re-lowered fresh at each call site, so runtime expressions like
|
||||
`context.allocator` resolve in the **caller's** scope, not the callee's
|
||||
definition site. This is the mechanism that lets stdlib containers like
|
||||
`List(T)` expose an optional allocator argument that defaults to
|
||||
`context.allocator` without requiring callers to thread one through:
|
||||
|
||||
```sx
|
||||
// In std.sx:
|
||||
List :: struct ($T: Type) {
|
||||
append :: (list: *List(T), item: T, alloc: Allocator = context.allocator) {
|
||||
// ... grows via `alloc.alloc(...)` ...
|
||||
}
|
||||
}
|
||||
|
||||
// Call sites:
|
||||
list.append(42); // alloc = current context.allocator
|
||||
list.append(42, self.parent_allocator); // alloc = the named long-lived owner
|
||||
```
|
||||
|
||||
Defaults are only consulted for **trailing** missing positional args; once
|
||||
a position is provided, all earlier positions must also be provided. There
|
||||
is no named-argument syntax for skipping middle defaults.
|
||||
|
||||
### Enum Definition
|
||||
|
||||
```sx
|
||||
@@ -2041,7 +2080,7 @@ struct_decl = IDENT '::' 'struct' '{' struct_member* '}'
|
||||
struct_member = field_group | '#using' IDENT ';'
|
||||
field_group = IDENT (',' IDENT)* ':' type ('=' expr)? ';'
|
||||
params = param (',' param)* ','?
|
||||
param = IDENT ':' type
|
||||
param = IDENT ':' type ('=' expr)?
|
||||
block = '{' stmt* '}'
|
||||
stmt = decl | assignment ';' | multi_assign ';' | return_stmt | defer_stmt | insert_stmt
|
||||
| push_stmt | break_stmt | continue_stmt | expr ';'
|
||||
|
||||
Reference in New Issue
Block a user