Skip to content

A file's names are keyed ns:name inside the compiler, so no declaration lands in another file's namespace and a foreign CID(None) keeps meaning Base's None (#1045) - #1156

Merged
nicolas-abril merged 2 commits into
mainfrom
ns-slash
Oct 1, 2026

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1045. Closes #1124. Closes #1184. Supersedes #1095.

The bug

A file's names were keyed namespace + "." + name. The entry file's namespace is "", so its names are keyed bare. As a result, the entry's declaration lib.None got exactly the key that lib.bend's own None would have. Two things then went wrong:

  • lib.bend's Bend code was resolved while lib.bend was parsed, so its None meant Base's None.
  • Its foreign C/JS source is resolved after every file has loaded. There, CID(None) tries lib.None first and so took the entry's constructor.

The same spelling meant two different constructors in the two halves of one library. The result was the wrong match arm, and on C, a read of the allocator's bump word plus a free into the wrong size class:

import ./lib.bend as Q + type Evil is Data: lib.None{} interp JS C
main Some Some Some 97
this PR None None None

The fix

The ambiguity was in the key encoding, so this fixes the encoding. #1095 instead kept the ambiguous keys and made c_ids compute an ownership scope per foreign source to work around them.

  • Keys: inside the compiler, a namespace now joins its names with :. A name is words joined by dots and a namespace is a path, so neither can contain :, and a key always names its file: lib.bend's None is lib:None, and the entry's lib.None stays lib.None.

  • Lookups: parse_qual joins with :, an alias expands to ns:name, c_ids tries ns:k then k, and name_show splits a key at its :.

  • Output: the : never leaves the compiler. name_key spells ns:name as ns.name everywhere a key is written out:

    • messages, and the unsafe/foreign and out-of-scope lists;
    • a printed main value, in SHOW_NAMES for C and in the JS show table;
    • JS constructor tags, effect ids and .mjs exports;
    • BendTT names.

    Every output reads exactly as on main.

  • Size: after the rebase, bend.ts +114 ttok, comp.ts +96 (63,213 → 63,309, under its 64k cap), safe.ts +37, main.ts +4.

Also fixes #1124

The same encoding caused #1124 from the other side. Word.bend's namespace is Word, so its bare Nil was first looked up as its own Word.Nil, which is Base's Word.Nil. With ns:name keys the file's own guess is Word:Nil, which no file declares, so Nil is List's. Likewise, String.bend's bare eq, which nothing declares, is now refused as undefined, instead of silently binding Base's String.eq.

Only a sibling of the entry file gets a bare family name as its namespace (libs/Word.bend is libs/Word), so the test sits next to its entry:

  • tests/import/base_family_file.bend imports a sibling tests/import/Word.bend and prints 1n on every lane. On main the check, JS and C lanes all fail with the Word.Nil error.
  • gates/repo.ts now allows capital letters in a test's file name, so a test can be named like the family it stands for.

Behaviour changes

  • Newly accepted:
    • The entry may declare lib.x next to lib.bend's x; main refuses it as a duplicate.
    • A file may be named like a Base type, e.g. U32.bend declaring show; main clashed it with Base's U32.show.
    • A file may declare its own name, e.g. lib.bend declaring lib.lib.
  • Now refused: a file reaches another file's names only through its own aliases. Spelling another namespace directly, e.g. filling lib.bend's law with def lib.one() instead of def L.one(), now says the name isn't in scope. This is Imported files answer to their file name as well as to their alias, so one prefix can point at two files #1184: an imported X.bend no longer answers to X.f when its alias is Z.
  • Same printed name: the only case where two distinct keys print alike is the first one above. There, the entry's lib.x and lib.bend's x both print as lib.x. The program still behaves correctly because the keys differ. In JS, two such foreign effects fail loudly ("two effects register"), and two such .mjs exports share one export name.

Verification

Each program was run on this branch and on main (ef66a7cc) through the interpreter, --check-only, -o .js + node, native -o + run, --verdict and -o .bendtt, and all outputs were diffed. The programs:

  • all 1578 under tests/, bench/runtime and demos;
  • a generated matrix of 120: 4 layouts (sibling, subdirectory, ../ climb, chain with diamond) × 4 name shapes (bare, Geo.x, lib.x, A.B.x) × def/type/constructor, working and with errors;
  • 67 adversarial cases: hub packages through BEND_LIB, hyphenated paths, cycles, one file under two aliases, colliding aliases, laws and LAWS/PROOF, templates, operators, .mjs interop, foreign effects in sub/deep and ../shared, @unsafe in nested files, and every error message that names a key;
  • this PR's regression test.

