diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 0d833228..8c2a9575 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -132,6 +132,10 @@ escaped extern's emitted name (an extern `class` is called as `class_`) is refused. Trait impl methods sharing a name are now refused on Bun-ESM instead of silently replacing one another (`stdlib/traits.affine`). Rebuild compiled output (Closes #801) +* fix(modules): an imported function bound as `main` (`use A::{entry as main};`) +is no longer called when the compiled module loads (Bun-ESM, JS and the other +flattened backends). Only a `main` the root module declares is an entry point; +the root's calls to the alias go to the imported function (Closes #805) * fix(vscode-smoke): SKIP cleanly when @hyperpolymath/affine-vscode is unpublished (#381) * fix(governance): rename CLAUDE.md TypeScript exemptions heading to diff --git a/lib/module_loader.ml b/lib/module_loader.ml index 85b7b364..217c8e43 100644 --- a/lib/module_loader.ml +++ b/lib/module_loader.ml @@ -709,7 +709,9 @@ let check_unique (entries : entry list) : unit = first and each module's in its own order (declarations must precede their uses: the interpreter binds each function in an environment that holds only what came before it); then a copy, under its bound name, of - each function and immutable constant the root imports; then the root's + each function and immutable constant the root imports (not of a function + bound as [main]: only the root's own [main] is an entry point, so + references to that binding are rewritten to the qualified name); then the root's own declarations. The copy of an immutable constant keeps a literal value as-is (C emits [static const T K = ;], which needs a constant initialiser) and otherwise refers to the qualified declaration, so the @@ -748,6 +750,13 @@ let flatten_imports (loader : t) (prog : program) : program = match x with | XValue q -> (match Hashtbl.find_opt pool_value q with + | Some { decl = TopFn _; _ } when b = "main" -> + (* A top-level [main] is the program's entry point (the JS + backends call it on load). Only the root's own [main] is one: + an imported function bound as [main] gets no copy under that + name, and the root's references to it are rewritten to its + qualified name (#805). *) + ren := (b, q) :: !ren | Some { decl = TopFn fd; _ } -> copies := { origin = path; decl = TopFn { fd with fd_name = { fd.fd_name with name = b } } } diff --git a/test/test_module_root_flatten.ml b/test/test_module_root_flatten.ml index 91eb9c3a..2db163fd 100644 --- a/test/test_module_root_flatten.ml +++ b/test/test_module_root_flatten.ml @@ -903,8 +903,42 @@ let test_801_shared_constructor_exported () = Alcotest.(check bool) "Green stays private" false (Str.string_match (Str.regexp "\\(.\\|\n\\)*export const Green") js 0) +(* #805: only the root's own [main] is an entry point. An imported function + bound as [main] is not called on load, on either JS backend, and stays + callable from the root; a root [main] keeps its auto-call. *) +let test_805_imported_main_alias () = + let dir = fresh_dir "m805" in + write_file dir "A.affine" "module A;\npub fn entry() -> Int { 7 }\n"; + let flat src = + let prog = Parse_driver.parse_string ~file:"
" src in + let ld = loader_in dir in + match Resolve.resolve_program_with_loader prog ld with + | Error (e, _) -> Alcotest.failf "resolve: %s" (Resolve.show_resolve_error e) + | Ok (rctx, _) -> (Module_loader.flatten_imports ld prog, rctx.symbols) + in + let lines js = String.split_on_char '\n' js in + let check_backends src ~auto = + let prog, syms = flat src in + (match Codegen_deno.codegen_bun prog syms with + | Error e -> Alcotest.failf "Bun-ESM: %s" e + | Ok js -> Alcotest.(check bool) "Bun-ESM auto-call" auto + (List.mem "await main();" (lines js))); + (match Js_codegen.codegen_js prog syms with + | Error e -> Alcotest.failf "JS: %s" e + | Ok js -> Alcotest.(check bool) "JS auto-call" auto + (List.mem "main();" (lines js))) + in + let aliased = "module Main;\nuse A::{entry as main};\npub fn go() -> Int { main() }\n" in + check_backends aliased ~auto:false; + check_backends "module Main;\nuse A::{entry};\npub fn main() -> Int { entry() }\n" + ~auto:true; + if node_available () then + Alcotest.(check string) "the alias stays callable" "7" (run_flat_js dir aliased "go()") + let tests = [ + Alcotest.test_case "#805: an imported `main` alias is not auto-run" `Quick + test_805_imported_main_alias; Alcotest.test_case "#801: impl method reads an imported mutable" `Quick test_801_impl_reads_imported_mutable; Alcotest.test_case "#801: an immutable import is one runtime value" `Quick