Skip to content

fix(codegen): better union-arm selection for object-literal return#518

Merged
cs01 merged 2 commits intomainfrom
struct-layout-p1
Apr 17, 2026
Merged

fix(codegen): better union-arm selection for object-literal return#518
cs01 merged 2 commits intomainfrom
struct-layout-p1

Conversation

@cs01
Copy link
Copy Markdown
Owner

@cs01 cs01 commented Apr 17, 2026

User impact

Functions returning a union of interfaces (Hit | Miss, etc.) now pick the correct arm when the returned object literal uses a subset of the arm's fields. Previously the heuristic matched on field-count equality, so any literal with fewer fields than the intended arm silently fell through to the first arm — producing wrong GEP indices and occasional crashes in downstream code.

Change

src/codegen/llvm-generator.ts (return-statement path): when the declared return type is a union and no discriminant match is found, search for the smallest arm whose declared field names contain every key of the returned literal. Exact-size superset wins outright. Fallback to prior first-non-null arm when no superset is found.

Part of the struct-layout-unification effort (plan phase P2). Self-hosting (stage 0/1/2) + full test suite green.

Test plan

  • added fixture: tests/fixtures/interfaces/union-return-subset-fields.ts — exercises subset-matching between Big (3 fields) and Small (1 field) arms
  • npm run verify — tests + stage 1 + stage 2 all green

…pick smallest superset by field set instead of count equality
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 17, 2026

Benchmark Results (Linux x86-64)

Benchmark C ChadScript Go Node Place
Binary Trees 1.085s 0.968s 2.066s 0.951s 🥈
Cold Start 0.6ms 0.7ms 0.9ms 21.4ms 🥈
Fibonacci 0.705s 0.706s 1.343s 2.608s 🥈
File I/O 0.073s 0.078s 0.073s 0.143s 🥉
JSON Parse/Stringify 0.003s 0.004s 0.012s 0.011s 🥈
Matrix Multiply 0.390s 0.914s 0.412s 0.345s #4
Monte Carlo Pi 0.341s 0.348s 0.355s 2.011s 🥈
N-Body Simulation 1.387s 1.746s 1.767s 1.845s 🥈
Quicksort 0.190s 0.218s 0.188s 0.232s 🥉
SQLite 0.250s 0.264s 0.314s 🥈
Sieve of Eratosthenes 0.011s 0.021s 0.016s 0.032s 🥉
String Manipulation 0.006s 0.015s 0.012s 0.031s 🥉

CLI Tool Benchmarks

Benchmark ChadScript grep node xxd Place
Hex Dump 0.345s 0.772s 0.109s 🥈
Recursive Grep 0.016s 0.008s 0.078s 🥈

…od/constructor args so object literals in scrambled field order get correct struct layout (#519)

Co-authored-by: cs01 <cs01@users.noreply.github.com>
@cs01 cs01 merged commit 3e8f803 into main Apr 17, 2026
6 checks passed
@cs01 cs01 deleted the struct-layout-p1 branch April 24, 2026 20:53
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