Repository navigation
fix(modules,codegen): module-qualified names in flattened programs (#801) - #802
hyperpolymath wants to merge 1 commit into
Conversation
) 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
|
|
CI note: the 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: Generated by Claude Code |
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



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:XusesA.f,YusesB.f, andMainimportsX::{via_a}andY::{via_b}. Both printfrom B, and there is no diagnostic. Bun keeps the last of two same-named top-levelfunctions 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_importsis rewritten. The program that every non-Wasm backend compiles now follows these rules:__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.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.Bun,Deno,Vscode, MCP-shim stdlibextern 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 namedh, which no host binds.Ast.map_free), so any dependency it misses is also one the renamer would leave unrenamed. This replaces the partialfind_free_varsclosure.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.claim_top) and JS emitters refuse a second top-level binding of one JS name. This also catches the non-injectivemangle(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 (BluevsBlue(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 Intandimpl Eq for Boolboth emittedexport function eq, and Bun silently keptBool's. Trait dispatch (desugar_traits.ml) is not wired into the non-Wasm path. This PR makes the case loud: the AOT smoke test pinstraits.affine's refusal inexpected_refusals, and the ledger row isloud-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
.jsor another non-Wasm target from more than one module. metadatastician/kyX is among them (pinned at 613e9be)..wasmoutput and the VSCode extension (codegen_nodewraps Wasm) are unchanged.main,activateand harness-visible functions. That is everything a host or harness calls.__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.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_fromhunk so that the transitive public collision raisesImportCollision. This PR replaces that function and makes the same case compile correctly instead. Whichever lands second needs a conflict resolution inlib/module_loader.ml(and possiblylib/codegen_deno.ml, where #798 adds runtime pruning). #798 lists "real module namespacing, so two modules may each exportf" as a follow-up; this is that follow-up.Tests
Each new check fails against main's compiler (70379fe):
tests/codegen-bun/same_name_modulesruns Bun-ESM codegen: same-named functions in two modules collapse to one, and calls silently go to the wrong one #801's reproduction (publiclabel, privatehelper, plus the transitive X/Y shape) under Bun. On 70379fe:got "…|from B|from B".tests/codegen-bun/binding_collision.affineis a planted constructor clash that must be refused with#801(runner step 8). On 70379fe it compiles.test/test_module_root_flatten.ml:test_801_same_name_in_two_modules: public and privatelabel; interpreter and Bun-ESM, run under Node.test_801_transitive_public_and_const: transitive functions and constants.test_801_externs_keep_host_namestest_801_planted_collision_refused:check_unique, function-vs-extern, and both JS emitters handed a duplicate directly.test_801_enum_constructorstest_801_impl_methods_refuseddocs/SOUNDNESS.adochas two new rows (Bun-ESM codegen: same-named functions in two modules collapse to one, and calls silently go to the wrong one #801fixed; Bun-ESM codegen: same-named functions in two modules collapse to one, and calls silently go to the wrong one #801 impl methodsloud-fail). The stamp is advanced to 70379fe and the anchors are resealed; four anchors were added and no existing digest changed.Local runs on this branch:
just check: lint,dune runtest(630 tests), all guards, and the soundness ledger, all green.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