Repository navigation
Port GC heap to std/rt + typeck extern dedupe (Wave 1 unit 5) - #9
Merged
Merged
Conversation
Add std/rt/heap.ag with generational handles, free-list slot reuse, and mark-sweep collect_garbage(roots). Type checker now skips registering the same symbol id twice for identical extern redeclarations inlined from multiple imports (avoids phantom ambiguous-overload errors). Co-Authored-By: Claude Fable 5 <[email protected]>
Comment on lines
+389
to
+391
| // Invalidate all outstanding handles for this index (wrapping add). | ||
| self.generations.set(idx, self.generations.get(idx) + (u32)1); | ||
| self.free_list.push(handle.index); |
Contributor
There was a problem hiding this comment.
Generation bump comment promises wrapping, code does not
The inline comment says "wrapping add" but the expression self.generations.get(idx) + (u32)1 is ordinary u32 addition. If Silver applies overflow checks in debug builds (or panics on unsigned overflow in any build mode), a slot freed exactly 2^32 times would trap rather than wrap. The same pattern appears in the GC sweep at line 453. If the intent is a guaranteed modular wrap, an explicit wrapping primitive (or at minimum a % (u32::MAX + 1) note) would match the documented contract.
CierCier
added a commit
that referenced
this pull request
Aug 1, 2026
Port GC heap to std/rt + typeck extern dedupe (Wave 1 unit 5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Wave 1 unit 5 of
docs/runtime-migration.md.std/rt/heap.ag— generationalHandle, slot vector, free list,RtValue/Objecttagged structs (type ids as bareu32for now; unified with TypeId in unit 10),alloc/get/get_mut/free, mark-sweepcollect_garbage(roots)agc/src/semantic/typeck.rs— identical extern redeclarations no longer register the same symbol id twice (fixes phantomabort()ambiguity when modules redeclare the same prototype)tests/rt_heap_test.ag— 39 cases: generational safety, GC reachability, get_mut, slot reuseTest plan
agc tests/rt_heap_test.ag(with this typeck patch) → 39/39 PASScargo test -p agcfor typeck unit testsGreptile Summary
This PR introduces a generational mark-sweep GC heap (
std/rt/heap.ag) as a Silver port ofsilver_runtime/src/runtime/heap.rs, and fixes a type-checker bug where identicalextern "C"redeclarations from multiple imported modules caused phantom overload-resolution ambiguities.std/rt/heap.ag: AddsHandle,RtValue,Object, andHeapwithalloc/get/get_mut/free/collect_garbage. The implementation uses five parallel slot vectors (objects,occupied,marked,generations,free_list) instead of Rust'sVec<Option<Entry>>, with explicit ownership managed through Silver's mandatory-drop pattern.agc/src/semantic/typeck.rs: Changesfunction_symbols.inserttoentry().or_insert_with()and guardsoverloads.pushwith acontainscheck, preventing duplicatesymbol_identries that caused a function to appear as a phantom self-ambiguous overload.tests/rt_heap_test.ag: 39 test cases covering generational stale-handle rejection, GC reachability tracing through composite objects,get_mutmutation visibility, slot reuse, andallocated_countbookkeeping.Confidence Score: 4/5
Safe to merge for integration; the one open concern is that generation counters in both
free()and the GC sweep use plain addition rather than a guaranteed-wrapping operation, leaving the slot-reuse safety guarantee dependent on build-mode overflow behaviour.The GC algorithm and ownership model are correct, the typeck dedupe fix is sound, and the 39-case test suite gives solid coverage. The generation counter arithmetic (noted in a prior thread) is the remaining unresolved point: if overflow checks fire in any build configuration, a slot freed exactly 2^32 times would trap rather than wrap as the comment promises, and if they are silently truncated the ABA window reappears at that boundary.
std/rt/heap.ag — specifically the generation bump expressions in
free()(line 433) and the GC sweep (line 453).Important Files Changed
function_symbolsnow usesentry().or_insert_with()(idempotent, keeps first FunctionSig) andoverloadsguards withcontainsbefore push. Logic is correct for the documented use case.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A["alloc(object)"] --> B{free_list empty?} B -- No --> C["pop index from free_list\nread bumped generation\nset objects[idx]\nset occupied[idx]=true\nset marked[idx]=false"] B -- Yes --> D["append object\nappend occupied=true\nappend marked=false\nappend generation=0"] C --> E["return Handle{index, generation}"] D --> E F["get / get_mut(handle)"] --> G{handle_is_valid?} G -- No --> H["ObjectRef { ok=false, ptr=null }"] G -- Yes --> I["ObjectRef { ok=true, ptr=&objects[idx] }"] J["free(handle)"] --> K{handle_is_valid?} K -- No --> L["FreeResult { ok=false, empty Object }"] K -- Yes --> M["objects.take(idx)\noccupied[idx]=false\ngenerations[idx]+=1\nfree_list.push(idx)"] M --> N["FreeResult { ok=true, object }"] O["collect_garbage(roots, len)"] --> P["clear marked[0..slot_count]"] P --> Q["push root handles onto work stack"] Q --> R{stack empty?} R -- No --> S["pop handle\nvalidate index+generation+occupied+!marked\nmark slot\npush children"] S --> R R -- Yes --> T["sweep: drop+free all occupied && !marked\nbump generation, push to free_list"] T --> U["return freed count"]%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% flowchart TD A["alloc(object)"] --> B{free_list empty?} B -- No --> C["pop index from free_list\nread bumped generation\nset objects[idx]\nset occupied[idx]=true\nset marked[idx]=false"] B -- Yes --> D["append object\nappend occupied=true\nappend marked=false\nappend generation=0"] C --> E["return Handle{index, generation}"] D --> E F["get / get_mut(handle)"] --> G{handle_is_valid?} G -- No --> H["ObjectRef { ok=false, ptr=null }"] G -- Yes --> I["ObjectRef { ok=true, ptr=&objects[idx] }"] J["free(handle)"] --> K{handle_is_valid?} K -- No --> L["FreeResult { ok=false, empty Object }"] K -- Yes --> M["objects.take(idx)\noccupied[idx]=false\ngenerations[idx]+=1\nfree_list.push(idx)"] M --> N["FreeResult { ok=true, object }"] O["collect_garbage(roots, len)"] --> P["clear marked[0..slot_count]"] P --> Q["push root handles onto work stack"] Q --> R{stack empty?} R -- No --> S["pop handle\nvalidate index+generation+occupied+!marked\nmark slot\npush children"] S --> R R -- Yes --> T["sweep: drop+free all occupied && !marked\nbump generation, push to free_list"] T --> U["return freed count"]Reviews (3): Last reviewed commit: "Merge origin/main into std/rt-gc-heap an..." | Re-trigger Greptile