Skip to content

fix set heap allocation and require explicit type args#69

Merged
cs01 merged 1 commit intomainfrom
fix/set-heap-allocation
Feb 27, 2026
Merged

fix set heap allocation and require explicit type args#69
cs01 merged 1 commit intomainfrom
fix/set-heap-allocation

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Feb 27, 2026

Summary

  • Fix segfault in native compiler on resp.json<T>() / results[0].json<T>(): %Set and %StringSet structs were stack-allocated (alloca) inside their generator functions, then stored in class fields. After the constructor returned, the pointer was dangling — dereferencing it in generateTypedJson caused SIGSEGV.
  • Heap-allocate Set structs: switched generateSetLiteral and generateEmptyStringSet from alloca to GC_malloc(16), matching JavaScript's heap semantics for new Set().
  • Fix Set<string> type mismatch: new Set() without explicit type args was creating %Set* (double-backed) even when the field was declared Set<string> (%StringSet*). handleClassFieldAssignment now propagates currentDeclaredSetType before evaluating the RHS so the right generator is selected.
  • Error on untyped Set construction: new Set() without a type argument now throws in codegen (semantic analysis already catches this, but the codegen path is now explicit too).
  • Add parallel.ts to example runner: scripts/run-examples.sh now includes parallel HTTP fetch as test [9/9] with a 30s watchdog timeout.

Test plan

  • node ./dist/chad-node.js run examples/parallel.ts — still works (JS path)
  • ./.build/src/chad-native run examples/parallel.ts — no longer segfaults, prints Vue/React star counts
  • ./.build/src/chad-native run /tmp/test_json2.ts — simple resp.json<Repo>() works
  • new Set() without type arg produces error (sema + codegen)
  • new Set<string>() and new Set<number>() still work
  • bash scripts/run-examples.sh — all 9 tests pass

set/stringset structs were stack-allocated (alloca) and stored in class
fields, causing dangling pointers and segfaults when accessed after the
constructor returned. switched to gc_malloc for heap allocation.

also propagate declared set type through class field assignment so
new Set<string>() always creates %StringSet* instead of %Set*. added
error in codegen for new Set() without explicit type args (sema already
catches this but codegen path is now explicit too).

fixes native compiler segfault on resp.json<T>() and results[0].json<T>().
adds parallel.ts as example test [9/9] in run-examples.sh.
@cs01 cs01 force-pushed the fix/set-heap-allocation branch from 88acf35 to 1630356 Compare February 27, 2026 07:42
@cs01 cs01 merged commit 921796a into main Feb 27, 2026
12 checks passed
@cs01 cs01 deleted the fix/set-heap-allocation branch February 27, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant