fix(codegen): better union-arm selection for object-literal return#518
Merged
fix(codegen): better union-arm selection for object-literal return#518
Conversation
…pick smallest superset by field set instead of count equality
Contributor
Benchmark Results (Linux x86-64)
CLI Tool Benchmarks
|
4 tasks
…od/constructor args so object literals in scrambled field order get correct struct layout (#519) Co-authored-by: cs01 <cs01@users.noreply.github.com>
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.
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
tests/fixtures/interfaces/union-return-subset-fields.ts— exercises subset-matching between Big (3 fields) and Small (1 field) armsnpm run verify— tests + stage 1 + stage 2 all green