Skip to content

fix(modules,codegen): module-qualified names in flattened programs (#801) - #802

Open
hyperpolymath wants to merge 1 commit into
mainfrom
claude/issue-801-qualified-names
Open

hyperpolymath wants to merge 1 commit into
mainfrom
claude/issue-801-qualified-names

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Closes #801.

The hole, re-checked on main (70379fe)

#801 was found at 613e9be. On 70379fe, #797's private-helper renaming already makes the issue's literal three-file repro print from A / from B. The flaw is still there in its transitive shape: X uses A.f, Y uses B.f, and Main imports X::{via_a} and Y::{via_b}. Both print from B, and there is no diagnostic. Bun keeps the last of two same-named top-level functions without complaint (Node rejects them), which is why this showed up as wrong output rather than a load error.

The Wasm backend is not affected: it links imports by (module, name) (Lib.X.via_a, Lib.Y.via_b).

Fix: collision-free by construction

Module_loader.flatten_imports is rewritten. The program that every non-Wasm backend compiles now follows these rules:

  • Every function and constant of an imported module, public or private, is carried as __mod_<len-prefixed path>__<name>, for example __mod_3Lib1A__label. This replaces __priv_…; the encoding stays injective. References are rewritten through a capture-avoiding substitution.
  • The root module keeps its own names (main, activate, harness exports) and the names it imports. Each imported function or immutable constant is present as a copy under its bound name, so the flattened program and the bundle's export surface keep their shape. A mutable constant is referred to by its qualified name and never copied.
  • Externs keep their host names everywhere (Bun, Deno, Vscode, MCP-shim stdlib extern fns). An aliased extern (use H::{host as h}) is rewritten to the extern's own name. Previously it was re-declared as an extern named h, which no host binds.
  • Enum types and constructors keep their names.
  • Carrying uses a reachability pass over a total free-name collector. The collector shares its traversal with the renamer (Ast.map_free), so any dependency it misses is also one the renamer would leave unrenamed. This replaces the partial find_free_vars closure.

Defence in depth (fail loudly)

  • Module_loader.check_unique (all non-Wasm backends) refuses two different top-level bindings of one name, and refuses a function or constant named like a carried extern (E0206). The second case used to rebind a dependency's host call to the root's function.
  • Bun-ESM (claim_top) and JS emitters refuse a second top-level binding of one JS name. This also catches the non-injective mangle (class → class_) and anything that bypasses the flattener. Two enums' identical constructors ({ tag: "Red" }) are the same runtime value and are emitted once; differing ones (Blue vs Blue(Int)) are refused. Before, they failed at load time with a SyntaxError.

Found on the way: trait impl methods

The emitter check refused stdlib/traits.affine. impl Eq for Int and impl Eq for Bool both emitted export function eq, and Bun silently kept Bool's. Trait dispatch (desugar_traits.ml) is not wired into the non-Wasm path. This PR makes the case loud: the AOT smoke test pins traits.affine's refusal in expected_refusals, and the ledger row is loud-fail. Giving impl methods distinct names is left as an owner decision (see the hub card); it is not done here.

What changes for existing compiled output: rebuild needed

  • Who must rebuild: anything compiled to Bun-ESM, .js or another non-Wasm target from more than one module. metadatastician/kyX is among them (pinned at 613e9be). .wasm output and the VSCode extension (codegen_node wraps Wasm) are unchanged.
  • Unchanged: the root module's names, imported names as bound in the root, extern names, constructor names, and exported entry points such as main, activate and harness-visible functions. That is everything a host or harness calls.
  • Renamed (internal): imported modules' declarations now appear as __mod_… instead of bare names or __priv_…. Nothing should import those names. Anything that did (for example __priv_…) must switch to the root's bound names.
  • Now refused instead of silently wrong: a root function named like an extern a dependency uses; two different top-level bindings of one JS name; trait impls sharing a method name on Bun-ESM.
  • kyX: its sx_ / cl_ prefixing was a workaround for exactly this. After rebuilding against a compiler with this fix, the prefixes can be dropped. I have not touched kyX.

Overlap with #798

#798 (open) changes the same flatten_imports_from hunk so that the transitive public collision raises ImportCollision. This PR replaces that function and makes the same case compile correctly instead. Whichever lands second needs a conflict resolution in lib/module_loader.ml (and possibly lib/codegen_deno.ml, where #798 adds runtime pruning). #798 lists "real module namespacing, so two modules may each export f" as a follow-up; this is that follow-up.

Tests

Each new check fails against main's compiler (70379fe):

Local runs on this branch:

  • just check: lint, dune runtest (630 tests), all guards, and the soundness ledger, all green.
  • codegen-deno corpus: 39 harnesses. WASM corpus: 38 harnesses. Native Bun-ESM: 3 harnesses plus the negative checks.

No proof obligation is touched. formal/ is unchanged, and the ledger row is test-anchored per the repo's proof policy.

Commit is SSH-signed (%G? = G), committer HoTT-stuff.


Generated by Claude Code

)

