Repository navigation
A length holds and prints -1: index converts from a signed integer and to text as signed - #426
Merged
Merged
Conversation
…d to text as signed `let n = s.length; n = -1` printed 4294967295, and so did `n` as a number: the si32 -> index cast fell through to the zext in castLLVMTypesLogic, so -1 became 0x00000000FFFFFFFF. It now sign-extends. index -> string printed unsigned, while index already compares and converts to number as signed: `s.length - 3` printed 18446744073709551615 though `s.length - 3 < 0` was true. It now prints signed (-1). 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.
Follow-up to #425, which left
let x = p.length; x = -1;printing 4294967295.What was wrong
An
index(a length) already compares as signed (for (let k = s.length - 1; k >= 0; k--)stops) and converts tonumberas signed (sitofp). Two places disagreed:ZExtOpincastLLVMTypesLogic, son = -1stored0x00000000FFFFFFFF.n == -1was still true, since the literal was cast the same way, butconst v: number = nwas 4294967295. It now sign-extends.s.length - 3printed 18446744073709551615 thoughs.length - 3 < 0was true. It now prints-1.Before / after (
s = "ab",a = [1, 2]):let n = s.length; n = -1; print(n)const v: number = n`${s.length - 3}`let w = a.length; w -= 5const b: any = n; print(b)Trade-off: a length of 2^63 or more (2^31 on x86) would print negative. No real length gets that large.
Not changed
i32still prints unsigned (let i: i32 = 5; i = i - 6prints 4294967295).u32printing 4294967295 for5 - 6is correct.let y = 1; y = 0.5prints 0: integer literals ares32by design.Tests
00length_signed_arithmetic.ts(compile + jit): assignment, number / text /anyconversion, length subtraction,--,-=, countdown loop.🤖 Generated with Claude Code