Commit Graph
3 Commits
Author SHA1 Message Date
Malte UblandClaude Opus 5.5 887aa774e0 Build an empty array literal in a union of array slots at its first arm
`const message: M = { content: [] }` with `content: TextContent[] |
ImageContent[]` threw at run time, `a 'number[]' value is not
representable in the target union`. tsc types an empty literal `never[]`,
whose element has no representation of its own: the literal mapped through
never's f64 element as `number[]`, then the union slot's retag found no
width lift into either array arm and took the stranded-source trap.

An empty array carries no element to select an arm by, and it is the same
value whichever array family stores it, so the literal is built as the
union's FIRST array arm — the rule a lone array slot already states, and
the one the ternary arm and the `?? []` default apply. Both the IR slot
(lowerExprExpecting) and the literal's own type mapping (lowerArrayLiteral)
now state it for any number of array arms; a single array arm keeps the
identical lowering it had. A `never[]` VALUE (an explicitly declared
variable) still maps to `number[]` and keeps its refusal: mapping never's
element globally is a different change.

Corpus: tests/corpus/empty-array-union-slot.ts pins the nested record
field, declarations and reassignment, declared and async returns, ternary
arms (one and both empty), arguments and default parameters, deeper
nesting, a class field, an optional arm, array/Set/Map element slots, a
callback argument, an assertion, and a fresh array per evaluation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 19:11:34 -07:00
Malte UblandClaude Opus 5.5 378901df95 Convert every arm of a mixed union into the array width
`["one"] | string[]` flowing into a `string[]` slot compiled to the
checked extraction against the array arm alone, so a tuple value threw at
runtime. Node accepts it: the value is an array, and nothing about a
`string[]` position distinguishes which arm produced it.

Let the union-width plan cover a mixed union that holds the destination
arm beside a convertible one, and place that plan before the
checked-extraction rule, so each arm converts on its tag (a tuple rebuilds,
an array copies). A union whose only non-unit arm is the destination
(`T[] | undefined`) still has nothing to convert and keeps its tested
extraction.

tests/corpus/8513-tuple-union-array.ts pins the binding, call-argument,
readonly, and single-tuple shapes against Node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 09:37:11 -07:00
Malte UblandClaude Opus 5.5 4c2adcf123 Convert a union of tuples into an array slot
A value typed as a union of tuples or arrays, such as the `as const`
key-table read `SEQUENCES[functionKey]` in pi, was refused with SC2003
when it flowed into a `string[]` or `readonly string[]` slot. A single
tuple already converted through the width family's tuple-to-array
rebuild; a union of tuples had no plan at all.

widthLiftPlan gains the unionArr step for an array destination: every
non-unit arm must lift on its own tag (a tuple rebuilds through
tupleArr, an array copies or lifts through arr), and a unit arm takes
the stranded TypeError, since the slot has no representation for it.
%union.arr dispatches on the runtime tag and applies each arm's lift;
applyWidthLift and one coerceToExpected hook wire it into the top-level
edges that the nested slots already reach through the plan.

The step is planned only after the union-into-one-of-its-arms rule and
only when no arm of the source is already the destination, so every
union that converted before - `T[] | undefined` into `T[]`, optional
array fields, `??=` - keeps byte-identical IR; only sources with no
conversion at all gain one.

Adds the differential corpus program, its cost file, an IR unit test,
and the order-parity baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:44:46 -07:00