Repository navigation
Widen an s32[] to number[] from what it holds, not from its literal - #354
Merged
Merged
Conversation
castConstArrayToArray traced an array load back to the variable's
initializer (or the global's initializer region) and rebuilt the array
from that literal. Every push, element write, reassignment or sort made
since the declaration was silently lost:
let a = [1, 2]; a.push(3); a[0] = 9;
const b: number[] = a; // b was [1, 2]
The trace-back is gone. An array whose elements are integers or floats
(nested arrays too) read as number[], a union holding a number, or any[]
is now converted at run time by castArrayElementwise: a new array of the
same length, filled by a synthesized loop whose element assignment does
the cast. A real const_array literal still takes the constant path. This
also makes a plain s32[] parameter widen, which was a compile error.
NewArray now allocates zeroed storage. Under -mm=rc the first store into
an element releases what the slot held, and malloc's leftovers were
released as references - the conversion loop into any[] or
(number | string)[] crashed on the first element. gc was unaffected
because GC_malloc zeroes.
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.
Summary
Reading an
s32[]asnumber[]silently dropped every change made since the declaration:castConstArrayToArray(from #344) followed an array load back to the variable's initializer, or the global's initializer region, and rebuilt the array from that literal. Push, element writes, reassignment and sort were all lost. The module-level path had the same fault.castArrayElementwisehandles an array whose elements are integers or floats (nested arrays included) when it is read asnumber[], a union holding a number, orany[]. It builds a new array of the same length at run time, and a synthesized loop does the element cast as each element is assigned. A realconst_arrayliteral still takes the constant path. Ans32[]parameter read asnumber[]now compiles too; it used to be an error.NewArrayallocates zeroed storage. Under-mm=rcthe first store into an element releases what the slot held, somalloc's leftovers were released as references. The conversion loop intoany[],(number | string)[]ornumber[][]crashed on its first element. gc was unaffected becauseGC_malloczeroes. This also coversnew Array(n)under rc.The widened array is a copy, so a later write to one is not seen through the other. Converting a literal already worked this way.
Verification
00array_widen_elements_now.ts(compile, jit, and the rc/none corpus). It covers let, const, reassignment, a parameter, nested arrays, union andanytargets, an empty array, copy semantics, and module-level arrays. Onmainit fails to compile, and the stale-value cases print the old literal.Found while testing, not changed here:
const nested = [[1, 2], [3]]; nested[1].push(4)crashes inGC_realloc, onmainas well.🤖 Generated with Claude Code