Skip to content

A union of literals takes part in arithmetic as its literals' type - #560

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-native-ts-graphics-issues
Oct 12, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-native-ts-graphics-issues

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

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:

let c = true;
const width = 100;                     // s32
console.log(width * (c ? 0.5 : 1.0));  // was 0, now 50
const left = c ? 448.0 : 96.0;
let m = left * 2;
m = m / 3;
console.log(m);                        // was 298, now 298.667

unwrapForBinaryOp now 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

  • New tslang/test/tester/tests/00literal_union_arith.ts (compile, JIT and corpus): int and number operands, through a variable, +/-, comparisons, string unions.
  • Full suite (windows-msbuild-2026-release): 4032/4032 passed (1 already disabled).

🤖 Generated with Claude Code

`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
ASDAlexander77 merged commit fee4226 into main Oct 12, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-native-ts-graphics-issues branch October 12, 2026 18:19
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>
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