Repository navigation
Represent array<array<T, N>, M> as rows, not as an array of pointers (#575) - #578
Merged
Merged
Conversation
…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]>
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.
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:
int32_t** rows_flow_row_int32_t_3* rowsint32_t* rows[4]_flow_row_int32_t_3 rows[4](int32_t*[]){ I, V, vi, IV }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:
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 wantsint32_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:_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]onint32_t**is two loads; onint32_t (*)[3]it is one load at a computed offset. The indexing codegen already emittedrows[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:
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>declaredinlineand then referenced_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
pytest tests/./flow test --tier2 --strictruff check srcThree new tests: nested arrays built from literals and from row expressions, a real
progression_popreturning48 52 55, and the C-keyword name. Each asserts clang emits noreturn-stack-addresswarning, using the compiler's own diagnostic as ground truth.