A root that imports a function as `main` (`use A::{entry as main};`) got a
bound-name copy named `main`, and the JS backends call any top-level `main`
on load, so importing the compiled module ran `A::entry`. The flattener now
makes no copy for a function bound as `main`: the root's references to it are
rewritten to its qualified name. Only a `main` the root declares is an entry
point. Regression test for Bun-ESM and JS, plus the root-main auto-call.
Closes #805
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
Closes #805
What was wrong
Suppose the root imports a function under the alias
main(use A::{entry as main};) and declares nomainitself. The flattener then made a bound-name copy calledmain. Bun-ESM (await main();) and JS (main();) call any top-levelmainwhen the module loads, so importing the compiled module ranA::entry.Fix
In
lib/module_loader.ml, the flattener no longer makes a copy for a function bound asmain. The root's references to that name are rewritten to the function's module-qualified name, the same way imported mutable constants are already handled. Only amainthat the root module declares is an entry point. The emitters are unchanged.For example, with
pub fn go() -> Int { main() }the output is nowexport function go() { return __mod_1A__entry(); }, with no auto-call.Tests
New test:
test_805_imported_main_aliasintest/test_module_root_flatten.ml:main, both backends still emit it.go()still returns the imported value.The test fails without the loader change. Locally, the full suite (606 tests), the Bun harness and the soundness ledger pass.
Refs #801, #802.
Generated by Claude Code