Skip to content

fix: native compiler segfault on unknown new expression at module scope#516

Merged
cs01 merged 1 commit intomainfrom
fix-date-segv
Apr 17, 2026
Merged

fix: native compiler segfault on unknown new expression at module scope#516
cs01 merged 1 commit intomainfrom
fix-date-segv

Conversation

@cs01
Copy link
Copy Markdown
Owner

@cs01 cs01 commented Apr 17, 2026

User impact

Before this PR, const x = new UnknownClass() at module scope caused the native self-hosted compiler (.build/chad) to segfault silently. After this PR, it emits a compile error and exits cleanly.

Root cause

VariableDeclaration creation sites had inconsistent field counts across parsers:

  • 4-field sites (destructuring): { type, kind, name, value }
  • 5-field sites (normal): { type, kind, name, value, declaredType }
  • 6-field sites (statements.ts): { type, kind, name, value, declaredType, loc }

Per CLAUDE.md rule #3, the native compiler infers struct layouts from creation sites. Inconsistent field sets caused GEP misalignment — reading stmt.value returned a wrong pointer, and subsequent field accesses on the misaligned expression struct crashed.

Fix

  1. Aligned ALL VariableDeclaration creation sites to include all 6 fields (adding declaredType: undefined, loc: undefined where missing)
  2. Changed the inline type assertion in generateGlobalVariableDeclarations to use the real VariableDeclaration type (per rule Add cross compilation support #5: never invent subset types for assertions)

What this does NOT fix

  • new Date() at module scope still segfaults — requires aligning NewNode creation sites, but doing so shifts the overall union struct layout and undoes the VariableDeclaration fix (cascading struct-layout interference)
  • instanceof with class hierarchies still segfaults — same systemic issue
  • These need a deeper fix: making the native compiler unify struct layouts from interface definitions rather than creation sites

Test plan

  • tests/fixtures/edge-cases/new-unknown-class-module-scope.ts — new fixture
  • npm run verify:quick — all tests + stage 1 self-hosting pass

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark Results (Linux x86-64)

Benchmark C ChadScript Go Node Place
Binary Trees 1.572s 1.276s 2.691s 1.282s 🥇
Cold Start 1.0ms 0.9ms 1.3ms 31.3ms 🥇
Fibonacci 0.911s 0.794s 1.733s 3.367s 🥇
File I/O 0.087s 0.092s 0.086s 0.171s 🥉
JSON Parse/Stringify 0.004s 0.006s 0.016s 0.015s 🥈
Matrix Multiply 0.503s 0.665s 0.530s 0.479s #4
Monte Carlo Pi 0.440s 0.439s 0.457s 2.591s 🥇
N-Body Simulation 1.748s 2.254s 2.290s 2.403s 🥈
Quicksort 0.246s 0.283s 0.242s 0.290s 🥉
SQLite 0.326s 0.342s 0.430s 🥈
Sieve of Eratosthenes 0.014s 0.027s 0.020s 0.042s 🥉
String Manipulation 0.008s 0.019s 0.016s 0.038s 🥉

CLI Tool Benchmarks

Benchmark ChadScript grep node xxd Place
Hex Dump 0.446s 1.043s 0.139s 🥈
Recursive Grep 0.021s 0.011s 0.101s 🥈

@cs01 cs01 merged commit 00ce0a6 into main Apr 17, 2026
13 checks passed
@cs01 cs01 deleted the fix-date-segv branch April 17, 2026 06:56
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