Skip to content

fix: emit const arrays as T name[N] not T* name in C backend - #1763

Open
godofecht wants to merge 4 commits into
mainfrom
cursor/fix-const-array-cgen-5b13
Open

godofecht wants to merge 4 commits into
mainfrom
cursor/fix-const-array-cgen-5b13

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Summary

Fixed a bug in flowc's C backend where top-level const array declarations were incorrectly emitted as pointers instead of arrays, causing segmentation faults when indexing.

Problem

Given a Flow program:

const NOTES: [i32; 4] = [36, 38, 42, 46]
function main() -> i32 {
    if NOTES[2] == 42 { return 0 }
    return 1
}

The C backend emitted:

static const int32_t* NOTES = { 36, 38, 42, 46 };

This initializes a pointer to address 0x24 (the integer 36), so indexing NOTES[2] causes a segfault.

Solution

Modified flowc_cgen_emit_const in compiler/src/cgen.flow to:

  1. Detect array types with fixed sizes (similar to how struct fields handle them)
  2. Emit the element type instead of the full array type
  3. Append the [N] suffix after the identifier

Generated C: Before and After

Before:

static const int32_t* NOTES = { 36, 38, 42, 46 };

After:

static const int32_t NOTES[4] = { 36, 38, 42, 46 };

Testing

Added comprehensive regression tests:

  • test_const_array_i32.flow - i32 array indexing
  • test_const_array_f32.flow - f32 element type
  • test_const_array_f64.flow - f64 element type
  • test_const_array_u64.flow - u64 element type
  • test_const_array_struct.flow - struct element type
  • test_const_array_import.flow - exported const arrays through module imports

All tests compile the generated C and verify correct indexing behavior.

Verification

  • ✅ All 337 language tests pass (./flow test-lang)
  • ✅ Bootstrap verification passes (./compiler/scripts/bootstrap_from_c.sh --verify)
  • ✅ Self-host full passes (./flow tool compiler/scripts/self_host_full.flow)
  • ✅ Roundtrip passes (./flow tool compiler/scripts/roundtrip.flow)
  • ✅ C generation tests pass (154/156, 2 unrelated explain report diffs)

Related

Found in flooooooooooow/flow-audio#61 where 808 and piano asset patches crashed with valgrind showing invalid read at address 0x24.

Open in Web Open in Cursor 

cursoragent and others added 3 commits November 23, 2026 15:27
The C backend incorrectly emitted top-level const array declarations
as pointers (const T* name) instead of arrays (const T name[N]),
causing segmentation faults when indexing.

This was because flowc_cgen_emit_type returns T* for array<T, N> types
(as documented in line 2717-2718 comment), and struct fields handle
the [N] suffix separately. Added similar logic to flowc_cgen_emit_const
to emit the element type and append [N] after the identifier.

Fixes indexing of const arrays like:
  const NOTES: [i32; 4] = [36, 38, 42, 46]

Before: static const int32_t* NOTES = { 36, 38, 42, 46 };
After:  static const int32_t NOTES[4] = { 36, 38, 42, 46 };

Tested with i32, f32, f64, u64, struct elements and module imports.

Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>
Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>
Tier 1 · macos-15 was cancelled without acquiring a hosted runner
(no steps ran). Empty commit to restart the workflow.

Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>
@cursor
cursor Bot enabled auto-merge November 23, 2026 16:14
Co-authored-by: Abhishek Shivakumar <abhishek.shivakumar@gmail.com>

This branch has not been deployed

No deployments
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.

2 participants