Two modules' same-named functions collapsed to one in the program the
non-Wasm backends compile, and every caller ran the survivor: #801's
Lib.A.a_label printed Lib.B.label's text. #797 renamed private helpers,
but public declarations that reached the root through other modules'
imports (X uses A.f, Y uses B.f) still met under one name, and Bun keeps
the last of two same-named `function`s without complaint.

Flattening now makes a clash impossible by construction:
- every function and constant of an imported module, public or private,
  is carried as `__mod_<len-prefixed path>__<name>` (replacing
  `__priv_`), with references rewritten capture-avoidingly;
- the root module keeps its own names and the names it imports (a copy
  under the bound name; a mutable constant is referred to, not copied);
- externs keep their host names; an aliased extern is rewritten to the
  extern's own name instead of being re-declared under the alias;
- imported declarations are carried by a reachability pass over a total
  free-name collector that shares the renamer's traversal
  (Ast.map_free), replacing the partial find_free_vars closure.

Defence in depth: Module_loader.check_unique refuses two different
top-level bindings of one name, or a function named like an extern
(E0206), for every backend; the Bun-ESM and JS emitters refuse a second
top-level binding of a JS name. Identical enum constructors from two
enums are emitted once; differing ones are refused.

The emitter check also exposed trait impl methods sharing a name
(stdlib/traits.affine: two `function eq`s, Bun silently kept Bool's).
They are now refused; the AOT smoke pins traits.affine's refusal.

The Wasm backend links imports by (module, name) and was not affected.

Closes #801

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
@sonarqubecloud

Copy link
Copy Markdown

@hyperpolymath
hyperpolymath marked this pull request as ready for review November 19, 2026 03:02
@hyperpolymath hyperpolymath assigned Copilot and unassigned Copilot Nov 19, 2026

Copy link
Copy Markdown
Owner Author

CI note: the CI workflow (build, dune runtest, codegen corpora) never started on this PR. Its run ended in startup_failure with zero jobs (run 44448428678). The cause is not this PR's diff. The same startup_failure occurs on main's own push runs (70379fe, aea7de8, 613e9be) and on #798 and #800. .github/workflows/ci.yml pins its actions by tag (actions/checkout@v7.0.1, actions/setup-node@v7.1.0, actions/upload-artifact@v7.0.2). The repository's allowed-actions policy rejects tag pins when it creates the run; this is the known class in .claude/CLAUDE.md.

So the green checks here cover governance, scanning and DEED only. The build and test evidence for #802 comes from local runs on this head: just check (630 tests, guards, soundness ledger), the codegen-deno corpus (39 harnesses), the WASM corpus (38) and native Bun-ESM (3 plus negative checks). SHA-pinning ci.yml is a separate fix; I have raised it rather than folding it into this PR.


Generated by Claude Code

Repository owner deleted a comment from coderabbitai Bot Nov 19, 2026
@hyperpolymath
hyperpolymath enabled auto-merge (rebase) November 19, 2026 03:25
@hyperpolymath
hyperpolymath enabled auto-merge (squash) November 19, 2026 11:19
@hyperpolymath
hyperpolymath enabled auto-merge (rebase) November 19, 2026 12:21
@hyperpolymath
hyperpolymath enabled auto-merge (rebase) November 19, 2026 12:21
hyperpolymath pushed a commit that referenced this pull request Nov 19, 2026
affinescript-tea/e2e/todo.test.js calls node, key, text and create from the
compiled todo.bun.js. Since #797 the flattener exports only the Tea names a
module imports under their plain names; the rest become __priv_3Tea__<name>,
so the keyed-diffing test failed with 'm.node is not a function'. Both #778
and #797 landed while CI was not starting, so this was first seen here.

Importing the two names makes the test's contract explicit and is
independent of how the flattener names unimported items (#802).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
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.

Bun-ESM codegen: same-named functions in two modules collapse to one, and calls silently go to the wrong one

3 participants