Skip to content

A name is words joined by dots, a namespace is the file's real path, a C id is unique, and every clash is refused - #1042

Merged
nicolas-abril merged 1 commit into
mainfrom
unique-names
Sep 24, 2026
Merged

nicolas-abril merged 1 commit into
mainfrom
unique-names

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

Several soundness bugs came from names that looked different but met under one key, one C id or one JS tag. This PR stops that at the source: names and import paths are validated, every generated id is unique, and every clash is a compile error.

Closes #994, closes #989, closes #1002, closes #1005. Supersedes #1006. It also covers what #905, #790, #870 and #914 fixed one by one.

What changes

  1. Names. A declared name is words joined by dots: [A-Za-z_]\w*(\.[A-Za-z_]\w*)*. ~ is only for names the checker and compiler mint (k~binder, k~n, Clo~apply, IO~emit), so a user name can never land on one. This closes A constructor name reused by a later definition bypasses #905 and admits Empty #994: a later def or constructor can no longer take a template's opaque ~ name.
  2. Namespaces. An import path is plain names (letters, digits, _, -), with / and ../ as separators. The namespace is the file's real path, relative to the entry's real dir, or to BEND_LIB for hub files. A file reached through two paths (a symlink, a/../b) loads once. Only a path spelled 0x<hash>/… goes to the hub, and a hub file imports only the hub. A local ../0x<hash>/f.bend or ./0x<hash>/f.bend is refused (A local file can take a hub package's 0x… namespace: ../0x<hash>/f.bend from a subdirectory is loaded locally but named like the package #1005). A dotted file name like LAWS.Nat.bend is refused, and LAWS/Nat.bend gets its own namespace, LAWS/Nat. So no file can register keys in another file's namespace (Names resolve to keys other files register under the importer's namespace: an app file can rewrite what LAWS.bend says, or make PROOF.bend check different code than bend main.bend runs #1002). book_load was rewritten around this.
  3. Clashes are compile errors:
    • a name declared twice;
    • a Base name redeclared in a module;
    • an alias given twice, or an alias that shadows a dotted name or a declaration;
    • a foreign def named like a constructor;
    • one effect file imported from two namespaces;
    • an effect registered twice (C and JS runtimes).
  4. C ids come from one table: CID_/FID_ plus the cleaned name, suffixed _N when taken, never equal to a runtime id (FID_EXIT, CID_ARITY_T, …). Case-distinct constructors get distinct ids (C backend rejects case-distinct constructors because both names become the same CID macro #989).
  5. Effect files write CID(Name) and FID(name). They are substituted at compile time, reading the name in the effect's namespace first, then as written. An unknown name is an error. In JS, CID(Name) is the constructor's key, which is its tag, and JS names mangle reversibly (JS backend silently merges distinct defs whose names mangle to the same identifier (C lane refuses the same program) #790).
  6. JS effects register themselves with io_eff(CID(Name), run, need), as C effects do, instead of being found by name. bend2/effs/* and guide/EFFECTS.md use the new form.

Breaking changes

  • Effect code outside this repo must switch from CID_FOO to CID(Foo) and, in JS, add the io_eff(...) registration line.
  • Import paths with dots, ~, spaces or other characters in a segment are refused, and so are def names outside the rule above.
  • A module can no longer redeclare a Base name.

Verification

Not done

  • The cluster gates/test.ts, perf and GPU gates were not run.
  • bend.ts's book_load is rewritten from scratch, so it needs a careful human read.

🤖 Generated with Claude Code

…a C id is unique, and every clash is refused

Names that differed only in special characters or case, or in a
namespace and a dotted name, met under one key, one C id or one JS tag:
a later def could take a template's opaque ~ name and admit Empty
(#994, after #905), case-distinct constructors shared a CID macro
(#989), and a file could register keys in another file's namespace
(#1002) or take a hub package's 0x<hash> namespace (#1005).

- A declared name matches [A-Za-z_]\w*(\.[A-Za-z_]\w*)*; ~ is left to
  names the checker and compiler mint (k~binder, Clo~apply, IO~emit).
- An import path is plain names (letters, digits, _ and -), with ../
  and / as separators. The namespace is the real path relative to the
  entry's real dir, or to BEND_LIB for hub files, so a file reached by
  two paths loads once. Only a path spelled 0x<hash>/ goes to the hub,
  and a hub file imports only the hub.
- A name declared twice, a Base name redeclared in a module, an alias
  given twice, an alias that shadows a dotted name or a declaration, a
  foreign def named like a constructor, one effect file imported from
  two namespaces, and an effect registered twice all fail to compile.
- C ids come from one table: CID_/FID_ plus the cleaned name, suffixed
  _N when taken, never a runtime id. Effect files write CID(Name) and
  FID(name), read in the effect's namespace and then as written; an
  unknown name fails. In JS, CID(Name) is the constructor's key, which
  is its tag, and names mangle reversibly.
- A JS effect registers itself with io_eff(CID(Name), run, need), as a
  C effect does; the C and JS runtimes refuse a second registration.
  bend2/effs and guide/EFFECTS.md use the new form.

Closes #994, #989, #1002, #1005.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit 95317d9 into main Sep 24, 2026
@nicolas-abril
nicolas-abril deleted the unique-names branch September 24, 2026 18:19
nicolas-abril added a commit that referenced this pull request Sep 25, 2026
…me) form

The Process.run branch predates #1042, so its JS file never called
io_eff and the interpreter and JS lanes stopped with "no effect
registers Process.run", while C still built from the old CID_PROCESS_RUN
macro. The JS file now registers with io_eff(CID(Process.run), ...) and
walks the argument list by CID(Con), and the C file registers under
CID(Process.run), as every other effect does. tests/io/process_run and
process_run_parallel pass on the interpreter, JS and C.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 25, 2026
…mes: op_name, and a constructor's full name as its JS tag

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril pushed a commit that referenced this pull request Sep 25, 2026
…1049)

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 #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 #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]>
@Lorenzobattistela Lorenzobattistela mentioned this pull request Sep 25, 2026
VictorTaelin added a commit that referenced this pull request Sep 27, 2026
… kernel

`bend <file> --safe` runs bend2's checker, then elaborates the checked book
to a .bendtt file (bend2/safe.ts) and checks it with the BendTT kernel's CLI
(bend2/bendtt.lean, bend5 main 8284f4a8: one Lean file with the kernel, its
claims and their proofs).

- safe.ts (19,924 ttok): per-def scope; convoys through matches and
  refutations; eta-long at binder or kind mismatch; bodiless defs go out as
  opaque defs at a model built from their type; items specialize at every
  parameter a kind depends on; templates specialize at their ~ arguments
  (or check at bend2's instances when an opaque constant has no model); a
  law-declared def and the helpers that call it back go out as one group
  def with a selector; a linear pair used twice is matched once and rebuilt
  at each use; it reads main's typed literals (#1004): a Nat literal past
  4096 goes out as Nat.add(Nat.mul(q, 4096), r), a U32 or F32 literal as one
  def per word.
- base.bend: Array.get.go, Array.swap.go, Map.get, Map.put, Map.ins,
  Map.seek, Map.has and Map.pop recurse on their own pieces; the forward
  helpers (.bit/.deep/.if) and five laws go. tests/flatten/array_statement
  expects Array.get.go.
- gates/safe.ts (+ safe_node.ts, safe_diag.ts): bend2's verdict against
  BendTT's per file, on the minis, for tests/ or a BendHub store. Last run:
  tests 979 agree, 1 partial (base's @unsafe Array.join), 24 user @unsafe,
  481 bend2 rejects, 0 false rejects, 0 timeouts; hub 715 agree, 11
  vacuous, 2 partial (F32 LtOk, no model for the native), 78 user @unsafe,
  213 bend2 rejects, 0 false rejects, 0 timeouts. Since the old base, bend2
  itself rejects 155 more hub files: 101 name clashes and 52 digit words
  (#1042), 2 calls to the gone Nat.read.fit; every other file that agreed
  still agrees.
- main.ts takes --safe; repo.ts and AGENTS.md list safe.ts and bendtt.lean;
  .gitignore skips the *.bendtt files --safe writes.

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