Repository navigation
Fix C emission when a native effect invokes a Bend callback - #1286
Conversation
Lorenzobattistela
left a comment
There was a problem hiding this comment.
Thanks for the report and the repro. The bug is real: foreign C can't name Clo~apply unless the Bend code happens to apply a closure itself.
The cause is order. compile_book reads the .c sources inside the fixpoint loop, before seg_new(IO_EMIT) / seg_new(CLO_APPLY) create the runtime segments. Registering the two names up front works, but it leaves the order as it is. It also hands the unsuffixed macro name to the runtime when a user def's name collides (tests/run/ids_unique and tests/check/name_owned_def swap FID_CLO_APPLY and FID_CLO_APPLY_1). And any future runtime segment would need the same treatment.
Nothing in the loop uses reqs, so the sources can be read where they're used, after every ID exists:
let was: number;
- let reqs: string;
do {
...
- reqs = effect_srcs(fl, ".c", "no .c import: ").join("");
...
return c_ids(fl, runtime_c([tabs, ...desc].join("\n\n"), spins, segs,
- reqs));
+ effect_srcs(fl, ".c", "no .c import: ").join("")));This also reads the sources once instead of on every loop iteration.
The budget matters too. On current main (60b6c5f), comp.ts is at 63,979 ttok, and with this PR merged it comes to 64,007, which fails gates/repo.ts. The diff above brings it to 63,966.
I ran the diff above with your two tests on 60b6c5f:
- repo: 50/50. test: 1545/1545. ping: 46/46. safe: same as main, plus your two foreign tests as unsafe.
- perf: same as main; the reds were the usual PAR-GPU noise.
- _run: three runs; the test gate went over the 30 s cap in one of them, the other two passed.
- Emitted C: byte-identical to main for all 736 programs.
- Your tests: both print 42 under C, bun, node and the interpreter.
Could you swap the seg_fid lines for this? A few smaller things on the tests:
- foreign_runtime_emit: nothing in user code needs to spell IO~emit, which is io_spawn's final continuation. The test locks in its private result layout, including the heap_free(e, 0, loc) size class, and its JS twin is just console.log(42). I'd drop it and keep foreign_runtime_apply.
- foreign_runtime_apply: returning the callback's result to Bend (an IO(U32) effect, printed with IO.print) instead of printf would also check that the value makes it back into Bend. It would also avoid skipping io_out's short-write check.
Read each source once after segment emission, without pre-registering runtime names or changing ID assignment order. Return the callback result through IO and print it from Bend. Keep a sole-effect regression without an IO.bind continuation, and remove the IO~emit layout test.
A native C effect should be able to call a callback received from Bend. This can currently fail at compile time because the compiler resolves the runtime helper's ID before registering it.
What you did
Saved the two files below and compiled on upstream
main@947db722, still the latest HEAD when checked on 2026-10-03:The C effect calls the Bend callback with
40; the callback adds2.What happened
C emission fails before clang runs:
Expected: compilation succeeds and the native program prints
42.The file
main.bend:native.c:Why
Clo~applyapplies a closure in the generated runtime; it is not declared in the Bend book.compile_bookreads foreign C sources througheffect_srcsinside the fixpoint loop, before the runtime segments have been created. When no emitted Bend call has registeredClo~applyyet,c_idsrejects the valid runtime name.Change
Read the foreign C sources in the final
runtime_ccall, after every segment and ID exists. This reads each source once and preserves the existing ID assignment order, including user definitions whose C macro names collide with runtime names.foreign_runtime_applyreturns the callback's result asIO(U32)and prints it from Bend withIO.print. Both foreign implementations return the callback's value.foreign_runtime_apply_tailuses the same C/JS sources with the foreign call as main's only effect. It retains coverage of the original missing-ID failure: adding anIO.bindcontinuation to print the result can itself registerClo~applyon unpatched main. This test succeeds without output, as an IO main's return value is discarded.Verification
Local checks against upstream
main@565d7fdeon Apple M5/macOS:foreign_runtime_apply_tailfail C emission on main withFID(Clo~apply) names no constructor or def. Both compile with this patch; the original reproducer prints42with 1/4 threads and GPU off.ids_unique,name_owned_def, foreign values and namespaces, and the two ownership regressions.gates/repo.ts: PASS 50/50.comp.ts: 63,951 → 63,938 ttok (cap 64,000, unchanged).The cluster
gates/test.tscould not start because this machine cannot resolvecluster. Full cluster test/perf/safe/ping gates and CUDA were not run locally.bend --version
Output of
bun bend2/main.ts version(tested compiler):Base:
565d7fdec289b1f2f5f5037bb58b5afb6738ff1d; PR head:72cfbe97de41add3cb993d924ccc14a37bfbab6b.uname -sm
Apple M5, macOS 26.6.2.
clang --version (the first line)
Created with AI assistance (Codex).