Skip to content

fix(flowc): C emitter bugs for imports, deref, &call, statics - #1081

Closed
godofecht wants to merge 9 commits into
mainfrom
cursor/fix-flowc-bugs-b994
Closed

godofecht wants to merge 9 commits into
mainfrom
cursor/fix-flowc-bugs-b994

Conversation

@godofecht

@godofecht godofecht commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a batch of Stage-A flowc bugs found while porting tools to Flow.

C emitter / parser

Resolve / typecheck / diagnostics

Tests

HOST=$(./compiler/scripts/flowc_host.sh)
for f in tests/lang/test_import_array_field_copy.flow \
         tests/lang/test_addr_of_call.flow \
         tests/lang/test_module_ptr_field.flow \
         tests/lang/test_ptr_deref_star.flow \
         tests/lang/test_deref_store_statement.flow \
         tests/lang/test_array_literal_value.flow \
         tests/lang/test_static_ptr_field.flow \
         tests/lang/test_static_span_null_forms.flow \
         tests/lang/test_sizeof_builtin.flow \
         tests/lang/test_ptr_arg_struct_reject.flow \
         tests/lang/test_bundle_path_norm.flow; do
  FLOWC_BUNDLE=1 FLOWC_DIR=tests/lang FLOWC_IN=$f FLOWC_OUT=/tmp/t.c $HOST \
    && cc -O0 -Itests/lang -o /tmp/t /tmp/t.c -lm && /tmp/t
done

Bootstrap C regeneration follows in a separate commit.

Fixes #1074
Fixes #1066
Fixes #1078
Fixes #1046
Fixes #1048
Fixes #1045
Fixes #1073
Fixes #952
Fixes #1080
Fixes #986

Open in Web Open in Cursor 

cursoragent and others added 9 commits September 30, 2026 09:24
- #1074: struct-literal array field copy across modules uses memcpy
  when the struct def is not in the current arena
- #1066: &f() spills the call result to a temporary before taking address
- #1078: field read through a module-level ptr<T> emits ->
- #1046: parse *p = v as a deref-store statement
- #1050: array literals in value position get a compound-literal type
- #1048: null for span statics (covered by static span null forms test)

Co-authored-by: Abhishek Shivakumar <[email protected]>
- #1073: normalize bundle import paths so ../x and x are one module
- #952: sizeof<T>() is a sem_check builtin (mangled sizeof_T)
- #1080: reject struct value where ptr<T> is expected under typecheck
- #986: parse error for `else if` suggests `elif`

Co-authored-by: Abhishek Shivakumar <[email protected]>
Array and pointer element types were unknown to expr_prim, so
`s + frequency[0]` passed a float into __flowc_str_concat. Resolve the
element type of AST_INDEX and format it like other numeric operands.

Co-authored-by: Abhishek Shivakumar <[email protected]>
…en stored

The path fold added for #1073 ran inside flowc_resolve_append_path, so the
module store held folded paths while flowc_resolve_deps_ready and
flowc_resolve_topo still looked deps up by the path
flowc_resolve_import_at had written. `import "helper.flow"` from a module in
the search directory resolves to `./helper.flow`, was stored as
`helper.flow`, and the lookup for `./helper.flow` missed. The ordering pass
read that as "this dep is not in the bundle", so a module was emitted before
the module it imports and the bundle typechecker reported the imported
functions as undefined. 98 of the 234 programs under tests/lang stopped
building; 87 of them are plain two-module imports.

Fold at the single point where an import becomes a path instead, the tail of
flowc_resolve_import_at, so the store and every lookup see one form. The
fold inside append_path stays and is now idempotent.

