diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 414748a2..a59dfaf7 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -109,6 +109,20 @@ pure-extern semantics (Closes #332) (#338) ==== Fixed +* fix(modules,codegen): same-named functions in two modules no longer +collapse to one in a flattened program (Bun-ESM, JS and the other +non-Wasm backends). Every function and constant of an imported module is +carried under a module-qualified name (`__mod___`, +replacing `__priv_…`), so `Lib.A.label` and `Lib.B.label` stay distinct, +public or private, also when they arrive transitively. The root module's +own names, the names it imports, and extern (host) names are unchanged. +Defence in depth: the flattener refuses two different top-level bindings of +one name (E0206), and the Bun-ESM and JS emitters refuse a second top-level +binding of a JS name (Bun keeps the last of two same-named `function`s +silently). Two enums' identical constructors are emitted once; differing +ones are 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(vscode-smoke): SKIP cleanly when @hyperpolymath/affine-vscode is unpublished (#381) * fix(governance): rename CLAUDE.md TypeScript exemptions heading to diff --git a/docs/COMPLETION-PLAN.adoc b/docs/COMPLETION-PLAN.adoc index cacbd129..1df1c00b 100644 --- a/docs/COMPLETION-PLAN.adoc +++ b/docs/COMPLETION-PLAN.adoc @@ -221,7 +221,7 @@ All were observed by RUN unless marked otherwise. The maps confirm that H1–H4 . H18. The interpreter fails when dereferencing refs returned through calls ("Expected reference type"). The session lead reproduced this on two #554 fixtures. . H19 (typed-wasm). No L5 index guard: `get_player_hp(0,150)` reads an adjacent region. `where` constraints are discarded. Malformed `session` blocks are silently ignored. . H23 (found and fixed 2026-11-09, checking and running metadatastician/kyq; numbered H20 in #788, renumbered because #787 assigned H20–H22). A module file found for `A.B` that declares `module A;` was accepted as `A.B`, so `use A.Nonexistent::*` type-checked and imported `A`'s exports; it is now a `ModuleDeclMismatch` load error. *Round 2 (2026-11-16):* a nested candidate (`dir/A/B.affine`) is now searched in every directory before any parent-module fallback, and a module whose load fails is no longer left marked as loading (which reported a later import of it as a cycle). Anchor: `test/test_module_root_flatten.ml`. -. H24 (found and fixed 2026-11-09; numbered H21 in #788). `eval` ran the unflattened program, so no imported function was bound at run time; flattening also emitted a selected function before the private helpers it calls and carried no helpers for glob or plain imports. `eval` now flattens, every import kind carries its helpers, and they are emitted in the imported module's declaration order. *Round 2 (2026-11-16):* two modules with a same-named private helper no longer collide: every private helper carried into a flattened program is renamed `__priv___` with a capture-avoiding substitution (the module path is length-prefixed, so `A_B.C` and `A.B_C` cannot share a name), and an aliased import emits the original before the alias. *Round 3 (2026-11-16):* a public declaration carried only for the module's own callers (the original of an alias, or a helper only a selected function calls) gets an internal name too, so two modules' `helper`s cannot replace one another; aliasing a mutable binding, which would make a second copy, and an unresolvable name collision are structured flattening errors (diagnostic E0206), not exceptions. Only the root program's `main` is run. Anchor: `test/test_module_root_flatten.ml`. +. H24 (found and fixed 2026-11-09; numbered H21 in #788). `eval` ran the unflattened program, so no imported function was bound at run time; flattening also emitted a selected function before the private helpers it calls and carried no helpers for glob or plain imports. `eval` now flattens, every import kind carries its helpers, and they are emitted in the imported module's declaration order. *Round 2 (2026-11-16):* two modules with a same-named private helper no longer collide: every private helper carried into a flattened program is renamed `__priv___` with a capture-avoiding substitution (the module path is length-prefixed, so `A_B.C` and `A.B_C` cannot share a name), and an aliased import emits the original before the alias. *Round 3 (2026-11-16):* a public declaration carried only for the module's own callers (the original of an alias, or a helper only a selected function calls) gets an internal name too, so two modules' `helper`s cannot replace one another; aliasing a mutable binding, which would make a second copy, and an unresolvable name collision are structured flattening errors (diagnostic E0206), not exceptions. *Round 4 (2026-11-19, #801):* the per-case renaming above still let two modules' public declarations of one name meet when they arrived through other modules' imports (`X` uses `A.f`, `Y` uses `B.f`; the flattened program kept one `f` and Bun ran it for both). Every function and constant of an imported module, public or private, now carries a module-qualified name `__mod___` (length-prefixed as before; `__priv_` is gone), the root keeps its own and its imported names, externs keep their host names, and the flattener and the JS emitters refuse two different top-level bindings of one name. Only the root program's `main` is run. Anchor: `test/test_module_root_flatten.ml`. . H25 (found and fixed 2026-11-09; numbered H22 in #788). The interpreter had no `++` on arrays and no `==`/`!=` beyond Int, Float, String and Bool, though the checker accepts them; it now uses the structural `Value.value_eq`. H7 (`let mut`) is unchanged. Anchor: `test/test_module_root_flatten.ml`. == 2. Target architecture diff --git a/docs/SOUNDNESS.adoc b/docs/SOUNDNESS.adoc index 533643ce..2e80af4e 100644 --- a/docs/SOUNDNESS.adoc +++ b/docs/SOUNDNESS.adoc @@ -4,8 +4,8 @@ // Freshness stamp — a real main-ancestor commit (survives squash-merge); advance // it (and --reseal) in any change that touches a soundness path. See "Keeping // this honest" and tools/check-soundness-ledger.sh (property 4). -:ground-truth-sha: 65c2ae3 -:ground-truth-date: 2026-06-21 +:ground-truth-sha: 70379fe +:ground-truth-date: 2026-11-19 :stamp-verified-by: tools/check-soundness-ledger.sh :toc: macro :toclevels: 2 @@ -211,6 +211,33 @@ pinned residual).) `test_stub_backend_return_fenced` (positive check: asserts `Lean_codegen.codegen_lean` returns `Error` on the early-`return` program — it was `Ok`; the check would fail if the silent drop regressed). + +| #801 +| Two modules' top-level functions or constants of one name stay distinct in +the flattened program every non-Wasm backend compiles: each imported +module's declarations, public and private, carry a module-qualified name, so +`Lib.A`'s code calls `Lib.A.label` (it called `Lib.B.label`, silently, also +when the two arrived through other modules' imports). The flattener refuses +two different bindings of one name (E0206), and the Bun-ESM and JS emitters +refuse a second top-level binding of a JS name: Bun keeps the last of two +same-named `function`s without complaint, so this is the loud backstop. The +Wasm backend links imports by module and name and was not affected. +| `fixed` +| `test_801_same_name_in_two_modules` + `test_801_transitive_public_and_const` +(interpreter and Bun-ESM, run) + `test_801_planted_collision_refused` (the +backstop refuses a planted collision in the flattener and in both JS +emitters); `tests/codegen-bun/same_name_modules` runs #801's reproduction +under Bun. + +| #801 (impl methods) +| Trait impl methods are emitted as free functions under their own names on +Bun-ESM (trait dispatch is not lowered there), so `impl Eq for Int` and +`impl Eq for Bool` gave two `function eq`s and Bun silently kept `Bool`'s. +The emitter now refuses the program. `stdlib/traits.affine` is refused for +this reason until impl methods get distinct names. +| `loud-fail` +| `test_801_impl_methods_refused` (the AOT smoke pins `traits.affine`'s refusal +in `expected_refusals`). |=== == Still open — read before claiming "sound" diff --git a/lib/ast.ml b/lib/ast.ml index 07ec17a0..aa9d7e0b 100644 --- a/lib/ast.ml +++ b/lib/ast.ml @@ -576,126 +576,155 @@ let rec pattern_binders (pat : pattern) : string list = | PatOr (p1, p2) -> pattern_binders p1 @ pattern_binders p2 | PatAs (id, pat) -> id.name :: pattern_binders pat -(** Rename free occurrences of variables: [ren] maps old names to new ones. - Capture-avoiding in the usual way: a binder (let, match arm, lambda - parameter, block `let`, `for` pattern, handler pattern) removes the - names it binds from [ren] for its scope, so a local that shadows a - renamed top-level name keeps referring to the local. A record - shorthand field `#{ x }` whose variable is renamed becomes - `#{ x: x' }`. Used by import flattening to give each module's private - helpers module-specific names. *) +(** Map the free occurrences of variables through [look]: an occurrence of + [n] becomes [n'] when [look n = Some n'] and is left alone when it is + [None]. Capture-avoiding in the usual way: a binder (let, match arm, + lambda parameter, block `let`, `for` pattern, handler pattern) hides the + names it binds from [look] for its scope, so a local that shadows a + top-level name keeps referring to the local. A record shorthand field + `#{ x }` whose variable is mapped becomes `#{ x: x' }`. + + [look] is also called, for observation only, with the type name of a + qualified constructor `T::C`; its result is ignored there. That lets + {!free_names} report the enums a body names without a second walker: + the renamer and the free-name collector see exactly the same + occurrences, so a dependency the collector misses is one the renamer + would also leave unrenamed. Every expression form is visited. *) +let shadow (look : string -> string option) (names : string list) = + if names = [] then look + else fun n -> if List.mem n names then None else look n + +let rec map_free (look : string -> string option) (e : expr) : expr = + let r = map_free look in + let ro = Option.map r in + match e with + | ExprLit _ | ExprBreak _ | ExprContinue _ -> e + | ExprVariant (ty, _) -> ignore (look ty.name); e + | ExprVar id -> + (match look id.name with + | Some n -> ExprVar { id with name = n } + | None -> e) + | ExprLet lb -> + let inner = shadow look (pattern_binders lb.el_pat) in + ExprLet { lb with el_value = r lb.el_value; + el_body = Option.map (map_free inner) lb.el_body } + | ExprIf ei -> + ExprIf { ei_cond = r ei.ei_cond; ei_then = r ei.ei_then; + ei_else = ro ei.ei_else } + | ExprMatch m -> + ExprMatch { em_scrutinee = r m.em_scrutinee; + em_arms = List.map (map_arm look) m.em_arms } + | ExprLambda lam -> + let inner = + shadow look (List.map (fun (p : param) -> p.p_name.name) + lam.elam_params) in + ExprLambda { lam with elam_body = map_free inner lam.elam_body } + | ExprApp (f, args) -> ExprApp (r f, List.map r args) + | ExprField (x, f) -> ExprField (r x, f) + | ExprTupleIndex (x, i) -> ExprTupleIndex (r x, i) + | ExprIndex (a, b) -> ExprIndex (r a, r b) + | ExprTuple es -> ExprTuple (List.map r es) + | ExprArray es -> ExprArray (List.map r es) + | ExprRecord rc -> + let field ((n : ident), eo) = + match eo with + | Some x -> (n, Some (r x)) + | None -> + (match look n.name with + | Some nn -> (n, Some (ExprVar { n with name = nn })) + | None -> (n, None)) + in + ExprRecord { er_fields = List.map field rc.er_fields; + er_spread = ro rc.er_spread } + | ExprRowRestrict (x, f) -> ExprRowRestrict (r x, f) + | ExprBinary (a, op, b) -> ExprBinary (r a, op, r b) + | ExprStringConcat (a, b) -> ExprStringConcat (r a, r b) + | ExprStringEq (a, b, neg) -> ExprStringEq (r a, r b, neg) + | ExprStringRel (a, b, op) -> ExprStringRel (r a, r b, op) + | ExprFloatBinary (a, op, b) -> ExprFloatBinary (r a, op, r b) + | ExprFloatArray es -> ExprFloatArray (List.map r es) + | ExprFloatIndex (a, b) -> ExprFloatIndex (r a, r b) + | ExprCellTuple cs -> ExprCellTuple (List.map (fun (x, f) -> (r x, f)) cs) + | ExprCellTupleIndex (x, i, f) -> ExprCellTupleIndex (r x, i, f) + | ExprCellRecord fs -> + ExprCellRecord (List.map (fun (n, x, f) -> (n, r x, f)) fs) + | ExprCellField (x, i, f) -> ExprCellField (r x, i, f) + | ExprUnary (op, x) -> ExprUnary (op, r x) + | ExprBlock b -> ExprBlock (map_block look b) + | ExprReturn eo -> ExprReturn (ro eo) + | ExprTry t -> + ExprTry { et_body = map_block look t.et_body; + et_catch = Option.map (List.map (map_arm look)) t.et_catch; + et_finally = Option.map (map_block look) t.et_finally } + | ExprHandle h -> + let handler = function + | HandlerReturn (p, x) -> + HandlerReturn (p, map_free (shadow look (pattern_binders p)) x) + | HandlerOp (op, ps, x) -> + HandlerOp (op, ps, + map_free + (shadow look (List.concat_map pattern_binders ps)) x) + in + ExprHandle { eh_body = r h.eh_body; + eh_handlers = List.map handler h.eh_handlers } + | ExprResume eo -> ExprResume (ro eo) + | ExprUnsafe ops -> + ExprUnsafe (List.map (function + | UnsafeRead x -> UnsafeRead (r x) + | UnsafeWrite (a, b) -> UnsafeWrite (r a, r b) + | UnsafeOffset (a, b) -> UnsafeOffset (r a, r b) + | UnsafeTransmute (t1, t2, x) -> UnsafeTransmute (t1, t2, r x) + | UnsafeForget x -> UnsafeForget (r x)) ops) + | ExprSpan (x, sp) -> ExprSpan (r x, sp) + +and map_arm look (arm : match_arm) : match_arm = + let inner = shadow look (pattern_binders arm.ma_pat) in + { arm with ma_guard = Option.map (map_free inner) arm.ma_guard; + ma_body = map_free inner arm.ma_body } + +and map_block look (blk : block) : block = + let rec go look acc = function + | [] -> (List.rev acc, look) + | StmtLet sl :: rest -> + let s = StmtLet { sl with sl_value = map_free look sl.sl_value } in + go (shadow look (pattern_binders sl.sl_pat)) (s :: acc) rest + | StmtExpr x :: rest -> go look (StmtExpr (map_free look x) :: acc) rest + | StmtAssign (a, op, b) :: rest -> + go look (StmtAssign (map_free look a, op, map_free look b) :: acc) rest + | StmtWhile (c, b) :: rest -> + go look (StmtWhile (map_free look c, map_block look b) :: acc) rest + | StmtFor (p, it, b) :: rest -> + let s = StmtFor (p, map_free look it, + map_block (shadow look (pattern_binders p)) b) in + go look (s :: acc) rest + in + let stmts, look_after = go look [] blk.blk_stmts in + { blk_stmts = stmts; blk_expr = Option.map (map_free look_after) blk.blk_expr } + +(** Rename free occurrences of variables: [ren] maps old names to new ones, + with the capture rules of {!map_free}. Used by import flattening to + give each module's declarations module-qualified names (#801). *) let without_names (ren : (string * string) list) (names : string list) = List.filter (fun (old, _) -> not (List.mem old names)) ren -let rec rename_free (ren : (string * string) list) (e : expr) : expr = - if ren = [] then e - else - let r = rename_free ren in - let ro = Option.map r in - match e with - | ExprLit _ | ExprBreak _ | ExprContinue _ | ExprVariant _ -> e - | ExprVar id -> - (match List.assoc_opt id.name ren with - | Some n -> ExprVar { id with name = n } - | None -> e) - | ExprLet lb -> - let inner = without_names ren (pattern_binders lb.el_pat) in - ExprLet { lb with el_value = r lb.el_value; - el_body = Option.map (rename_free inner) lb.el_body } - | ExprIf ei -> - ExprIf { ei_cond = r ei.ei_cond; ei_then = r ei.ei_then; - ei_else = ro ei.ei_else } - | ExprMatch m -> - ExprMatch { em_scrutinee = r m.em_scrutinee; - em_arms = List.map (rename_arm ren) m.em_arms } - | ExprLambda lam -> - let inner = - without_names ren (List.map (fun (p : param) -> p.p_name.name) - lam.elam_params) in - ExprLambda { lam with elam_body = rename_free inner lam.elam_body } - | ExprApp (f, args) -> ExprApp (r f, List.map r args) - | ExprField (x, f) -> ExprField (r x, f) - | ExprTupleIndex (x, i) -> ExprTupleIndex (r x, i) - | ExprIndex (a, b) -> ExprIndex (r a, r b) - | ExprTuple es -> ExprTuple (List.map r es) - | ExprArray es -> ExprArray (List.map r es) - | ExprRecord rc -> - let field ((n : ident), eo) = - match eo with - | Some x -> (n, Some (r x)) - | None -> - (match List.assoc_opt n.name ren with - | Some nn -> (n, Some (ExprVar { n with name = nn })) - | None -> (n, None)) - in - ExprRecord { er_fields = List.map field rc.er_fields; - er_spread = ro rc.er_spread } - | ExprRowRestrict (x, f) -> ExprRowRestrict (r x, f) - | ExprBinary (a, op, b) -> ExprBinary (r a, op, r b) - | ExprStringConcat (a, b) -> ExprStringConcat (r a, r b) - | ExprStringEq (a, b, neg) -> ExprStringEq (r a, r b, neg) - | ExprStringRel (a, b, op) -> ExprStringRel (r a, r b, op) - | ExprFloatBinary (a, op, b) -> ExprFloatBinary (r a, op, r b) - | ExprFloatArray es -> ExprFloatArray (List.map r es) - | ExprFloatIndex (a, b) -> ExprFloatIndex (r a, r b) - | ExprCellTuple cs -> ExprCellTuple (List.map (fun (x, f) -> (r x, f)) cs) - | ExprCellTupleIndex (x, i, f) -> ExprCellTupleIndex (r x, i, f) - | ExprCellRecord fs -> - ExprCellRecord (List.map (fun (n, x, f) -> (n, r x, f)) fs) - | ExprCellField (x, i, f) -> ExprCellField (r x, i, f) - | ExprUnary (op, x) -> ExprUnary (op, r x) - | ExprBlock b -> ExprBlock (rename_block ren b) - | ExprReturn eo -> ExprReturn (ro eo) - | ExprTry t -> - ExprTry { et_body = rename_block ren t.et_body; - et_catch = Option.map (List.map (rename_arm ren)) t.et_catch; - et_finally = Option.map (rename_block ren) t.et_finally } - | ExprHandle h -> - let handler = function - | HandlerReturn (p, x) -> - HandlerReturn (p, rename_free (without_names ren (pattern_binders p)) x) - | HandlerOp (op, ps, x) -> - HandlerOp (op, ps, - rename_free - (without_names ren (List.concat_map pattern_binders ps)) x) - in - ExprHandle { eh_body = r h.eh_body; - eh_handlers = List.map handler h.eh_handlers } - | ExprResume eo -> ExprResume (ro eo) - | ExprUnsafe ops -> - ExprUnsafe (List.map (function - | UnsafeRead x -> UnsafeRead (r x) - | UnsafeWrite (a, b) -> UnsafeWrite (r a, r b) - | UnsafeOffset (a, b) -> UnsafeOffset (r a, r b) - | UnsafeTransmute (t1, t2, x) -> UnsafeTransmute (t1, t2, r x) - | UnsafeForget x -> UnsafeForget (r x)) ops) - | ExprSpan (x, sp) -> ExprSpan (r x, sp) - -and rename_arm ren (arm : match_arm) : match_arm = - let inner = without_names ren (pattern_binders arm.ma_pat) in - { arm with ma_guard = Option.map (rename_free inner) arm.ma_guard; - ma_body = rename_free inner arm.ma_body } - -and rename_block ren (blk : block) : block = - if ren = [] then blk - else - let rec go ren acc = function - | [] -> (List.rev acc, ren) - | StmtLet sl :: rest -> - let s = StmtLet { sl with sl_value = rename_free ren sl.sl_value } in - go (without_names ren (pattern_binders sl.sl_pat)) (s :: acc) rest - | StmtExpr x :: rest -> go ren (StmtExpr (rename_free ren x) :: acc) rest - | StmtAssign (a, op, b) :: rest -> - go ren (StmtAssign (rename_free ren a, op, rename_free ren b) :: acc) rest - | StmtWhile (c, b) :: rest -> - go ren (StmtWhile (rename_free ren c, rename_block ren b) :: acc) rest - | StmtFor (p, it, b) :: rest -> - let s = StmtFor (p, rename_free ren it, - rename_block (without_names ren (pattern_binders p)) b) in - go ren (s :: acc) rest - in - let stmts, ren_after = go ren [] blk.blk_stmts in - { blk_stmts = stmts; blk_expr = Option.map (rename_free ren_after) blk.blk_expr } +let rename_free (ren : (string * string) list) (e : expr) : expr = + if ren = [] then e else map_free (fun n -> List.assoc_opt n ren) e + +let rename_block (ren : (string * string) list) (blk : block) : block = + if ren = [] then blk else map_block (fun n -> List.assoc_opt n ren) blk + +(** The free names of [e] (variables, record shorthands, and the type names + of qualified constructors), exactly the occurrences {!rename_free} would + rewrite. May contain duplicates. *) +let free_names (e : expr) : string list = + let acc = ref [] in + ignore (map_free (fun n -> acc := n :: !acc; None) e); + !acc + +let free_names_block (b : block) : string list = + let acc = ref [] in + ignore (map_block (fun n -> acc := n :: !acc; None) b); + !acc (** Free variables of an expression. diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 42613c7b..114575b9 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -101,6 +101,11 @@ type codegen_ctx = { associated call (`(await this.m(...))`) — `await` inside a plain `(() => {...})()` is a SyntaxError ("Unexpected reserved word"). *) in_async : bool; + (* Every top-level JS binding emitted so far (runtime preamble included), + with a signature: [""] for a function, constant or class, the binding + text for an enum constructor. Shared by every copy of the context. + See {!claim_top} (#801). *) + top_names : (string, string) Hashtbl.t; } (** Create an empty emission context for [host], sharing [symbols] and @@ -120,6 +125,7 @@ let create_ctx host symbols = { int_vars = Hashtbl.create 16; int_array_vars = Hashtbl.create 16; in_async = false; + top_names = Hashtbl.create 128; } (* Expression-position IIFE wrapper. In an async method body it must be @@ -1261,6 +1267,67 @@ let js_reserved = [ let mangle (name : string) : string = if List.mem name js_reserved then name ^ "_" else name + +(* ============================================================================ + Top-level name uniqueness (#801) + + An ES module may declare a top-level name once. Import flattening gives + every module's declarations distinct names, and {!mangle} is not + injective (`class` and a user's `class_` both emit `class_`), so this is + the last check before the text exists: a second binding of a name is a + compile error, never a module where one binding silently replaces another + (#801) or fails only when it is loaded. An enum constructor emitted twice + with the same text (two enums of one program each with a nullary `Red`) + is the same runtime value, a `{ tag }` object or factory, so it is emitted + once. + ============================================================================ *) + +(** Reserve the top-level JS name [name]. [signature] is [""] for a + binding that must be unique (a function, constant or class), or the + binding's text for one that may be shared when identical (an enum + constructor). Returns [false] when an identical shared binding has + already been emitted, so the caller emits nothing. + @raise Failure on any other second binding of [name]. *) +let claim_top ctx (name : string) (signature : string) : bool = + match Hashtbl.find_opt ctx.top_names name with + | None -> Hashtbl.replace ctx.top_names name signature; true + | Some s when s <> "" && s = signature -> false + | Some _ -> + failwith + (Printf.sprintf + "two different top-level bindings would both be emitted as `%s`; \ + refusing to emit a module in which one replaces the other (#801)" + name) + +(** The name a column-0 line of emitted runtime text declares at top + level ([const x], [let x], [function x], [class x], optionally + [export]ed or [async]), if any. *) +let top_binding_name (line : string) : string option = + let strip p s = + if String.starts_with ~prefix:p s then + Some (String.sub s (String.length p) (String.length s - String.length p)) + else None + in + let s = Option.value (strip "export " line) ~default:line in + let s = Option.value (strip "async " s) ~default:s in + List.find_map (fun kw -> strip (kw ^ " ") s) + [ "const"; "let"; "var"; "function"; "class" ] + |> Fun.flip Option.bind (fun r -> + let is_id c = + (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') || c = '_' || c = '$' + in + let n = ref 0 in + while !n < String.length r && is_id r.[!n] do incr n done; + if !n = 0 then None else Some (String.sub r 0 !n)) + +(** Reserve every top-level name the runtime text [js] declares. *) +let claim_runtime ctx (js : string) : unit = + List.iter (fun line -> + Option.iter (fun n -> Hashtbl.replace ctx.top_names n "") + (top_binding_name line)) + (String.split_on_char '\n' js) + (* Resolve a variable reference, honouring the active [self] rename. *) let resolve_var ctx (name : string) : string = match ctx.self_name with @@ -2136,6 +2203,7 @@ let gen_body ctx (fb : fn_body) : unit = let gen_function ctx (fd : fn_decl) : unit = let name = mangle fd.fd_name.name in + ignore (claim_top ctx name ""); let params = List.map (fun (p : param) -> mangle p.p_name.name) fd.fd_params in let is_async = fd_is_async fd in @@ -2272,6 +2340,7 @@ let gen_default_constructor ctx (fields : struct_field list) : unit = let gen_class ctx ~(name : string) ~(fields : struct_field list) ~(ctor : fn_decl option) ~(methods : (string * string * fn_decl) list) : unit = + ignore (claim_top ctx name ""); emit_line ctx (Printf.sprintf "export class %s {" name); let cls = increase_indent ctx in (match ctor with @@ -2300,21 +2369,21 @@ let gen_type_decl ctx (td : type_decl) : unit = else begin let name = mangle vd.vd_name.name in let arity = List.length vd.vd_fields in - if arity = 0 then - emit_line ctx - (Printf.sprintf "%sconst %s = { tag: \"%s\" };" exp name - vd.vd_name.name) - else if arity = 1 then - emit_line ctx - (Printf.sprintf "%sconst %s = (value) => ({ tag: \"%s\", value });" - exp name vd.vd_name.name) - else - let ps = List.init arity (fun i -> "v" ^ string_of_int i) in - emit_line ctx - (Printf.sprintf - "%sconst %s = (%s) => ({ tag: \"%s\", values: [%s] });" - exp name (String.concat ", " ps) vd.vd_name.name - (String.concat ", " ps)) + let binding = + if arity = 0 then + Printf.sprintf "const %s = { tag: \"%s\" };" name vd.vd_name.name + else if arity = 1 then + Printf.sprintf "const %s = (value) => ({ tag: \"%s\", value });" + name vd.vd_name.name + else + let ps = List.init arity (fun i -> "v" ^ string_of_int i) in + Printf.sprintf + "const %s = (%s) => ({ tag: \"%s\", values: [%s] });" + name (String.concat ", " ps) vd.vd_name.name + (String.concat ", " ps) + in + (* Two enums' identical constructors are one binding (#801). *) + if claim_top ctx name binding then emit_line ctx (exp ^ binding) end ) variants; emit ctx "\n" @@ -2372,6 +2441,7 @@ let generate (host : host_profile) (program : program) (symbols : Symbol.t) : st emit_line ctx "// SPDX-License-Identifier: MPL-2.0"; emit ctx (match host with Deno -> deno_host_prelude | Bun -> bun_host_prelude); emit ctx common_prelude; + claim_runtime ctx (Buffer.contents ctx.output); (* Collect structs. AffineScript's grammar accepts neither inherent [impl Type {}] nor a [self] expression (SELF_KW has no expression @@ -2455,8 +2525,11 @@ let generate (host : host_profile) (program : program) (symbols : Symbol.t) : st emit_class_for td.td_name.name fields | _ -> gen_type_decl ctx td) | TopImpl ib -> - (* Inherent/trait impls don't parse in the current grammar; this - is a defensive fallback only. *) + (* Impl methods are emitted as free functions under their own + names (trait dispatch is not lowered here), so two impls of one + trait give two bindings of one name: {!gen_function}'s + {!claim_top} refuses that rather than letting Bun keep the + last (#801). *) List.iter (function | ImplFn fd -> gen_function ctx fd | ImplType _ -> ()) ib.ib_items @@ -2467,6 +2540,7 @@ let generate (host : host_profile) (program : program) (symbols : Symbol.t) : st Plain `const` (tc_mut=false) keeps the existing `const` emission and JS-enforces immutability. *) let kw = if tc_mut then "let" else "const" in + ignore (claim_top ctx (mangle tc_name.name) ""); emit_line ctx (Printf.sprintf "%s%s %s = %s;" exp kw (mangle tc_name.name) (gen_expr ctx tc_value)) diff --git a/lib/js_codegen.ml b/lib/js_codegen.ml index c98aa0a4..7e28f2f6 100644 --- a/lib/js_codegen.ml +++ b/lib/js_codegen.ml @@ -618,7 +618,42 @@ let generate (program : program) (symbols : Symbol.t) : string = emit_line ctx "// Generated by AffineScript compiler"; emit_line ctx "// SPDX-License-Identifier: MPL-2.0"; emit ctx prelude; - List.iter (gen_top_level ctx) program.prog_decls; + (* #801: one top-level JS name per binding. A classic script silently + keeps the last of two `function f` declarations, so a second binding of + a name is refused here rather than emitted. An enum constructor of the + same name and arity as one already emitted is the same runtime value + (a `{ tag }` object or factory) and is emitted once. *) + let top_names = Hashtbl.create 64 in + let claim name signature = + match Hashtbl.find_opt top_names name with + | None -> Hashtbl.replace top_names name signature; true + | Some s when s <> "" && s = signature -> false + | Some _ -> + failwith + (Printf.sprintf + "two different top-level bindings would both be emitted as `%s`; \ + refusing to emit a script in which one replaces the other (#801)" + name) + in + List.iter (fun top -> + match top with + | TopFn fd -> + ignore (claim (mangle fd.fd_name.name) ""); + gen_top_level ctx top + | TopConst { tc_name; _ } -> + ignore (claim (mangle tc_name.name) ""); + gen_top_level ctx top + | TopType ({ td_body = TyEnum variants; _ } as td) -> + let fresh = + List.filter (fun (vd : variant_decl) -> + List.mem vd.vd_name.name [ "Some"; "None"; "Ok"; "Err" ] + || claim (mangle vd.vd_name.name) + ("ctor/" ^ string_of_int (List.length vd.vd_fields))) + variants + in + gen_top_level ctx (TopType { td with td_body = TyEnum fresh }) + | _ -> gen_top_level ctx top) + program.prog_decls; (* If a `main` function exists, invoke it so `node foo.js` actually runs. *) let has_main = List.exists (function | TopFn fd -> fd.fd_name.name = "main" diff --git a/lib/module_loader.ml b/lib/module_loader.ml index 13282b9a..ecba4de5 100644 --- a/lib/module_loader.ml +++ b/lib/module_loader.ml @@ -375,17 +375,45 @@ let clear_cache (loader : t) : unit = Hashtbl.clear loader.loaded; Hashtbl.clear loader.loading -(** The name a module's private helper carries once flattened. Flattening - puts the declarations of every imported module into one program, so two - modules' private helpers of the same name (kyq: `parse_int` in both - `Kyaml` and `Kyaml.Parser`) would otherwise collide: one would be - dropped, or the importer's own local of that name would win, and a - module's code would call another module's helper. Underscores keep the - name valid in every backend's target language. *) -let qualified_private_name (mod_path : string list) (name : string) : string = - (* Each path component is prefixed by its length, so the encoding is - injective: joining with `_` made `A_B.C` and `A.B_C` the same. *) - "__priv_" +(* ============================================================================ + Import flattening (#801) + + The non-Wasm backends compile one flattened program, so every module's + top-level declarations share one namespace there. Flattening used to keep + the source names of the declarations it carried and resolve clashes by + dropping one, which made one module's code call another module's function + of the same name with no diagnostic (#801: `Lib.A.a_label` printed + `Lib.B.label`'s text). The scheme below makes a clash impossible by + construction instead of resolving it: + + - Every function and constant of an imported module, public or private, + is carried under its module-qualified name ({!qualified_name}), and every + reference to it, in its own module and in the modules that import it, is + rewritten to that name. The encoding is injective, so declarations of + different modules never share a name. + - The root program's own declarations keep their source names: they are + the compiled unit's host-facing surface (`main`, `activate`, the + exports a harness imports). A name the root imports is also present + under the name the root binds it to, as a copy of the qualified + declaration, so the flattened program keeps the shape it had. + - Extern functions keep their names everywhere: the host binds them by + name (the Vscode, Bun, Deno and MCP-shim stdlib modules declare them). + An alias of an extern rewrites its references to the extern's own name. + - Enum types and constructors keep their names; constructors are values + a backend preamble may already define, and an enum's constructors are + carried with the enum. + + {!check_unique} then refuses a flattened program in which two different + top-level bindings still share a name, so a regression in the scheme + fails loudly instead of silently picking one. + ============================================================================ *) + +(** The name a module's function or constant carries once flattened. Each + path component is prefixed by its length, so the encoding is injective: + joining with `_` made `A_B.C` and `A.B_C` the same. Underscores and + alphanumerics keep the name valid in every backend's target language. *) +let qualified_name (mod_path : string list) (name : string) : string = + "__mod_" ^ String.concat "" (List.map (fun c -> string_of_int (String.length c) ^ c) mod_path) ^ "__" ^ name @@ -394,14 +422,35 @@ let qualified_private_name (mod_path : string list) (name : string) : string = let flatten_error_message = function | ImportCollision (name, a, b) -> Printf.sprintf - "cannot flatten imports: `%s` is defined in both %s and %s" name - (String.concat "::" a) (String.concat "::" b) + "cannot flatten imports: two different top-level bindings named `%s` \ + (from %s and from %s) would share one name in the compiled program" + name + (match a with [] -> "the root module" | a -> String.concat "::" a) + (match b with [] -> "the root module" | b -> String.concat "::" b) | MutableAlias (name, alias, m) -> Printf.sprintf "cannot import the mutable binding `%s` from %s as `%s`: an alias would \ be a separate copy of it; import it under its own name" name (String.concat "::" m) alias +(** Map, through [look], a top-level function or constant's free references; + parameters shadow. The declaration's own name is not touched. *) +let map_top_level_free (look : string -> string option) (decl : top_level) + : top_level = + match decl with + | TopFn fd -> + let inner = + shadow look (List.map (fun (p : param) -> p.p_name.name) fd.fd_params) + in + let body = match fd.fd_body with + | FnExpr e -> FnExpr (map_free inner e) + | FnBlock b -> FnBlock (map_block inner b) + | FnExtern -> FnExtern + in + TopFn { fd with fd_body = body } + | TopConst c -> TopConst { c with tc_value = map_free look c.tc_value } + | other -> other + (** Rename, by [ren] (old name to new), a top-level function or constant's own name and the free references to the renamed names in its body; a parameter that shadows a renamed name is left alone. *) @@ -414,448 +463,391 @@ let rename_top_level (ren : (string * string) list) (decl : top_level) | Some n -> { id with name = n } | None -> id in - match decl with - | TopFn fd -> - let inner = - without_names ren - (List.map (fun (p : param) -> p.p_name.name) fd.fd_params) - in - let body = match fd.fd_body with - | FnExpr e -> FnExpr (rename_free inner e) - | FnBlock b -> FnBlock (rename_block inner b) - | FnExtern -> FnExtern - in - TopFn { fd with fd_name = rename_ident fd.fd_name; fd_body = body } - | TopConst c -> - TopConst { c with tc_name = rename_ident c.tc_name; - tc_value = rename_free ren c.tc_value } + match map_top_level_free (fun n -> List.assoc_opt n ren) decl with + | TopFn fd -> TopFn { fd with fd_name = rename_ident fd.fd_name } + | TopConst c -> TopConst { c with tc_name = rename_ident c.tc_name } | other -> other -(** Give [prog]'s own private functions and constants (those of [own], the - module's unflattened declarations) their {!qualified_private_name}, and - rewrite the references to them in the module's own declarations. - Declarations [prog] carries from other modules are left alone: their - private helpers were qualified when those modules were flattened, and - they cannot refer to this module's private names. Extern functions keep - their names (the host binds them by name). *) -let qualify_private (mod_path : string list) (own : top_level list) - (prog : program) : program = - let ren = - List.filter_map (function - | TopFn fd when fd.fd_vis = Private && fd.fd_body <> FnExtern -> - Some (fd.fd_name.name, qualified_private_name mod_path fd.fd_name.name) - | TopConst { tc_vis = Private; tc_name; _ } -> - Some (tc_name.name, qualified_private_name mod_path tc_name.name) - | _ -> None) own - in - if ren = [] then prog - else - let rename_decl = rename_top_level ren in - let is_own d = List.exists (fun o -> o == d) own in - { prog with - prog_decls = - List.map (fun d -> if is_own d then rename_decl d else d) - prog.prog_decls } - -(** Flatten cross-module imports by inlining the public top-level decls of - each imported module into [prog.prog_decls], with deduplication by name. - - The WASM backend handles cross-module imports via a separate Wasm-import - section ([Codegen.gen_imports]). Every other codegen ([Julia / JS / C / - Rust / OCaml / ReScript / Lua / Bash / Nickel / LLVM / ...]) iterates - [prog.prog_decls] only and would otherwise fail to find imported - functions at codegen time. Inlining here is the simplest correct - semantics for those targets and saves implementing per-backend module - systems. - - The loader must already have been used by [Resolve.resolve_program_with_loader] - so that the cache is populated; this function does NOT re-load on - demand. If a referenced module isn't cached, its imports are silently - skipped — the resolver would have reported the error already. - - Imports are processed in declaration order; later imports replace - earlier imported declarations with the same name. This mirrors the - resolver's name-table order and keeps the two paths deterministic. - Glob/glob collisions are rejected by the resolver before code generation; - this function retains the same last-import policy as a defensive fallback - for callers that flatten an already-loaded program directly. Local decls - in [prog.prog_decls] always win over imported ones. - - Selective imports carry referenced helpers, including private ones, when - [find_free_vars] detects them. That walker is partial: dependencies - referenced only in record spreads, shorthand record fields or qualified - constructors may be omitted. Public enums named directly by type or - constructor in a selective import - are included; enums containing only [Some], [None], [Ok] and [Err] are - excluded from this direct selection because the runtime supplies them. - - [cache] memoises flattened dependencies for this traversal and is updated - in place. [visiting] lists module paths already being traversed; a cycle - uses the cached module's original declarations without further recursion. - Imported declarations are prepended; [prog.prog_imports] is retained. *) -let rec flatten_imports_from - (cache : (string list, program) Hashtbl.t) - (visiting : string list list) (loader : t) - (prog : program) : program = - (* Local-decl names suppress same-named imports of any kind. *) - let local_name_list = - List.filter_map (function - | TopFn fd -> Some fd.fd_name.name - | TopConst { tc_name; _ } -> Some tc_name.name - | TopType td -> Some td.td_name.name - | _ -> None - ) prog.prog_decls - in - let local_names = Hashtbl.create 32 in - List.iter (fun n -> Hashtbl.replace local_names n ()) local_name_list; - (* Keep imported declaration order stable while replacing the declaration - stored for a later duplicate. A plain `already_in` set used to keep the - FIRST declaration here, while resolve.ml kept the LAST one (#743). *) - let imported_by_name = Hashtbl.create 32 in - let imported_origin = Hashtbl.create 32 in - let imported_order = ref [] in - let is_private = function - | `Fn fd -> fd.fd_vis = Private - | `Const (TopConst { tc_vis; _ }) -> tc_vis = Private - | `Type (TopType td) -> td.td_vis = Private - | `Const _ | `Type _ -> false - in - (* The same declaration reaches here more than once in a diamond (`D` - imports `P` and `J`, and `J` imports `P`): flattening is recursive, so - `P`'s helpers arrive both directly and inside `J`. That is one helper, - not a collision. *) - let same_decl a b = match a, b with - | `Fn x, `Fn y -> x == y || x = y - | `Const x, `Const y | `Type x, `Type y -> x == y || x = y - | _ -> false - in - let add_imported origin name decl_kind = - if not (Hashtbl.mem local_names name) then begin - (match Hashtbl.find_opt imported_origin name, - Hashtbl.find_opt imported_by_name name with - | Some other, Some prev - when other <> origin - && (is_private prev || is_private decl_kind) - && not (same_decl prev decl_kind) -> - (* Two modules each carry a private helper of this name: one - flattened program can hold only one, and keeping the later would - make the other module call the wrong function. (Public - collisions are rejected by the resolver; #743's last-wins rule - still applies to them.) *) - raise (Flatten_error (ImportCollision (name, other, origin))) - | _ -> ()); - if not (Hashtbl.mem imported_by_name name) then - imported_order := name :: !imported_order; - Hashtbl.replace imported_by_name name decl_kind; - Hashtbl.replace imported_origin name origin - end - in +(** The free names of a top-level declaration (see {!Ast.free_names}). *) +let top_level_free_names (decl : top_level) : string list = + let acc = ref [] in + ignore (map_top_level_free (fun n -> acc := n :: !acc; None) decl); + !acc + +(** What a name a module exports binds, as an importer sees it. *) +type export = + | XValue of string (** a function or constant, by its qualified name *) + | XExtern of string (** an extern function, by its (host) name *) + | XEnum of string (** an enum type or one of its constructors, by the + enum's name *) + +let exported_vis = function Public | PubCrate -> true | _ -> false + +(* Every non-Wasm backend's runtime preamble defines these constructors. *) +let preamble_ctors = [ "Some"; "None"; "Ok"; "Err" ] + +(** An enum the flattener carries: one with a constructor the preamble does + not already supply. *) +let carried_enum (td : type_decl) : bool = + match td.td_body with + | TyEnum variants -> + List.exists (fun (vd : variant_decl) -> + not (List.mem vd.vd_name.name preamble_ctors)) variants + | _ -> false + +let is_extern = function + | TopFn { fd_body = FnExtern; _ } | TopExternFn _ -> true + | _ -> false + +(** The name a value-level declaration binds: functions (extern or not), + extern functions and constants. *) +let value_name = function + | TopFn fd -> Some fd.fd_name.name + | TopExternFn { ef_name; _ } -> Some ef_name.name + | TopConst { tc_name; _ } -> Some tc_name.name + | _ -> None + +(** The names module [mod_path] exports, read from its own declarations. *) +let exports_of (mod_path : string list) (prog : program) + : (string * export) list = + List.concat_map (function + | TopFn fd when exported_vis fd.fd_vis -> + let n = fd.fd_name.name in + [ (n, if fd.fd_body = FnExtern then XExtern n + else XValue (qualified_name mod_path n)) ] + | TopExternFn { ef_name; _ } -> [ (ef_name.name, XExtern ef_name.name) ] + | TopConst { tc_vis; tc_name; _ } when exported_vis tc_vis -> + [ (tc_name.name, XValue (qualified_name mod_path tc_name.name)) ] + | TopType ({ td_body = TyEnum variants; _ } as td) + when exported_vis td.td_vis && carried_enum td -> + (td.td_name.name, XEnum td.td_name.name) + :: List.map (fun (vd : variant_decl) -> + (vd.vd_name.name, XEnum td.td_name.name)) variants + | _ -> []) + prog.prog_decls + +let import_path = function + | ImportSimple (p, _) | ImportList (p, _) | ImportGlob p -> + List.map (fun (id : ident) -> id.name) p + +(** The names [imports] bind in a module whose own value names are [local] + (an own declaration shadows an import of the same name), each with the + module it comes from and what it binds, in import order. A glob or a + plain `use` binds every export; an item list binds each item under its + alias or its own name. One name bound to two different things is an + {!ImportCollision}: the resolver rejects such a program first, and the + flattener does not pick one. *) +let bindings_of (loader : t) ~(local : string list) (imports : import_decl list) + : (string * (string list * export)) list = + let tbl = Hashtbl.create 32 in + let order = ref [] in List.iter (fun imp -> - let path_strs path = - List.map (fun (id : ident) -> id.name) path - in - let mod_path = match imp with - | ImportSimple (p, _) | ImportList (p, _) | ImportGlob p -> path_strs p - in - match Hashtbl.find_opt loader.loaded mod_path with + let path = import_path imp in + match Hashtbl.find_opt loader.loaded path with | None -> () - | Some lm0 -> - (* Flatten the imported module's own imports first, so the closure - below can reach what *it* uses from other modules: `Nav` importing - `Router.on_url_change`, whose body calls `Tea.subs`, emitted an - undefined `subs`. [visiting] stops an import cycle. *) - let own = lm0.mod_program.prog_decls in - let lm = - if List.mem mod_path visiting then - { lm0 with mod_program = qualify_private mod_path own lm0.mod_program } - else - (* Memoised per flatten call: a module shared by several import - paths (a diamond) is flattened once, not once per path. *) - let flat = match Hashtbl.find_opt cache mod_path with - | Some p -> p - | None -> - let p = flatten_imports_from cache (mod_path :: visiting) loader - lm0.mod_program in - let p = qualify_private mod_path own p in - Hashtbl.replace cache mod_path p; - p - in - { lm0 with mod_program = flat } - in - let public_decls = List.filter_map (fun decl -> - match decl with - | TopFn fd when fd.fd_vis = Public || fd.fd_vis = PubCrate -> - Some (fd.fd_name.name, `Fn fd) - | TopConst { tc_vis; tc_name; _ } - when tc_vis = Public || tc_vis = PubCrate -> - Some (tc_name.name, `Const decl) - | _ -> None - ) lm.mod_program.prog_decls in - (* Transitive closure. A selected declaration's body may call - helpers of the same module that the import list never names: - `use Dom::{div}` pulls in `div`, whose body calls `h`. Leaving - `h` out of the flattened program produced - `ReferenceError: h is not defined` in the emitted module - (tests/codegen-deno/dom_startup_error). Private helpers are - pulled in too — a public wrapper may delegate to one. *) - let module_value_decls = - List.filter_map (fun decl -> - match decl with - | TopFn fd when fd.fd_body <> FnExtern -> - Some (fd.fd_name.name, `Fn fd) - | TopConst { tc_name; _ } as d -> - Some (tc_name.name, `Const d) - | _ -> None - ) lm.mod_program.prog_decls - in - (* Constructors of this module's own enums, keyed by constructor - name. An inlined wrapper body can name a constructor - (`text` returns `VText(content)`) and the constructor exists - only where the enum is declared. Option/Result constructors - are excluded: every non-wasm preamble already defines - Some/None/Ok/Err, and re-emitting them is the duplicate-`const` - crash that reverted type-carrying in #138 — this narrows the - carry to user enums a backend preamble cannot supply. *) - let preamble_ctors = [ "Some"; "None"; "Ok"; "Err" ] in - let ctor_decls = Hashtbl.create 16 in - List.iter (fun decl -> - match decl with - | TopType td -> - (match td.td_body with - | TyEnum variants -> - List.iter (fun (vd : variant_decl) -> - if not (List.mem vd.vd_name.name preamble_ctors) then - Hashtbl.replace ctor_decls vd.vd_name.name - (td.td_name.name, decl) - ) variants - | _ -> ()) - | _ -> ()) lm.mod_program.prog_decls; - let by_name = Hashtbl.create 16 in - List.iter (fun (n, d) -> Hashtbl.replace by_name n d) module_value_decls; - (* Close a selection over the helpers its bodies reference, then emit - it in this module's own declaration order. Declarations must - precede their uses (the checker rejects a forward reference, and - the interpreter binds each function in an environment that holds - only what came before it), so the order a selection was gathered - in is not an order a consumer can use: `api` selected first and - its helper `h` carried second left `h` unbound under `eval`. - Every import kind needs the closure: a glob or a plain `use` - carries the module's public declarations, whose bodies call - private helpers just as a selected item's do. *) - let close_over direct = - let seen = Hashtbl.create 16 in - List.iter (fun (n, _) -> Hashtbl.replace seen n ()) direct; - let rec close pending acc = - match pending with - | [] -> List.rev acc - | (_, dk) :: rest -> - let free = match dk with - | `Fn fd -> - let params = - List.map (fun (p : param) -> p.p_name.name) fd.fd_params - in - (match fd.fd_body with - | FnExtern -> [] - | FnExpr e -> find_free_vars params e - | FnBlock b -> find_free_vars params (ExprBlock b)) - | `Const (TopConst { tc_value; _ }) -> find_free_vars [] tc_value - | `Const _ | `Type _ -> [] - in - let (pending, acc) = - List.fold_left (fun (pending, acc) name -> - if Hashtbl.mem seen name then (pending, acc) - else begin - Hashtbl.replace seen name (); - match Hashtbl.find_opt by_name name with - | Some dep -> ((name, dep) :: pending, (name, dep) :: acc) - | None -> - (* A constructor name: carry the enum it belongs to, - once per type. *) - (match Hashtbl.find_opt ctor_decls name with - | Some (td_name, decl) when not (Hashtbl.mem seen td_name) -> - Hashtbl.replace seen td_name (); - ((td_name, `Type decl) :: pending, - (td_name, `Type decl) :: acc) - | _ -> (pending, acc)) - end - ) (rest, acc) free - in - close pending acc - in - let extras = close direct [] in - let position = Hashtbl.create 32 in - List.iteri (fun i decl -> - let name = match decl with - | TopFn fd -> Some fd.fd_name.name - | TopConst { tc_name; _ } -> Some tc_name.name - | TopType td -> Some td.td_name.name - | _ -> None - in - Option.iter (fun n -> - if not (Hashtbl.mem position n) then Hashtbl.replace position n i) - name) lm.mod_program.prog_decls; - let pos (n, _) = - Option.value (Hashtbl.find_opt position n) ~default:max_int - in - List.stable_sort (fun a b -> compare (pos a) (pos b)) (direct @ extras) - in - let select = match imp with - | ImportGlob _ -> close_over public_decls - | ImportSimple _ -> - (* `use Foo` brings the namespace into scope but doesn't import - specific symbols. For codegens that need them inlined we still - include all public top-level bindings — same as glob. The - resolver determines what's referenceable; codegen just needs - the bodies present. *) - close_over public_decls + | Some lm -> + let ex = exports_of path lm.mod_program in + let bound = match imp with + | ImportGlob _ | ImportSimple _ -> ex | ImportList (_, items) -> - (* Every alias an item is imported under. *) - let aliases_of target = - List.filter_map (fun item -> - match item.ii_alias with - | Some a when item.ii_name.name = target -> Some a.name - | _ -> None) items - in - (* Names the importer binds under their own name. *) - let exposed = - List.filter_map (fun item -> - match item.ii_alias with - | None -> Some item.ii_name.name - | Some _ -> None) items - in - (* An alias is a second declaration: for a mutable binding that - would be a second, separate copy. *) - List.iter (fun item -> - match item.ii_alias, - List.assoc_opt item.ii_name.name public_decls with - | Some a, Some (`Const (TopConst { tc_mut = true; _ })) -> - raise (Flatten_error - (MutableAlias (item.ii_name.name, a.name, mod_path))) - | _ -> ()) items; - let rename_decl bound_name found = - match found with - | `Fn fd -> - `Fn { fd with fd_name = { fd.fd_name with name = bound_name } } - | `Const (TopConst { tc_vis; tc_mut; tc_name; tc_ty; tc_value }) -> - `Const (TopConst { - tc_vis; - tc_mut; - tc_name = { tc_name with name = bound_name }; - tc_ty; - tc_value; - }) - | `Const _ | `Type _ -> - (* `Const` here is unreachable: public_decls only stores - TopConst under `Const`. `Type` decls are never renamed — - they are carried under their own name. *) - found - in - let direct = - List.filter_map (fun item -> - let target = item.ii_name.name in - List.find_opt (fun (n, _) -> n = target) public_decls - |> Option.map (fun (_, found) -> (target, found)) - ) items - in - let closed = close_over direct in - (* This module's own public functions and immutable constants that - the closure carries but the importer does not bind under their - own name (`helper` in `use M::{helper as h, api}`, or a public - helper only `api` calls) get an internal name, with the - references to them rewritten. Left under their own name, two - modules' `helper`s would replace one another and `api` would - call the other module's (CodeRabbit on #797). A mutable binding - keeps its name: it may be imported under that name elsewhere, - and a renamed copy would be separate storage. *) - let hidden = - List.filter_map (function - | TopFn fd - when (fd.fd_vis = Public || fd.fd_vis = PubCrate) - && fd.fd_body <> FnExtern - && not (List.mem fd.fd_name.name exposed) - && List.mem_assoc fd.fd_name.name closed -> - Some (fd.fd_name.name, - qualified_private_name mod_path fd.fd_name.name) - | TopConst { tc_vis = (Public | PubCrate); tc_mut = false; - tc_name; _ } - when not (List.mem tc_name.name exposed) - && List.mem_assoc tc_name.name closed -> - Some (tc_name.name, - qualified_private_name mod_path tc_name.name) - | _ -> None) lm0.mod_program.prog_decls - in - let internalise = function - | `Fn fd -> - (match rename_top_level hidden (TopFn fd) with - | TopFn fd' -> `Fn fd' - | _ -> `Fn fd) - | `Const d -> `Const (rename_top_level hidden d) - | `Type _ as t -> t - in - (* An aliased item (`use M::{helper as h, api}`) is bound under its - alias for the importer; the original stays, under its internal - name, because the module's other declarations (`api`) call it. *) - List.concat_map (fun (n, dk) -> - let dk = internalise dk in - let n' = Option.value (List.assoc_opt n hidden) ~default:n in - (n', dk) :: List.map (fun b -> (b, rename_decl b dk)) (aliases_of n)) - closed + List.filter_map (fun item -> + List.assoc_opt item.ii_name.name ex + |> Option.map (fun x -> + ((match item.ii_alias with + | Some a -> a.name + | None -> item.ii_name.name), x))) + items in - (* Public enums the importer names directly. A constructor in the - import list (`use Geometry::{Up, Down}`) or a type name the - importer constructs was only carried when some carried *function* - happened to construct it, so `Some(Up)` in the importer compiled - to a reference to an undefined `Up`. Enums made only of the - preamble's Option/Result constructors are never carried (every - non-Wasm backend's runtime preamble already defines those). *) - let preamble_only = [ "Some"; "None"; "Ok"; "Err" ] in - let public_enums = List.filter_map (fun decl -> - match decl with - | TopType ({ td_body = TyEnum variants; _ } as td) - when (td.td_vis = Public || td.td_vis = PubCrate) - && List.exists (fun (vd : variant_decl) -> - not (List.mem vd.vd_name.name preamble_only)) variants -> - Some (td.td_name.name, - List.map (fun (vd : variant_decl) -> vd.vd_name.name) variants, - decl) - | _ -> None) lm.mod_program.prog_decls in - let named = match imp with - | ImportList (_, items) -> - List.filter (fun (ty, ctors, _) -> - List.exists (fun item -> - let n = item.ii_name.name in n = ty || List.mem n ctors) items) - public_enums - | ImportSimple _ | ImportGlob _ -> public_enums - in - List.iter (fun (ty, _, decl) -> add_imported mod_path ty (`Type decl)) named; - List.iter (fun (name, decl_kind) -> add_imported mod_path name decl_kind) select - ) prog.prog_imports; - let imported_decls = - List.rev !imported_order - |> List.filter_map (fun name -> - match Hashtbl.find_opt imported_by_name name with - | Some (`Fn fd) -> Some (TopFn fd) - | Some (`Const decl) -> Some decl - | Some (`Type decl) -> Some decl - | None -> None) + List.iter (fun (b, x) -> + if not (List.mem b local) then + match Hashtbl.find_opt tbl b with + | Some (p', x') when (p', x') <> (path, x) -> + raise (Flatten_error (ImportCollision (b, p', path))) + | Some _ -> () + | None -> + Hashtbl.replace tbl b (path, x); + order := b :: !order) + bound) + imports; + List.rev_map (fun b -> (b, Hashtbl.find tbl b)) !order + +(** The renaming that realises [bindings] in the importing module's code: + a value is referred to by its qualified name, an aliased extern by the + extern's own name. *) +let binding_renames bindings = + List.filter_map (fun (b, (_, x)) -> + match x with + | XValue q -> Some (b, q) + | XExtern x when x <> b -> Some (b, x) + | XExtern _ | XEnum _ -> None) + bindings + +(** A declaration in the flattened program, with the module it came from + ([[]] for an anonymous root). *) +type entry = { origin : string list; decl : top_level } + +(** Module [mod_path]'s own carried declarations, qualified: its functions + and constants under {!qualified_name}, its references to them and to + what it imports rewritten accordingly. Externs and enums keep their + names; other declarations (structs, traits, effects, impls) are not + carried. *) +let qualify_module (loader : t) (mod_path : string list) (prog : program) + : entry list = + let local = List.filter_map value_name prog.prog_decls in + let own = + List.filter_map (function + | TopFn fd when fd.fd_body <> FnExtern -> + Some (fd.fd_name.name, qualified_name mod_path fd.fd_name.name) + | TopConst { tc_name; _ } -> + Some (tc_name.name, qualified_name mod_path tc_name.name) + | _ -> None) + prog.prog_decls in - (* #138 follow-up: imported TYPE decls are intentionally NOT inlined here. - An earlier #138 revision carried imported public [TopType]s so the - prog_decls-iterating backends could register their constructors — but the - non-Wasm backends (Deno-ESM / JS / Julia / C / Rust / ...) already emit - the Option/Result constructors from a built-in runtime preamble, so - carrying the prelude types declared `Some`/`None`/`Ok`/`Err` twice - (`SyntaxError: Identifier 'Some' has already been declared` when the - emitted Deno-ESM module is run under node). The Wasm backend learns - imported variant tags natively via [Codegen.gen_imports] (it consumes the - original, un-flattened [prog]); the other backends rely on their preamble. - Re-introducing type-carrying for *user-defined* cross-module enums would - need per-backend constructor dedup first. *) - { prog with prog_decls = imported_decls @ prog.prog_decls } + let ren = + own @ binding_renames (bindings_of loader ~local prog.prog_imports) in + List.filter_map (fun decl -> + match decl with + | TopFn _ | TopConst _ | TopExternFn _ -> + Some { origin = mod_path; decl = rename_top_level ren decl } + | TopType td when carried_enum td -> Some { origin = mod_path; decl } + | _ -> None) + prog.prog_decls + +(** Keep the first of each declaration. A function or constant reached + twice (a diamond: `D` imports `P` and `J`, and `J` imports `P`) is one + declaration; two different ones under one name are an + {!ImportCollision}, which qualification makes unreachable short of a + bug. Externs of one name are one host binding, so later ones are + dropped. Enums are identified by module and name. *) +let merge (entries : entry list) : entry list = + let values = Hashtbl.create 64 in + let types = Hashtbl.create 16 in + List.filter (fun e -> + match e.decl, value_name e.decl with + | TopType td, _ -> + let k = (e.origin, td.td_name.name) in + if Hashtbl.mem types k then false + else (Hashtbl.replace types k (); true) + | d, Some n -> + (match Hashtbl.find_opt values n with + | None -> Hashtbl.replace values n e; true + | Some prev when is_extern prev.decl && is_extern d -> false + | Some prev when prev.decl == d || prev.decl = d -> false + | Some prev -> + raise (Flatten_error (ImportCollision (n, prev.origin, e.origin)))) + | _, None -> true) + entries + +(** Module [mod_path] flattened as a dependency: the qualified declarations + of everything it imports (transitively, dependencies first), then its + own. [cache] memoises per module for one flattening; [visiting] stops + an import cycle, whose modules the outer frames carry. *) +let rec flatten_module cache visiting (loader : t) (mod_path : string list) + : entry list = + match Hashtbl.find_opt cache mod_path with + | Some es -> es + | None -> + (match Hashtbl.find_opt loader.loaded mod_path with + | None -> [] + | Some lm -> + let prog = lm.mod_program in + if List.mem mod_path visiting then qualify_module loader mod_path prog + else begin + let deps = + List.concat_map (fun imp -> + flatten_module cache (mod_path :: visiting) loader + (import_path imp)) + prog.prog_imports + in + let es = merge (deps @ qualify_module loader mod_path prog) in + Hashtbl.replace cache mod_path es; + es + end) + +(** Refuse a flattened program in which two different top-level value + bindings share a name, or a function or constant has an extern's name + (a backend would lower calls to it as host calls). Several externs of + one name are one host binding. This is the loud backstop behind the + qualification scheme: it holds for every backend that compiles the + flattened program. *) +let check_unique (entries : entry list) : unit = + let values = Hashtbl.create 64 in + let externs = Hashtbl.create 32 in + List.iter (fun e -> + match value_name e.decl with + | Some n when is_extern e.decl -> + if not (Hashtbl.mem externs n) then Hashtbl.replace externs n e.origin + | Some n -> + (match Hashtbl.find_opt values n with + | Some o -> raise (Flatten_error (ImportCollision (n, o, e.origin))) + | None -> Hashtbl.replace values n e.origin) + | None -> ()) + entries; + Hashtbl.iter (fun n o -> + match Hashtbl.find_opt externs n with + | Some xo -> raise (Flatten_error (ImportCollision (n, o, xo))) + | None -> ()) + values (** Inline the declarations [prog]'s imports need (transitively) into [prog], - for the backends that compile one flattened program. Uses only modules - already loaded in [loader]; missing modules are silently skipped. - See [flatten_imports_from] for selection and name precedence. *) + for the backends that compile one flattened program (see the section + comment above for the naming scheme). Uses only modules already loaded + in [loader] (by {!Resolve.resolve_program_with_loader}); a module that + is not loaded is skipped, the resolver having reported it. + + The result is: the imported declarations the root reaches, dependencies + 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 + own declarations. A mutable constant is not copied (a copy would be + separate storage): the root's references to it are rewritten to its + qualified name. A local declaration shadows an import of its name. + [prog.prog_imports] is retained. + + Raises {!Flatten_error} on a mutable binding imported under an alias, and + on two different top-level bindings that would share a name. *) let flatten_imports (loader : t) (prog : program) : program = - flatten_imports_from (Hashtbl.create 8) [] loader prog + let root = + match prog.prog_module with + | Some p -> List.map (fun (id : ident) -> id.name) p + | None -> [] + in + let cache = Hashtbl.create 8 in + let visiting = if root = [] then [] else [ root ] in + let pool = + merge + (List.concat_map (fun imp -> + flatten_module cache visiting loader (import_path imp)) + prog.prog_imports) + in + let local = List.filter_map value_name prog.prog_decls in + let bindings = bindings_of loader ~local prog.prog_imports in + let pool_value = Hashtbl.create 64 in + List.iter (fun e -> + match value_name e.decl with + | Some n when not (Hashtbl.mem pool_value n) -> Hashtbl.replace pool_value n e + | _ -> ()) + pool; + (* Realise the root's bindings. *) + let copies = ref [] and ren = ref [] and enum_seeds = ref [] in + List.iter (fun (b, (path, x)) -> + match x with + | XValue q -> + (match Hashtbl.find_opt pool_value q with + | Some { decl = TopFn fd; _ } -> + copies := { origin = path; + decl = TopFn { fd with fd_name = { fd.fd_name with name = b } } } + :: !copies + | Some { decl = TopConst ({ tc_mut = false; _ } as c); _ } -> + copies := { origin = path; + decl = TopConst { c with tc_name = { c.tc_name with name = b } } } + :: !copies + | Some { decl = TopConst ({ tc_mut = true; _ } as c); _ } -> + if q <> qualified_name path b then begin + let own = + let pre = qualified_name path "" in + let pl = String.length pre in + String.sub c.tc_name.name pl (String.length c.tc_name.name - pl) + in + raise (Flatten_error (MutableAlias (own, b, path))) + end; + ren := (b, q) :: !ren + | _ -> ()) + | XExtern xn -> if xn <> b then ren := (b, xn) :: !ren + | XEnum ty -> enum_seeds := (path, ty) :: !enum_seeds) + bindings; + let copies = List.rev !copies in + let root_entries = + List.map (fun decl -> { origin = root; decl = rename_top_level !ren decl }) + prog.prog_decls + in + (* Reachability: carry the pool entries the root reaches. A constructor + or enum-type name resolves in the scope of the module whose code names + it: its own enums, then the enums it imports; failing that (a name the + scopes do not account for), every carried enum that declares it. *) + let enums = Hashtbl.create 16 in + List.iter (fun e -> + match e.decl with + | TopType td -> Hashtbl.replace enums (e.origin, td.td_name.name) e + | _ -> ()) + pool; + let enum_names (td : type_decl) = + td.td_name.name + :: (match td.td_body with + | TyEnum vs -> List.map (fun (vd : variant_decl) -> vd.vd_name.name) vs + | _ -> []) + in + let scopes = Hashtbl.create 16 in + let scope_of m = + match Hashtbl.find_opt scopes m with + | Some s -> s + | None -> + let s = Hashtbl.create 16 in + let imports = + if m = root then prog.prog_imports + else match Hashtbl.find_opt loader.loaded m with + | Some lm -> lm.mod_program.prog_imports + | None -> [] + in + List.iter (fun (b, (path, x)) -> + match x with + | XEnum ty -> Hashtbl.replace s b (path, ty) + | XValue _ | XExtern _ -> ()) + (try bindings_of loader ~local:[] imports + with Flatten_error _ -> []); + Hashtbl.iter (fun (o, ty) e -> + match e.decl with + | TopType td when o = m -> + List.iter (fun n -> Hashtbl.replace s n (o, ty)) (enum_names td) + | _ -> ()) + enums; + Hashtbl.replace scopes m s; + s + in + let key (e : entry) = + match e.decl with + | TopType td -> (e.origin, td.td_name.name, true) + | d -> ([], Option.value (value_name d) ~default:"", false) + in + let reached = Hashtbl.create 64 in + let rec reach (e : entry) = + if not (Hashtbl.mem reached (key e)) then begin + Hashtbl.replace reached (key e) (); + visit e + end + and visit (e : entry) = + List.iter (fun n -> + match Hashtbl.find_opt pool_value n with + | Some d -> reach d + | None -> + (match Hashtbl.find_opt (scope_of e.origin) n with + | Some k -> + Option.iter reach (Hashtbl.find_opt enums k) + | None -> + Hashtbl.iter (fun _ en -> + match en.decl with + | TopType td when List.mem n (enum_names td) -> reach en + | _ -> ()) + enums)) + (top_level_free_names e.decl) + in + List.iter visit copies; + List.iter visit root_entries; + List.iter (fun k -> Option.iter reach (Hashtbl.find_opt enums k)) + (List.rev !enum_seeds); + (* An extern the root declares itself is not carried a second time. *) + let root_externs = + List.filter_map (fun e -> if is_extern e.decl then value_name e.decl else None) + root_entries + in + let carried = + List.filter (fun e -> + Hashtbl.mem reached (key e) + && not (is_extern e.decl + && List.mem (Option.get (value_name e.decl)) root_externs)) + pool + in + let all = carried @ copies @ root_entries in + check_unique all; + { prog with prog_decls = List.map (fun e -> e.decl) all } diff --git a/test/test_module_root_flatten.ml b/test/test_module_root_flatten.ml index 7c5717b5..efdfdec3 100644 --- a/test/test_module_root_flatten.ml +++ b/test/test_module_root_flatten.ml @@ -489,8 +489,8 @@ let aliased_originals_stay_apart () = their private helpers different internal names. *) let internal_names_are_injective () = Alcotest.(check bool) "distinct" true - (Module_loader.qualified_private_name [ "S_T"; "U" ] "h" - <> Module_loader.qualified_private_name [ "S"; "T_U" ] "h"); + (Module_loader.qualified_name [ "S_T"; "U" ] "h" + <> Module_loader.qualified_name [ "S"; "T_U" ] "h"); let dir = fresh_dir "inject" in write_file dir "S_T/U.affine" "module S_T.U;\nfn h() -> Int { 1 }\npub fn u1() -> Int { h() }\n"; @@ -522,8 +522,214 @@ let mutable_alias_refused () = (Module_loader.MutableAlias ("counter", "c", [ "V"; "M" ])) -> ()) +(* ---- #801: one flattened namespace, collision-free ---------------------- *) + +(* Two modules each define a function of one name; each module's code must + call its own. [label] is public in both, as in #801's reproduction, then + private in both (same failure, reported in the issue too). *) +let label_modules dir vis = + write_file dir "Lib/A.affine" + (Printf.sprintf "module Lib.A;\n%sfn label() -> Int { 1 }\n\ + pub fn a_label() -> Int { label() }\n" vis); + write_file dir "Lib/B.affine" + (Printf.sprintf "module Lib.B;\n%sfn label() -> Int { 2 }\n\ + pub fn b_label() -> Int { label() }\n" vis) + +let label_main = + "module Main;\nuse Lib.A::{a_label};\nuse Lib.B::{b_label};\n\ + pub fn main() -> Int { a_label() * 10 + b_label() }\n" + +(* Compile [src] (with the modules under [dir]) to Bun-ESM and run it under + node with [expr] printed. *) +let run_flat_js dir src expr = + 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, _) -> + (match Codegen_deno.codegen_bun (Module_loader.flatten_imports ld prog) + rctx.symbols with + | Error e -> Alcotest.failf "Bun-ESM codegen: %s" e + | Ok js -> + (* Drop the auto-call of main: the test prints what it needs. *) + let js = + String.concat "\n" + (List.filter (fun l -> l <> "await main();") + (String.split_on_char '\n' js)) + in + run_node (js ^ "\nconsole.log(" ^ expr ^ ");\n")) + +let test_801_same_name_in_two_modules () = + List.iter (fun vis -> + let dir = fresh_dir ("i801" ^ String.trim vis) in + label_modules dir vis; + eval_int dir label_main 12; + if node_available () then + Alcotest.(check string) ("Bun-ESM, " ^ vis ^ "label") "12" + (run_flat_js dir label_main "main()")) + [ "pub "; "" ] + +(* The shape #801's reproduction has on main after #797: the colliding + functions reach the root only through other modules' bodies. X uses + A's [f] and Y uses B's; the old flattener kept one [f] for both. *) +let test_801_transitive_public_and_const () = + let dir = fresh_dir "t801" in + write_file dir "Lib/A.affine" + "module Lib.A;\npub fn f() -> Int { 1 }\npub const K: Int = 100;\n"; + write_file dir "Lib/B.affine" + "module Lib.B;\npub fn f() -> Int { 2 }\npub const K: Int = 200;\n"; + write_file dir "Lib/X.affine" + "module Lib.X;\nuse Lib.A::{f, K};\npub fn via_a() -> Int { f() + K }\n"; + write_file dir "Lib/Y.affine" + "module Lib.Y;\nuse Lib.B::*;\npub fn via_b() -> Int { f() + K }\n"; + let src = + "module Main;\nuse Lib.X::{via_a};\nuse Lib.Y::{via_b};\n\ + pub fn main() -> Int { via_a() * 1000 + via_b() }\n" + in + eval_int dir src 101202; + if node_available () then + Alcotest.(check string) "Bun-ESM" "101202" (run_flat_js dir src "main()") + +(* Externs keep their host names: an alias is rewritten to the extern's own + name, never carried as an extern of the alias's name. *) +let test_801_externs_keep_host_names () = + let dir = fresh_dir "x801" in + write_file dir "H.affine" + "module H;\npub extern fn host_thing(x: Int) -> Int;\n\ + pub fn twice(x: Int) -> Int { host_thing(host_thing(x)) }\n"; + let src = + "module Main;\nuse H::{host_thing as ht, twice};\n\ + pub fn main() -> Int { ht(1) + twice(2) }\n" + in + 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 _ -> ()); + let flat = Module_loader.flatten_imports ld prog in + let externs, others = + List.partition Module_loader.is_extern flat.Ast.prog_decls in + Alcotest.(check (list string)) "one extern, under its host name" + [ "host_thing" ] (List.filter_map Module_loader.value_name externs); + Alcotest.(check bool) "no declaration named after the alias" false + (List.mem "ht" (List.filter_map Module_loader.value_name others)); + let main_refs = + List.concat_map (function + | Ast.TopFn fd when fd.fd_name.name = "main" -> + Module_loader.top_level_free_names (Ast.TopFn fd) + | _ -> []) flat.prog_decls + in + Alcotest.(check bool) "main calls the extern by its host name" true + (List.mem "host_thing" main_refs && not (List.mem "ht" main_refs)) + +(* The backstop: a flattened program in which two different bindings share a + name is refused, whatever produced it. *) +let test_801_planted_collision_refused () = + let fn_decl src = + match (Parse_driver.parse_string ~file:"

" src).prog_decls with + | [ d ] -> d + | _ -> Alcotest.fail "expected one declaration" + in + let a = fn_decl "fn label() -> Int { 1 }" in + let b = fn_decl "fn label() -> Int { 2 }" in + (match + Module_loader.check_unique + [ { origin = [ "Lib"; "A" ]; decl = a }; { origin = [ "Lib"; "B" ]; decl = b } ] + with + | () -> Alcotest.fail "two different `label`s must be refused" + | exception Module_loader.Flatten_error + (Module_loader.ImportCollision ("label", _, _)) -> ()); + (* A function with an extern's name: a backend would lower calls to it as + host calls. The root's own `host_thing` meets the extern H carries. *) + let dir = fresh_dir "p801" in + write_file dir "H.affine" + "module H;\nextern fn host_thing(x: Int) -> Int;\n\ + pub fn twice(x: Int) -> Int { host_thing(host_thing(x)) }\n"; + let src = + "module Main;\nuse H::{twice};\nfn host_thing(x: Int) -> Int { x }\n\ + pub fn main() -> Int { twice(host_thing(2)) }\n" + in + 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 _ -> ()); + (match Module_loader.flatten_imports ld prog with + | _ -> Alcotest.fail "a function named like a carried extern must be refused" + | exception Module_loader.Flatten_error + (Module_loader.ImportCollision ("host_thing", _, _)) -> ()); + (* The JS emitters refuse a duplicate name even when handed one directly + (Bun keeps the last `function` of a name without complaint). *) + let dup = { (Parse_driver.parse_string ~file:"" "") with + Ast.prog_decls = [ a; b ] } in + let refused what = function + | Ok _ -> Alcotest.failf "%s emitted two `label`s" what + | Error msg -> + Alcotest.(check bool) (what ^ ": " ^ msg) true + (Str.string_match (Str.regexp ".*`label`.*#801") msg 0) + in + refused "Bun-ESM" (Codegen_deno.codegen_bun dup (Symbol.create ())); + refused "JS" (Js_codegen.codegen_js dup (Symbol.create ())) + +(* Enum constructors keep their names. Two enums' identical constructors are + one runtime value and are emitted once; constructors of one name and + different shapes are refused. *) +let test_801_enum_constructors () = + let dir = fresh_dir "e801" in + write_file dir "Lib/A.affine" + "module Lib.A;\npub enum Col { Red, Blue }\n\ + pub fn a_label() -> Int { match Red { Red => 1, Blue => 3 } }\n"; + write_file dir "Lib/B.affine" + "module Lib.B;\npub enum Paint { Blue, Red }\n\ + pub fn b_label() -> Int { match Blue { Red => 4, Blue => 2 } }\n"; + if node_available () then + Alcotest.(check string) "identical constructors shared" "12" + (run_flat_js dir label_main "main()"); + write_file dir "Lib/B.affine" + "module Lib.B;\npub enum Paint { Blue(Int), Red }\n\ + pub fn b_label() -> Int { match Red { Red => 2, Blue(n) => n } }\n"; + let prog = Parse_driver.parse_string ~file:"
" label_main 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, _) -> + (match Codegen_deno.codegen_bun (Module_loader.flatten_imports ld prog) + rctx.symbols with + | Ok _ -> Alcotest.fail "`Blue` and `Blue(Int)` must not share a binding" + | Error msg -> + Alcotest.(check bool) msg true + (Str.string_match (Str.regexp ".*`Blue`.*#801") msg 0)) + +(* Trait impl methods are emitted as free functions under their own names + (trait dispatch is not lowered on the non-Wasm backends), so two impls of + one trait gave two `function eq`s and Bun silently kept the second. They + are refused until impl methods get their own names. *) +let test_801_impl_methods_refused () = + let src = {| +module impls; +trait Same { fn same(ref self, ref other: Self) -> Bool; } +impl Same for Int { pub fn same(ref self, ref other: Int) -> Bool { self == other } } +impl Same for Bool { pub fn same(ref self, ref other: Bool) -> Bool { self == other } } +|} in + match compile_deno src with + | Ok _ -> Alcotest.fail "two impls' `same` must not both be emitted as `same`" + | Error msg -> + Alcotest.(check bool) msg true + (Str.string_match (Str.regexp ".*`same`.*#801") msg 0) + let tests = [ + Alcotest.test_case "#801: same-named functions in two modules" `Quick + test_801_same_name_in_two_modules; + Alcotest.test_case "#801: transitive public functions and constants" `Quick + test_801_transitive_public_and_const; + Alcotest.test_case "#801: externs keep their host names" `Quick + test_801_externs_keep_host_names; + Alcotest.test_case "#801: a planted collision is refused" `Quick + test_801_planted_collision_refused; + Alcotest.test_case "#801: enum constructors" `Quick test_801_enum_constructors; + Alcotest.test_case "#801: impl methods sharing a name are refused" `Quick + test_801_impl_methods_refused; Alcotest.test_case "Bun-ESM: a guard ignores the body's int tracking" `Quick guard_ignores_body_int_tracking; Alcotest.test_case "Bun-ESM: return inside an array arm refused" `Quick diff --git a/test/test_stdlib_aot.ml b/test/test_stdlib_aot.ml index fc90b2fa..35c07973 100644 --- a/test/test_stdlib_aot.ml +++ b/test/test_stdlib_aot.ml @@ -133,18 +133,37 @@ let stdlib_files () = |> List.sort compare |> List.map (fun f -> Filename.concat stdlib_dir f) +(* Stdlib files the Bun-ESM backend must refuse, with the text the refusal + must carry. traits.affine has `impl Eq for Int` and `impl Eq for Bool`; + impl methods are emitted as free functions under their own names (trait + dispatch is not lowered on the non-Wasm backends), so both would be + `function eq`, and Bun keeps the second without complaint: `Int`'s `eq` + was silently `Bool`'s. The emitter refuses that (#801); this pin flips + when impl methods get distinct names. *) +let expected_refusals = [ ("traits.affine", "`eq`") ] + let check_one path () = let result = match parse_file_safe path with | Error m -> Error m | Ok prog -> pipeline_to_deno prog in - match result with - | Ok js -> + match List.assoc_opt (Filename.basename path) expected_refusals, result with + | Some needle, Ok _ -> + Alcotest.failf "%s compiled, but it must be refused (two bindings named %s); \ + if impl methods now have distinct names, drop the pin" + (Filename.basename path) needle + | Some needle, Error m -> + Alcotest.(check bool) + (Printf.sprintf "%s refused loudly: %s" (Filename.basename path) m) + true + (Str.string_match + (Str.regexp (".*" ^ Str.quote needle ^ ".*#801")) m 0) + | None, Ok js -> Alcotest.(check bool) (Printf.sprintf "%s emits a non-empty ES module" (Filename.basename path)) true (String.length js > 0) - | Error m -> + | None, Error m -> Alcotest.failf "AOT pipeline failed for %s: %s" (Filename.basename path) m diff --git a/tests/codegen-bun/Lib801/A.affine b/tests/codegen-bun/Lib801/A.affine new file mode 100644 index 00000000..18e146ed --- /dev/null +++ b/tests/codegen-bun/Lib801/A.affine @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801 regression fixture: `label` exists in Lib801.A and Lib801.B, each +// called by its own module. +module Lib801.A; +pub fn label() -> String { "from A" } +pub fn a_label() -> String { label() } +fn helper() -> String { "A helper" } +pub fn a_helper() -> String { helper() } diff --git a/tests/codegen-bun/Lib801/B.affine b/tests/codegen-bun/Lib801/B.affine new file mode 100644 index 00000000..f4c8a226 --- /dev/null +++ b/tests/codegen-bun/Lib801/B.affine @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801 regression fixture: see Lib801/A.affine. +module Lib801.B; +pub fn label() -> String { "from B" } +pub fn b_label() -> String { label() } +fn helper() -> String { "B helper" } +pub fn b_helper() -> String { helper() } diff --git a/tests/codegen-bun/Lib801/X.affine b/tests/codegen-bun/Lib801/X.affine new file mode 100644 index 00000000..a2ceec50 --- /dev/null +++ b/tests/codegen-bun/Lib801/X.affine @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801 regression fixture: reaches Lib801.A's `label` through an import. +module Lib801.X; +use Lib801.A::{ label }; +pub fn via_a() -> String { label() } diff --git a/tests/codegen-bun/Lib801/Y.affine b/tests/codegen-bun/Lib801/Y.affine new file mode 100644 index 00000000..83eb4ca5 --- /dev/null +++ b/tests/codegen-bun/Lib801/Y.affine @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801 regression fixture: reaches Lib801.B's `label` through a glob import. +module Lib801.Y; +use Lib801.B::*; +pub fn via_b() -> String { label() } diff --git a/tests/codegen-bun/Lib801c/A.affine b/tests/codegen-bun/Lib801c/A.affine new file mode 100644 index 00000000..3d32e01c --- /dev/null +++ b/tests/codegen-bun/Lib801c/A.affine @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801 negative fixture: constructor `Blue` of arity 0 here, 1 in B. +module Lib801c.A; +pub enum Col { Red, Blue } +pub fn a_tag() -> Int { match Blue { Red => 1, Blue => 2 } } diff --git a/tests/codegen-bun/Lib801c/B.affine b/tests/codegen-bun/Lib801c/B.affine new file mode 100644 index 00000000..a7e84af8 --- /dev/null +++ b/tests/codegen-bun/Lib801c/B.affine @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801 negative fixture: see Lib801c/A.affine. +module Lib801c.B; +pub enum Paint { Blue(Int), Red } +pub fn b_tag() -> Int { match Blue(3) { Red => 1, Blue(n) => n } } diff --git a/tests/codegen-bun/binding_collision.affine b/tests/codegen-bun/binding_collision.affine new file mode 100644 index 00000000..359a9340 --- /dev/null +++ b/tests/codegen-bun/binding_collision.affine @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801 negative check: Lib801c.A's `Blue` and Lib801c.B's `Blue(Int)` would +// both be the top-level binding `Blue`. Compilation must fail with the +// emitter's refusal, not produce a module where one replaces the other. +module binding_collision; +use Lib801c.A::{ a_tag }; +use Lib801c.B::{ b_tag }; + +pub fn tags() -> Int { a_tag() * 10 + b_tag() } diff --git a/tests/codegen-bun/same_name_modules.affine b/tests/codegen-bun/same_name_modules.affine new file mode 100644 index 00000000..cf4a7c41 --- /dev/null +++ b/tests/codegen-bun/same_name_modules.affine @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: MPL-2.0 +// Regression for #801: two modules each define `label` (public) and `helper` +// (private). The flattened Bun-ESM program kept one `function label`, and +// Bun keeps the last of two same-named declarations without complaint, so +// both modules called Lib801.B's: "from B" twice. X and Y reach the two +// `label`s only through their own imports (the shape that still collided +// after #797). +module same_name_modules; +use Lib801.A::{ a_label, a_helper }; +use Lib801.B::{ b_label, b_helper }; +use Lib801.X::{ via_a }; +use Lib801.Y::{ via_b }; + +pub fn labels() -> String { + a_label() ++ "|" ++ b_label() ++ "|" ++ a_helper() ++ "|" ++ b_helper() + ++ "|" ++ via_a() ++ "|" ++ via_b() +} diff --git a/tests/codegen-bun/same_name_modules.harness.mjs b/tests/codegen-bun/same_name_modules.harness.mjs new file mode 100644 index 00000000..03e775c5 --- /dev/null +++ b/tests/codegen-bun/same_name_modules.harness.mjs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: MPL-2.0 +// #801: each module's code calls its own `label` and `helper`. +import * as subject from "./same_name_modules.bun.js"; + +const want = "from A|from B|A helper|B helper|from A|from B"; +const got = subject.labels(); +if (got !== want) { + throw new Error(`same_name_modules: got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`); +} +console.log("same_name_modules: ok"); diff --git a/tools/run_codegen_bun_tests.sh b/tools/run_codegen_bun_tests.sh index 9ab77a13..95c47bfd 100755 --- a/tools/run_codegen_bun_tests.sh +++ b/tools/run_codegen_bun_tests.sh @@ -105,6 +105,18 @@ if "${COMPILE_CMD[@]}" "$TEST_DIR/unsupported_host.affine" \ fail "unsupported Bun host operation compiled successfully" fi +# 8. Two different top-level bindings of one name must not compile (#801): +# Bun keeps the last of two same-named declarations without complaint, so +# an emitted collision is a silent wrong call, not a load error. +collision_log="$TEST_DIR/binding_collision.log" +if "${COMPILE_CMD[@]}" "$TEST_DIR/binding_collision.affine" \ + -o "$TEST_DIR/binding_collision.bun.js" --bun-esm >"$collision_log" 2>&1; then + fail "colliding top-level bindings compiled successfully (#801)" +elif ! grep -q '#801' "$collision_log"; then + fail "binding_collision refusal lacks its #801 diagnostic" + sed 's/^/ /' "$collision_log" >&2 +fi + if [ "${#failures[@]}" -gt 0 ]; then echo "" echo "✗ ${#failures[@]} native Bun-ESM check(s) failed:" diff --git a/tools/soundness-anchors.sha256 b/tools/soundness-anchors.sha256 index af58d440..441c434a 100644 --- a/tools/soundness-anchors.sha256 +++ b/tools/soundness-anchors.sha256 @@ -3,11 +3,15 @@ # Regenerate: tools/check-soundness-ledger.sh --reseal # Format: . FIXTURE = file hash; TEST = the # test's function-body hash; SUITE = whole-file hash. +09f299eaf7e9a0d912db441f346438e31ea7157f6f730ab1b6d78ef6d22a76a1 test:test_801_planted_collision_refused 0d3623cf36a67860106d64bb32d30633978bb426d4ddd77ecc2bf6e48e1b7263 fixture:test/e2e/fixtures/borrow_callee_returned_borrow_uam.affine +2dfe36a21983a7dfb7ffa1157f7ddec9865a531a92682976b8fd677e4825e78d test:test_801_impl_methods_refused +546939eaa3c64c82edd23da0a3ba7f74bcf7378cfe1e426132dd1a9f8fd46050 test:test_801_transitive_public_and_const 5b84a4f71aa65f9aa9383e31c0cb624291c4f9d0de7cca4a0ea6a76fe85aea35 fixture:test/e2e/fixtures/handle_resume_nontail.affine 68f0f4ad2462f146b87f491b7456aff9a56b77397344d22c62f4f4ff5ca6e3ae fixture:test/e2e/fixtures/assume_rejected.affine 7f416f186a15511ec5c64d2b96bfffbdf9e7c8c0879951d56c80e46129e2ea48 test:test_stub_backend_return_fenced 80541ee00580b4dccee4657cbd31d11a1ddaa18cc49813b48f686c9509f314dc suite:test/test_borrow_polonius.ml +832d4c8b64176706535334a5dbbb16345f42b75ac9b1a93f0a5dc371d84b5a0d test:test_801_same_name_in_two_modules 874a5531cd8bc402adf4b66277b21b15d921efe06a7128ea6583bcd6e55781ba test:test_resume_nontail_xfail ac1da10c3206605304f11e869f6aa07acba57f6f8248bb2dfacca19984f86c31 test:test_coherence_duplicate_rejected b76c7824a9b1570d66752359a28d75ebdfaedb5de5ad68c343a0a2c26ad4221c fixture:test/e2e/fixtures/async_sync_fallback.affine