Skip to content

Convert the inner arrays of a nested constant array literal too - #332

Merged
ASDAlexander77 merged 1 commit into
mainfrom
nested-const-array-cast
Sep 15, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
nested-const-array-cast

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

#330 rebuilds a constant array literal element by element when the element type has to change, but skipped a literal whose elements are arrays. Such a literal keeps s32 inside its inner arrays:

const c = [[1, 2]]; let u: number[][] = c;   // u[0][1] read as garbage (-1.#QNAN)
let v: number[][] | string = [[1, 2]];       // did not compile at all

The union case failed module verification:

'ts.Cast' op type ['!ts.const_array<!ts.array<si32>, 1>'] can't be stored in ['!ts.union<!ts.string, !ts.array<!ts.array<!ts.number>>>']

Flat literals (#330), and nested literals of floats or strings — where no element type changes — were already fine.

Fix

  • castConstArrayToArray turns a nested element back into a constant array and casts it in turn, so every level is rebuilt.
  • canWideTypeWithoutDataLoss compares the element type inside nested array elements, through the new canWideConstArrayElementWithoutDataLoss, and only for a constant array source. This is what castToTaggedUnionType asks when it looks for the union member to cast to; without it no member matched and a cast from the literal straight to the union survived into verification. An array that is not a literal is still not converted element-wise, as before.

Tests

New 00const_array_nested_elements.ts (compile, JIT and corpus variants): a nested const variable (outer and inner length, inner element, and that elements really are number), a nested literal assigned to a number[][] | string union, and a three-level literal.

Full Windows debug suite (ctest -R "^test-|^unittest-"): 2765/2765 passed.

🤖 Generated with Claude Code

@ASDAlexander77
ASDAlexander77 merged commit 2859391 into main Sep 15, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the nested-const-array-cast branch September 15, 2026 23:33
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