Results:

  • No : in any output: in the output of all 1773 programs (every lane's output and the emitted .c, .js, .mjs and .bendtt, string literals included), no name:name token appears that isn't in main's output.
  • Identical to main except for:
  • No existing test changes.
  • Regression test: tests/io/cid_capture is from Foreign effect IDs resolve only in the owner's import scope (#1045) #1095 (by @oxura). It forges None and a three-field Con over a List node and prints None / 6 on every lane (main: Some / Some 97).
  • Repo gate: gates/repo.ts passes, 49 / 49.

After the rebase onto main (607a095a)

  • Conflicts: only in comp.ts, all against The compiler says the same in 658 fewer tokens, with the same output and compile time #1235's rewrites and the newer comment-skipping CID scan. Each hunk keeps main's code with this PR's name_key and : applied. main's 31 new commits add no namespace joins or key printing of their own.
  • The has no tag message: this PR had broken it over three lines to fit name_key, which changed the emitted JS text. It now keeps main's layout, with the two names computed first.
  • tests/io/cid_capture_lib.bend had no #| lines, but in the check lane it prints the usual "2 defs rely on unsafe or foreign code" error, so the gate would have failed it. It now pins that error, as the other foreign-code helpers do.
  • Sweep: all 1603 .bend files under tests/, bench/ and demos/, comparing --check-only output, .c, .js, .bendtt and every log against main:
    • Real differences: only cid_capture (fixed).
    • Log paths: 37 .bendtt logs differ only by the output path written into them.
    • JS function names: 17 .js files differ only there. A : in a key is mangled as $058 ($alias_lib$058two$). That keeps the entry's lib.two and lib.bend's two as two JS functions, so it is intended. It does mean the .js above is identical to main only up to these internal names.
  • Tests: the new test and both cid_capture files pass the gate's check lane, run as its aggregator, and their JS and C lanes. tsc reports the same 12 errors as main. gates/repo.ts passes, 50 / 50.

Not run

  • The cluster gates (test, perf, safe): the minis refused my session's key.

🤖 Generated with Claude Code

@nicolas-abril nicolas-abril changed the title A namespace joins its names with /, so no declaration lands in another file's and a foreign CID(None) keeps meaning Base's None (#1045) A file's names are keyed ns:name, so no declaration lands in another file's namespace and a foreign CID(None) keeps meaning Base's None (#1045) Sep 28, 2026
@nicolas-abril nicolas-abril changed the title A file's names are keyed ns:name, so no declaration lands in another file's namespace and a foreign CID(None) keeps meaning Base's None (#1045) A file's names are keyed ns:name inside the compiler, so no declaration lands in another file's namespace and a foreign CID(None) keeps meaning Base's None (#1045) Sep 28, 2026
nicolas-abril and others added 2 commits October 1, 2026 18:29
…on lands in another file's namespace and a foreign CID(None) keeps meaning Base's None (#1045)

A file's names were keyed namespace + "." + name, and the entry file,
whose namespace is "", keys its names bare. So the entry's declaration
lib.None took the key lib.bend's own None would have had, and lib.bend's
foreign C and JS, whose CID(k) tries lib.k before k once every file has
loaded, returned the entry's constructor where lib.bend meant Base's
None: the wrong match arm, and on C a read of the allocator's bump word
and a free into the wrong size class.

A key now joins its namespace with ":", which neither a name nor a path
holds, so a key names its file: lib.bend's None is lib:None, and the
entry's lib.None stays lib.None. An alias expands to ns:name, c_ids
tries ns:k then k, and name_show splits a key at its ":".

The ":" never leaves the compiler: name_key spells ns:name as ns.name
wherever a key is written out (messages, the unsafe lists, a printed
main, JS tags, effect ids and exports, SHOW_NAMES, BendTT names), so
every output reads as before.

The entry may now declare lib.x beside lib.bend's x, a file may be named
like a Base type (U32.bend with show) or declare its own name (lib.bend
with lib.lib), and a file reaches another's names only through its
aliases.

tests/io/cid_capture is from #1095: a forged None and a three-field Con
over a List node print None and 6 on every lane.

Co-authored-by: oxura <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…s own keys are ns:name (#1124)

Word.bend's namespace is Word, so its bare Nil was looked up as Word.Nil
first, which is Base's Word.Nil, and Nil{} became the Word constructor.
With ns:name keys the file's own guess is Word:Nil, which no file
declares, and Nil is List's. tests/import/base_family_file imports a
sibling Word.bend and prints 1n on every lane (main: the Word.Nil
error). A test file may now be named with capitals, like the family it
stands for.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit f370026 into main Oct 1, 2026
@nicolas-abril
nicolas-abril deleted the ns-slash branch October 1, 2026 16:36
nicolas-abril added a commit that referenced this pull request Oct 1, 2026
… import_Word no longer glues onto the test before it

#1156 let tests/ hold A-Z file names (tests/import/Word.bend), but the
test gate still split the --checkup output on a lowercase-only header.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant