Repository navigation
Return array<T, N> by value instead of pointing into the dead frame (#573) - #576
Merged
Merged
Conversation
…573) function triad(root: i32) -> array<i32, 3> { return [root, root + 4, root + 7] } returned garbage. `-> array<T, N>` lowered to `T*` over automatic storage: int32_t* triad_i32(int32_t root) { return (int32_t[]){ root, (root + 4), (root + 7) }; } memcpy(chord, triad_i32(60), sizeof(chord)); // reads a dead frame No crash and no diagnostic from Flow, though clang printed -Wreturn-stack-address on every build. All 25 functions in lib/stdlib/ returning a fixed-size array were affected; lib/stdlib/audio/scales.flow was entirely non-functional. The value now travels home inside a generated wrapper: typedef struct { int32_t v[3]; } _flow_arr_int32_t_3; Consumers read `.v`, which decays to the same `int32_t*` the old signature produced, so the memcpy at each declaration and assignment is unchanged, and so are indexing a call result and passing one along. The rewrite sits in the single FunctionCall branch of _gen_expr rather than at each consumer. C11 6.2.4p8 gives the returned struct a lifetime through the enclosing full expression, which covers `memcpy(dest, f().v, sizeof(dest))` and `f(x).v[0]`. Nested `array<array<T, N>, M>` is excluded. It is an array of pointers everywhere in this generator, so wrapping only its return would still hand back pointers into the dead frame. Behaviour there is unchanged rather than half-changed; filed as #575 with what the representation needs first. The one existing test, tests/arrays/test_array_return.flow, returns an unsized array<f32>, which is heap allocated and therefore safe, and never reads a value back. The new tests read the values, and assert clang emits no return-stack-address warning. Closes #573 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
godofecht
added a commit
that referenced
this pull request
Aug 19, 2026
The xfail asserted that -> array<T, N> stops returning stack memory. #576 makes that true, so keeping it here would XPASS and fail the build the moment that lands. The real regression test lives with the fix, in tests/integration/test_array_return.py, and reads the values back. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
godofecht
added a commit
that referenced
this pull request
Aug 19, 2026
The xfail asserted that -> array<T, N> stops returning stack memory. #576 makes that true, so keeping it here would XPASS and fail the build the moment that lands. The real regression test lives with the fix, in tests/integration/test_array_return.py, and reads the values back. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
godofecht
added a commit
that referenced
this pull request
Aug 19, 2026
* Make lib/stdlib/audio/README.md describe the API that exists
Every code block in this README failed to compile. Three sections named
functions that were never written:
gpu_process_buffer(input, effect_kernel) -> audio_gain_block(backend, buf, gain)
simd_gain_f32x8(samples, gain) -> audio_gain_interleaved_f32(...)
graph_new(); graph_add_oscillator(...) -> effect_chain_new(rate); effect_chain_*
The graph section described a node-and-edge API. What exists is a value:
each stage returns a new EffectChain, so the section is rewritten around
that rather than renamed onto it.
pattern_4 and euclidean_pattern were called with a trailing voice argument.
They take no such argument; pattern_4 always plays through bass and
pattern_8 through lead, and the comment in livecode.flow ("caller can modify
this pattern if needed") shows the parameter was intended and never added.
The README now says which voice each one uses.
Five blocks used `clock` without declaring it, and two used both `clock` and
`pos` at file scope with no enclosing function. A reader copying any of them
got the same errors CI now catches.
The GPU block is tagged ignore= rather than fixed: it links
runtime/audio_gpu_metal.m, which the doc example build does not compile. The
prose now gives the clang line, which it did not before.
unverified: 235 -> 224.
This turned up issue #573. lib/stdlib/audio/scales.flow returns fixed-size
arrays from every function, and `-> array<T, N>` lowers to a pointer into
the callee's dead frame, so all 25 such functions in the stdlib return
garbage. Added as a strict xfail in test_coverage_known_bugs.py so it
reports the moment it is fixed. The blocks here compile, which is all the
doc checker asserts; the one that also runs a scale function still prints
garbage until #573 is fixed.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
* Drop the #573 xfail: PR #576 fixes the bug rather than recording it
The xfail asserted that -> array<T, N> stops returning stack memory. #576
makes that true, so keeping it here would XPASS and fail the build the moment
that lands. The real regression test lives with the fix, in
tests/integration/test_array_return.py, and reads the values back.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
* Regenerate the example ledger after rebasing onto the occurrence keys
#572 changed the key format and #574 changed which blocks pass, so the
checked-in ledger conflicted. Regenerated rather than hand-merged.
unverified: 235 -> 221 with both changes applied.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
---------
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 #573.
The bug
returned garbage.
-> array<T, N>lowered toT*over automatic storage:No crash and no diagnostic from Flow, though clang printed
-Wreturn-stack-addresson every build. All 25 functions inlib/stdlib/returning a fixed-size array were affected.lib/stdlib/audio/scales.flowwas entirely non-functional:scale_major(48)returned garbage rather than48 50 52 53 55 57 59.The fix
The value travels home inside a generated wrapper:
Consumers read
.v, which decays to the sameint32_t*the old signature produced. That is what makes this small: thememcpyat each declaration and each assignment is untouched, and so are indexing a call result and passing one to another function. The rewrite lives in the singleFunctionCallbranch of_gen_exprrather than at every consumer.C11 6.2.4p8 gives the returned struct a lifetime through the enclosing full expression, which is what
memcpy(dest, f().v, sizeof(dest))andf(x).v[0]need.What is deliberately not fixed
Nested
array<array<T, N>, M>is excluded. It is represented as an array of pointers everywhere in this generator, so wrapping only its return would give backstruct { int32_t* v[4]; }: still four pointers into the dead frame._sized_array_typeskips it, so behaviour there is unchanged rather than half-changed. Filed as #575 with what the representation needs first. Three functions are affected, allprogression_*inscales.flow.Why the suite was green
tests/arrays/test_array_return.flowis the only test for this and does not exercise the broken path:array<f32>is unsized and heap allocated, so returning it is safe. It also never reads a value back, onlyprint("Test complete").The new tests in
tests/integration/test_array_return.pyread the values, cover returning a literal, returning a local, indexing a call result and passing one along, check a real stdlib scale and triad, and assert clang emits noreturn-stack-addresswarning. That last one uses the compiler's own diagnostic as ground truth rather than depending on what the garbage happens to be.Verification
pytest tests/./flow test --tier2 --strictruff check srcFound by compiling the examples in
lib/stdlib/audio/README.md.