Cast a constant array literal to another element type element by element - #330
Open
ASDAlexander77 wants to merge 1 commit into
Open
ASDAlexander77 wants to merge 1 commit into
ASDAlexander77 wants to merge 1 commit into
Conversation
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.
Problem
A constant array keeps the element type its literal was built with.
const c = [1, 2]is aconst_array<si32>, and so is[1, 2]given to a union such asnumber[] | string, where no single array type guides the literal. Casting it tonumber[]produced a barets.Cast, and lowering turns a const array into an array by copying its data as it is, sonumberelements were read out ofs32data:The compiler printed only a warning ("invalid cast", or "source array and destination array have different types" followed by one) and exited 0, for a local initializer, an assignment and a function argument alike. Literals that do have a single target type (
let u: number[] = [1, 2],f([1, 2])withf(u: number[]),<number[]>[1, 2]) were already fine: their elements are cast while the literal is built.Fix
New
castConstArrayToArrayincast(): a constant array cast to an array of another element type is built withCreateArrayfrom its elements, each cast to the target element type (and retained for the data block, as array literals do). Arrays whose element type already matches keep the existing copy.Not changed:
let u: number[][] | string = [[1, 2]]) still fail module verification. That is a loud error, not a miscompile.undefresult inCastLogicHelper.his left as it is; its TODO says conditional compiling relies on it.Tests
New
00const_array_to_array_elements.ts(compile, JIT and corpus variants): a const variable tonumber[], and a union initializer, assignment and parameter, checking length, elements and fractional arithmetic on the converted elements.Full Windows debug suite (
ctest -R "^test-"): 2757/2757 passed.🤖 Generated with Claude Code