Repository navigation
A union of literals takes part in arithmetic as its literals' type - #560
Merged
Merged
Conversation
`c ? 0.5 : 1.0` has the type `0.5 | 1`, a union of literals that needs no tag. A binary operator unwrapped a single literal type to its element type, but cast such a union to the other operand's type, so with an integer on the other side the fraction was truncated: `int * (c ? 0.5 : 1.0)` multiplied two s32s and gave 0, and `const left = c ? 448.0 : 96.0; let m = left * 2; m = m / 3` gave 298. A union whose members are all literals of one type is now unwrapped to that type first, as a single literal is, and the operands promote as usual. Found while porting native_ts_graphics samples (TSLANG_ISSUES.md 8). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
added a commit
that referenced
this pull request
Oct 13, 2026
`c ? 1 : 0` is the union `0 | 1`, stored untagged as s32. Cast straight to another type (an `index` variable, a parameter, a tuple field) it reached the lowering as a union, and `llvm.sext` of the union failed with no source location. castFromSourceSpecialCases now casts such a union to its literals' type first, as #560 did for binary operators. Found writing native_ts_graphics' graphics pipeline (TSLANG_ISSUES.md 14). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
added a commit
that referenced
this pull request
Oct 13, 2026
`c ? 1 : 0` is the union `0 | 1`, stored untagged as s32. Cast straight to another type (an `index` variable, a parameter, a tuple field) it reached the lowering as a union, and `llvm.sext` of the union failed with no source location. castFromSourceSpecialCases now casts such a union to its literals' type first, as #560 did for binary operators. Found writing native_ts_graphics' graphics pipeline (TSLANG_ISSUES.md 14). Co-authored-by: Claude Opus 5.5 <noreply@anthropic.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.
c ? 0.5 : 1.0has the type0.5 | 1, a union of literals that needs no tag. A binary operator unwrapped a single literal type to its element type, but cast such a union to the other operand's type, so with an integer on the other side the fraction was truncated:unwrapForBinaryOpnow unwraps a union whose members are all literals of one type to that type, as it already did for a single literal; mixed unions are left alone.Found while porting native_ts_graphics samples (TSLANG_ISSUES.md 8).
Tests
tslang/test/tester/tests/00literal_union_arith.ts(compile, JIT and corpus): int and number operands, through a variable,+/-, comparisons, string unions.🤖 Generated with Claude Code