Skip to content

Represent array<array<T, N>, M> as rows, not as an array of pointers (#575) - #578

Merged
godofecht merged 2 commits into
mainfrom
fix-nested-arrays
Aug 19, 2026
Merged

godofecht merged 2 commits into
mainfrom
fix-nested-arrays

Conversation

@godofecht

@godofecht godofecht commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #575. Follow-up to #573, which fixed the single-level case and deliberately left this one alone.

The bug

A nested sized array was an array of pointers everywhere:

Position Was Now
parameter int32_t** rows _flow_row_int32_t_3* rows
local int32_t* rows[4] _flow_row_int32_t_3 rows[4]
literal (int32_t*[]){ I, V, vi, IV } per-row copy, or a brace initializer
return int32_t** struct { _flow_row_int32_t_3 v[4]; }

Every one of those pointers aimed at a local in the frame that built them:

before:  first chord: 1805590204 1 1805590192
after:   first chord: 48 52 55

Why a typedef

C cannot spell "array of arrays" in the places this generator needs it without declarator gymnastics: a parameter wants int32_t (*)[3], a local wants int32_t x[4][3], a struct member wants the same again, and none of those compose with a _c_type() that returns a plain string. One row typedef makes all three ordinary:

typedef int32_t _flow_row_int32_t_3[3];

_flow_row_int32_t_3 rows[4] is a real 2D array, _flow_row_int32_t_3* is what it decays to, and #573's return wrapper now carries rows instead of pointers.

This is a correctness change and not only a spelling one. rows[0][0] on int32_t** is two loads; on int32_t (*)[3] it is one load at a computed offset. The indexing codegen already emitted rows[0][0] textually, so it was silently wrong for the pointer type and is right for this one.

Literals

A literal whose rows are themselves literals becomes a brace initializer:

_flow_row_int32_t_2 inline[3] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

A literal whose rows are expressions, as in return [I, V, vi, IV], cannot be a compound literal at all without reintroducing the pointers, so each row is copied into place.

An unrelated bug this turned up

The sized-array declaration used the raw Flow name while every use site sanitized it, so let inline: array<array<i32, 2>, 3> declared inline and then referenced _flow_inline:

error: use of undeclared identifier '_flow_inline'

Pre-existing, and reachable by any array named after a C keyword. Fixed here with a test, since it was in the block I was already changing.

Verification

Check Result
pytest tests/ 1663 passed, 6 skipped
./flow test --tier2 --strict 687/687
ruff check src clean

Three new tests: nested arrays built from literals and from row expressions, a real progression_pop returning 48 52 55, and the C-keyword name. Each asserts clang emits no return-stack-address warning, using the compiler's own diagnostic as ground truth.

godofecht and others added 2 commits August 19, 2026 11:44
…575)

Follow-up to #573, which fixed the single-level case and deliberately left
this one alone.

A nested sized array was an array of pointers everywhere: `int32_t**` as a
parameter, `int32_t* rows[4]` as a local, `(int32_t*[]){ I, V, vi, IV }` as a
literal. Every one of those pointers aimed at a local in the frame that built
them, so progression_pop(note_C(4))[0] read garbage:

    first chord: 1805590204 1 1805590192      # expected 48 52 55

C cannot spell "array of arrays" in the places this generator needs it
without declarator gymnastics: a parameter wants `int32_t (*)[3]`, a local
wants `int32_t x[4][3]`, a struct member wants the same again. A row typedef
makes all three ordinary:

    typedef int32_t _flow_row_int32_t_3[3];

So `_flow_row_int32_t_3 rows[4]` is a real 2D array, `_flow_row_int32_t_3*`
is what it decays to, and #573's return wrapper now carries rows rather than
pointers. This is also a correctness change and not only a spelling one:
`rows[0][0]` on `int32_t**` is two loads, on `int32_t (*)[3]` it is one load
at a computed offset.

A literal whose rows are themselves literals becomes a brace initializer. A
literal whose rows are expressions, as in `return [I, V, vi, IV]`, cannot be
a compound literal at all, so each row is copied into place instead.

Also fixes a name-mangling mismatch this turned up: the sized-array
declaration used the raw Flow name while every use site sanitized it, so
`let inline: array<...>` declared `inline` and referenced `_flow_inline`.
Pre-existing, and reachable by any array named after a C keyword.

Closes #575

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@godofecht
godofecht merged commit 00e3cf7 into main Aug 19, 2026
18 checks passed
@godofecht
godofecht deleted the fix-nested-arrays branch August 19, 2026 11:15
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.

Nested array<array<T, N>, M> is an array of pointers, so it cannot be returned or stored safely

1 participant