tests/lang/test_import_path_spelling.flow reaches one module by three
spellings, through a module that imports it with a fourth. It fails on main
with `redefinition of ips_dep_val` (the bug #1073 fixed) and failed here
with `Undefined function ips_mid_val` (the ordering regression).

Refs #1073

Co-authored-by: Abhishek Shivakumar <[email protected]>
…1080

Two programs that build on main stopped building on this branch.

cgen, struct literals. The #1074 fallback treats a field whose destination
type is not in this module's arena as a possible array and memcpy's it. An
enum's literal takes that path, because an enum is not an AST_STRUCT, so
`Kind { tag: Kind_A }` emitted `memcpy(&sl.tag, &(Kind_A), ...)` and cc
refused to take the address of an enum constant. Require a plain name to
resolve to a variable before copying it; a field access or an index still
does, which is the #1074 case (`Lat { k: a.k }`).

sem_check, method receivers. #1080 dropped the blanket struct-to-ptr
coercion so `take(m)` with `take(m: ptr<M>)` errors instead of emitting C
that cc rejects. The note left the method-receiver case to #1009, but
nothing implemented it, so `c.bump(4)` with `bump(c: ptr<Counter>, ...)`
became "No matching overload". Accept a struct value for the receiver slot
of a method call only, which is where cgen passes `&recv`. A plain call is
still an error.

Recovered: test_enum_match_arms, test_lowered_forms, test_method_ptr_receiver,
test_method_recv_import and test_notation_parser. test_ptr_arg_struct_reject
and the #1074 tests still pass, and `take(m)` still errors.

Refs #1009, #1074, #1080

Co-authored-by: Abhishek Shivakumar <[email protected]>
…er field

#1074 widened the struct-literal copy to fields whose type is only known in
another bundle module, and in doing so replaced the source operand of the
memcpy with the plain address of the value. That is right for a scalar or
an array field, and wrong for a pointer field fed by an array: it copied the
array's first bytes into the pointer, so the field held data as an address.
examples/dynamics/ga_dsys_syntax.flow faults on its first matrix read, which
is the parity_dynamics_dsl e2e failure (exit 139).

Choose the source by what the value is. An array value keeps the
__builtin_choose_expr form from before #1074, where cc compares the field's
type with the type of a pointer to its first element and hands over either
the decayed array or its elements. Anything else keeps #1074's plain address,
which is what its own case (`Lat { k: a.k }` of an imported struct) needs.

tests/lang/test_struct_lit_ptr_field_array.flow fills an imported struct's
`ptr<f64>` field from a local array and reads it back; it faults without this.

Refs #1074

Co-authored-by: Abhishek Shivakumar <[email protected]>
…struct literal fixes

bootstrap_from_c.sh --verify and self_host_full.sh both pass: three
consecutive generations are byte-identical.

Co-authored-by: Abhishek Shivakumar <[email protected]>
run_lang_tests takes every .flow under tests/lang/ as a program, links it and
reads its exit code. The suite has grown files that only export items for a
test to import (tests/lang/bpn_m/a.flow and five others), and linking one on
its own fails for want of main, so `./flow test-lang` reported six failures
that are not tests.

Skip a file with no main() and say how many were skipped, so a helper that
loses its main is still visible in the count.

With this, `./flow test-lang` is 231/233 on this branch. The two failures,
test_c_import_dirs and test_dlopen, also fail on main and need headers and a
shared library this environment does not have.

Co-authored-by: Abhishek Shivakumar <[email protected]>
godofecht added a commit that referenced this pull request Sep 30, 2026
…1090)

Ports the two fixes in #1081 that #1082 did not cover, with their tests.

- #1050: an array literal used as a value (indexed, cast or measured with len) gets a compound literal's type, `(int32_t[]){ ... }`, because a bare initializer list is not a C expression. `let a = [1, 2, 3]` takes its element type and dimensions from the literal, and a statement-level literal evaluates nested rows for their effects.
- #1048: a module static of type `&[T]` or `&mut [T]` initialised from `null` is the empty span, as `span<T>` already was.

Tests, from #1081: `tests/lang/test_array_literal_value.flow`, `tests/lang/test_static_span_null_forms.flow` and the `tests/cgen/array_literal_value` golden. Both lang tests failed on main before this change.

Local gates: bootstrap fixed point, self_host_full, roundtrip, cgen 107/0, test-lang 230, every parity gate (parity_shader_dsl only at its Metal step, as always on this Mac), parity_typecheck, typecheck_rules, conformance, mlir_commands 72/72, fuzz, corpus parity and the ratchet.
@godofecht

Copy link
Copy Markdown
Contributor Author

Closing: the fixes in this branch are on main through #1082 and #1090.

I ran this branch's 15 new tests/lang programs and its tests/cgen golden against main after #1082. Thirteen of the programs passed as they were. The two that failed, and the golden, cover #1050 and #1048, which #1082 did not include. #1090 ports those two fixes and their tests from this branch.

@godofecht godofecht closed this Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment