Skip to content

The C leak guard skips the effect sources, which may say undefined - #1049

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
aldeni:effect-c-undefined-comment
Sep 25, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
aldeni:effect-c-undefined-comment

Conversation

@aldeni

@aldeni aldeni commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

What changes

compile_book refuses a C build whose text contains the word undefined
(/\bundefined\b/). In C the compiler generates, the word can only come from
a JS undefined that leaked into it. The test ran over
[tabs, spins, segs, fl.reqs], and fl.reqs holds the effect sources,
pasted in verbatim. It now runs over [tabs, spins, segs]. One line changes
in bend2/comp.ts, plus a two-line comment. There is one new test,
tests/io/effect_undefined_word.{bend,c,js}.

Why

A user effect .c that contains undefined anywhere, even in a comment such
as // the sum wraps at 2^32, so it is never undefined, fails the native
build with:

Error: an unbound name in the emitted C

The error does not say where the word is. For the same program, the check,
the interpreter and a JS-only build (-o x.js) all run correctly. If the
word is spelled differently, the program builds and runs natively.

History, as far as I could trace it:

A foreign def's generated request segment (compile_reqs pushes it to
fl.segs) is still read.

Test

tests/io/effect_undefined_word: the C twin contains undefined in a
comment and in a string literal (strlen("undefined")). main prints 18.

lane main (95317d9) this branch
check PASS PASS
interp 18 18
js build fails: an unbound name in the emitted C 18
c build fails: an unbound name in the emitted C 18

(The js lane fails on main because the test builds -o t.js -o t in one
command, as gates/test.ts does. A JS-only build on main runs and prints 18.)

The guard still fires on generated C. I checked with two temporary edits on
this branch, both reverted:

  • a #define LEAK ${fl.cids.get("nope")} line in the tables
  • WL_RETN(${fl.cids.get("nope")}) in the foreign def's generated segment

Each edit made the build fail with the same error.

Regression

These runs were on Linux x86-64, Bun 1.3.11 and clang 22:

  • All four lanes for the 9 tests in tests/io that have a .c twin: the
    same results as main, except for the new test.
  • The tests/io checkup (118 modules, one process): output identical to
    main.
  • -o x.c over all 118 tests/io programs, on main and on this branch:
    • 76 emit byte-identical C.
    • 41 are refused with the same message on both (no main, or expected
      errors).
    • The new test is the only difference.
  • gates/repo.ts: 46 / 46.
  • bend2/comp.ts ttok: 62602 -> 62632.

Not tested here

  • macOS (I did not run anything on an Apple M4) and the Metal/CUDA device
    paths.
  • The full gates/test.ts and gates/perf.ts on the cluster.

compile_book refuses a C build whose text holds the word undefined,
since in the C the emitter generates it can only be a JS undefined
that leaked. It tested [tabs, spins, segs, fl.reqs], and fl.reqs is
the effect sources pasted in verbatim, so an effect .c that said
undefined anywhere, even in a comment, failed `-o` with "Error: an
unbound name in the emitted C" and no location, while the check, the
interpreter and the JS build ran the same program. The guard now tests
[tabs, spins, segs].

The guard came with d7af6bf over the whole output; 85d805b narrowed
it to the code fills so a constructor named undefined could pass
through SHOW_NAMES. Until bendlang#1042, fl.reqs also held generated lines
(#pragma push_macro and #define CID_x CID_y around each source), which
the guard rightly read. Since bendlang#1042 it holds only the sources, their
CID(k) and FID(k) replaced by c_ids, which writes an uppercase id or
dies on an unknown name. A foreign def's generated segment goes to
fl.segs and is still read.

tests/io/effect_undefined_word: the C twin says undefined in a comment
and in a string; main prints 18. On 95317d9 the build fails with the
error above (lanes: check PASS, interp 18, js and c BUILDFAIL); with
the word respelled the program builds and runs there. On this branch
the check passes and interp, js and c all print 18.

The guard still fires on generated C: a temporary `#define LEAK
${fl.cids.get("nope")}` in the tables, and a `WL_RETN(${...})` of the
same miss in the foreign def's segment, each failed the build with the
same error on this branch (both edits reverted).

Regression, Linux x86-64: the 9 tests/io tests with a .c twin, four
lanes, same results as main but the new one; the tests/io checkup (118
modules) prints the same as main; -o x.c over all 118 tests/io
programs: 76 byte-identical, 41 refused alike, the new test the only
difference; gates/repo.ts 46/46; comp.ts ttok 62602 -> 62632.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril self-assigned this Sep 25, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you, this is merged. Your history checks out: before #1042 compile_reqs wrapped each source in generated push_macro/#define lines, and now fl.reqs holds only the effect files with CID/FID rewritten, so the guard loses nothing by skipping it. I also checked that no Bend name can put the word into the text the guard still reads: locals become _name_N and ids are uppercased, and on macOS a probe for every name position (defs, laws, params, lets, binders, types, constructors, fields, aliases, module and effect file names, foreign effects, pure-main show), with undefined, a.undefined, undefined_a and other variants against control names, behaved identically on check, interp, JS and C; names like constructor, __proto__, NaN and eval pass too. A clean, well-argued fix.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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