From 4ee0e22fd8aa22887f98ec2bc67eb408704de323 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 18:53:18 +0000 Subject: [PATCH 1/2] fix: module flattening, Bun-ESM match lowering and loader follow-ups to #788 Review follow-ups to #788, which merged before they were pushed. Module loader and flattening: - Private helpers carried into a flattened program are renamed `__priv___` by a capture-avoiding substitution (`Ast.rename_free`), so two modules with a same-named private helper (kyq's two `parse_int`s) no longer collide. A remaining collision is a loud error, not a silent pick. - Nested candidates are searched in every directory before any parent-module fallback; a failed load clears its loading marker, so a later import is not reported as a cycle. - An aliased import emits the original binding before the alias, in declaration order; free-variable analysis covers record shorthand and spread. - Only the root program's `main` is run by `eval`; the module root is normalised and the source directory is searched. Bun-ESM backend: - Match is lowered as a labelled block, so guards see the arm's bindings and a `return` in an arm leaves the function. - `let _ = e` still evaluates `e`; temporaries are fresh per lowering. - A `return` in an `if` condition, a scrutinee or a guard is refused loudly instead of being miscompiled. Interpreter: the untypeable Char-ordering arms are removed. Docs: the module-loader holes are renumbered H20-H22 -> H23-H25 in docs/COMPLETION-PLAN.adoc, because #787 assigned H20-H22 to the proof-review holes; H2, H23 and H24 record the round-2 fixes. Tests: test/test_module_root_flatten.ml (22 cases). Disabling qualification, the two-phase search, alias ordering, wildcard lowering or the marker clear each makes a test fail. Full suite: no failures beyond the 33 pre-existing walker-* tests (they need the tree-sitter CLI; same count on main). kyq parser and lint drivers: 20/20 under Bun. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P4yFkxW5Heb8p2YW7ZYs9F --- bin/main.ml | 28 ++-- docs/COMPLETION-PLAN.adoc | 8 +- lib/ast.ml | 142 +++++++++++++++++++- lib/codegen_deno.ml | 58 ++++++-- lib/interp.ml | 13 +- lib/module_loader.ml | 196 ++++++++++++++++++++++++--- test/test_module_root_flatten.ml | 223 ++++++++++++++++++++++++++++++- 7 files changed, 609 insertions(+), 59 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 0ee9586d..189e3ef1 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -173,25 +173,23 @@ let parse_file (face : Affinescript.Face.face) path = Affinescript.Span.pp_short span msg; `Error (false, "Parse error") -(** Build a loader rooted at the source file's directory. The historical - CLI used the process CWD as [current_dir], so compiling `src/Main.affine` - could not find a sibling `src/Helper.affine` even though the module - loader already supported relative sibling paths (#642). *) +(** Build the loader for a source file: rooted at its package root when its + `module` declaration matches its directories, with its own directory + still searchable (#642). See [Module_loader.config_for_file]. *) let loader_config_for_file ?prog path = - let absolute_path = - if Filename.is_relative path then Filename.concat (Sys.getcwd ()) path - else path - in - let config = Affinescript.Module_loader.default_config () in let mod_path = Option.bind prog (fun (p : Affinescript.Ast.program) -> Option.map (List.map (fun (id : Affinescript.Ast.ident) -> id.name)) p.prog_module) in - { config with - current_dir = - Affinescript.Module_loader.module_root_dir - ~file_dir:(Filename.dirname absolute_path) mod_path } + Affinescript.Module_loader.config_for_file ~file_path:path mod_path + +(* Auto-call `main` only when the root program declares it + (Module_loader.declares_main). *) +let lookup_root_main (prog : Affinescript.Ast.program) env = + if Affinescript.Module_loader.declares_main prog + then Affinescript.Value.lookup_env "main" env + else Error (Affinescript.Value.UnboundVariable "main") (** Type-check a file. With [--json], emits a structured diagnostic report on stderr. *) @@ -328,7 +326,7 @@ let eval_file face json path = (Affinescript.Module_loader.flatten_imports loader prog) with | Ok env -> (* Auto-call main() if defined — entry point for TEA apps and scripts *) - (match Affinescript.Value.lookup_env "main" env with + (match lookup_root_main prog env with | Ok main_fn -> (match Affinescript.Interp.apply_function main_fn [] with | Ok _ -> () @@ -378,7 +376,7 @@ let eval_file face json path = (Affinescript.Module_loader.flatten_imports loader prog) with | Ok env -> (* Auto-call main() if defined — entry point for TEA apps and scripts *) - (match Affinescript.Value.lookup_env "main" env with + (match lookup_root_main prog env with | Ok main_fn -> (match Affinescript.Interp.apply_function main_fn [] with | Ok _ -> `Ok () diff --git a/docs/COMPLETION-PLAN.adoc b/docs/COMPLETION-PLAN.adoc index efadf0d5..084e5869 100644 --- a/docs/COMPLETION-PLAN.adoc +++ b/docs/COMPLETION-PLAN.adoc @@ -202,7 +202,7 @@ Results were consistent, but they do not prove CI is green. All were observed by RUN unless marked otherwise. The maps confirm that H1–H4 and H9 are absent from `docs/SOUNDNESS.adoc`. H5–H8, H10, H12 and H13 were not checked against the ledger. H11 already has a drafted issue (`issues-drafts/06`, referenced from the AFFIRMATION). H14, H15 and H19 belong to typed-wasm, not the AffineScript ledger. . H1. Interpreter early `return` has no non-local exit (`lib/interp.ml:262`). -. H2. Bun-ESM `return` in expression position is lowered as an IIFE (`lib/codegen_deno.ml:1575`). *Narrowed 2026-11-09:* `let` initializers and function and lambda tails are now lowered to statements, so the `return` leaves the function; a match used as an argument or operand with a `return` in an arm is refused loudly. Anchor: `test/test_module_root_flatten.ml`. +. H2. Bun-ESM `return` in expression position is lowered as an IIFE (`lib/codegen_deno.ml:1575`). *Narrowed 2026-11-09:* `let` initializers and function and lambda tails are now lowered to statements, so the `return` leaves the function; a match used as an argument or operand with a `return` in an arm is refused loudly. *Round 2 (2026-11-16):* match arms and guards are lowered as a labelled block, so a guard sees the arm's bindings and a `return` in an arm leaves the function; `let _ = e` still evaluates `e`; a `return` in an `if` condition, a scrutinee or a guard is refused loudly. Anchor: `test/test_module_root_flatten.ml`. . H3. The JS-text backend drops block returns (`lib/js_codegen.ml`). The Lua backend emits invalid code. . H4. Non-exhaustive match compiles to "return 0" on core WASM. . H5. Effect rows are not checked against performed operations or callee rows. @@ -220,9 +220,9 @@ All were observed by RUN unless marked otherwise. The maps confirm that H1–H4 . H17. The lexer accepts the bad escape `\q`, and an empty `match` parses (conformance invalid cases 010 and 011). . 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. -. H20 (found and fixed 2026-11-09, checking and running metadatastician/kyq). 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. Anchor: `test/test_module_root_flatten.ml`. -. H21 (found and fixed 2026-11-09). `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. Anchor: `test/test_module_root_flatten.ml`. -. H22 (found and fixed 2026-11-09). 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`. +. 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, and an aliased import emits the original before the alias. 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/lib/ast.ml b/lib/ast.ml index 2e2b7822..07ec17a0 100644 --- a/lib/ast.ml +++ b/lib/ast.ml @@ -532,6 +532,16 @@ let rec expr_contains_return (e : expr) : bool = || List.exists (fun arm -> (match arm.ma_guard with Some g -> expr_contains_return g | None -> false) || expr_contains_return arm.ma_body) em_arms + (* Aggregates: `[return 1]`, `(x, return y)`, `#{ f: return z }` and + projections of them return just as directly. *) + | ExprTuple es | ExprArray es -> List.exists expr_contains_return es + | ExprRecord { er_fields; er_spread } -> + List.exists (fun (_, e) -> + match e with Some e -> expr_contains_return e | None -> false) er_fields + || (match er_spread with Some e -> expr_contains_return e | None -> false) + | ExprField (e, _) | ExprTupleIndex (e, _) -> expr_contains_return e + | ExprIndex (a, b) | ExprStringConcat (a, b) -> + expr_contains_return a || expr_contains_return b | _ -> false and block_contains_return (blk : block) : bool = @@ -566,6 +576,127 @@ 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. *) +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 } + (** Free variables of an expression. Returns the names used in [expr] but not bound within it; [bound_vars] @@ -659,11 +790,18 @@ let rec find_free_vars (bound_vars : string list) (expr : expr) : string list = | ExprTuple exprs | ExprArray exprs -> List.concat (List.map (find_free_vars bound_vars) exprs) | ExprRecord r -> - List.concat (List.map (fun (_, e_opt) -> + (* A shorthand field `#{ x }` reads the variable `x`, and a spread + `..base` reads `base`: both are free uses, which import flattening + needs to carry the helper or constant they name. *) + List.concat (List.map (fun ((name : ident), e_opt) -> match e_opt with | Some e -> find_free_vars bound_vars e - | None -> [] + | None -> + if List.mem name.name bound_vars then [] else [name.name] ) r.er_fields) + @ (match r.er_spread with + | Some e -> find_free_vars bound_vars e + | None -> []) | ExprField (e, _) -> find_free_vars bound_vars e | ExprTupleIndex (e, _) -> find_free_vars bound_vars e | ExprIndex (e1, e2) -> diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 61178909..eadf2e95 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -1906,6 +1906,10 @@ and gen_tail_to ctx (sink : string -> string) e = | None -> Buffer.add_string b (sink "Unit")); "{ " ^ Buffer.contents b ^ " }" | ExprIf { ei_cond; ei_then; ei_else } -> + (* The condition is emitted as an expression, where a `return` would + leave an IIFE instead of the function. *) + if expr_contains_return ei_cond then + refuse_return "an `if` condition"; let elseb = match ei_else with | Some el -> gen_tail_to ctx sink el | None -> sink "Unit" @@ -1913,22 +1917,43 @@ and gen_tail_to ctx (sink : string -> string) e = "if (" ^ gen_expr ctx ei_cond ^ ") { " ^ gen_tail_to ctx sink ei_then ^ " } else { " ^ elseb ^ " }" | ExprMatch { em_scrutinee; em_arms } -> + if expr_contains_return em_scrutinee then + refuse_return "a match scrutinee"; + if List.exists (fun arm -> + match arm.ma_guard with + | Some g -> expr_contains_return g + | None -> false) em_arms + then refuse_return "a match guard"; incr let_tmp_counter; - let sv = Printf.sprintf "__scrut_%d" !let_tmp_counter in - let rec arms_js = function - | [] -> "throw new Error(\"non-exhaustive match\");" - | arm :: rest -> - let cond = gen_pattern_test sv arm.ma_pat in - let binds = gen_pattern_bindings sv arm.ma_pat in - let guard = match arm.ma_guard with - | Some g -> " && (" ^ gen_expr ctx g ^ ")" | None -> "" in - "if (" ^ cond ^ guard ^ ") { " ^ binds - ^ gen_tail_to ctx sink arm.ma_body ^ " } else " ^ arms_js rest + (* `$` cannot occur in an AffineScript identifier, so these names + cannot collide with, or shadow, a source binding. *) + let sv = Printf.sprintf "$scrut_%d" !let_tmp_counter in + let label = Printf.sprintf "$match_%d" !let_tmp_counter in + (* A labelled block: each arm binds its pattern variables before its + guard reads them, and an arm whose guard fails falls through to + the next arm. *) + let arm_js arm = + let cond = gen_pattern_test sv arm.ma_pat in + let binds = gen_pattern_bindings sv arm.ma_pat in + let body = gen_tail_to ctx sink arm.ma_body ^ " break " ^ label ^ ";" in + let guarded = match arm.ma_guard with + | None -> body + | Some g -> "if (" ^ gen_expr ctx g ^ ") { " ^ body ^ " }" + in + "if (" ^ cond ^ ") { " ^ binds ^ guarded ^ " }" in - "{ const " ^ sv ^ " = " ^ gen_expr ctx em_scrutinee ^ "; " - ^ arms_js em_arms ^ " }" + label ^ ": { const " ^ sv ^ " = " ^ gen_expr ctx em_scrutinee ^ "; " + ^ String.concat " " (List.map arm_js em_arms) + ^ " throw new Error(\"non-exhaustive match\"); }" | other -> sink (gen_expr ctx other) +(* Fail loudly where a `return` would otherwise be compiled into an IIFE and + leave it instead of the enclosing function. *) +and refuse_return what = + failwith + ("a `return` inside " ^ what ^ " is not supported by the Bun-ESM \ + backend; bind the value to a `let` first") + and gen_assign_tail ctx target e = gen_tail_to ctx (fun v -> target ^ " = " ^ v ^ ";") e @@ -1966,7 +1991,12 @@ and gen_stmt ctx (stmt : stmt) : string = occurrence in the same scope ("Identifier '_' has already been declared"). Drop the binding entirely; the bare expression statement keeps the semantics. *) - gen_expr ctx sl_value ^ ";" + if lowers_to_statements sl_value && expr_contains_return sl_value then + (* As below for named bindings: lower to statements so an early + `return` leaves the function; the value itself is discarded. *) + gen_tail_to ctx (fun v -> "void (" ^ v ^ ");") sl_value + else + gen_expr ctx sl_value ^ ";" | StmtLet { sl_pat; sl_value; sl_mut; sl_quantity = _; sl_ty = _ } -> let kw = if sl_mut then "let" else "const" in let js = @@ -1978,7 +2008,7 @@ and gen_stmt ctx (stmt : stmt) : string = initializer to statements that assign a temporary, so the `return` is a real function return. *) incr let_tmp_counter; - let tmp = Printf.sprintf "__let_tmp_%d" !let_tmp_counter in + let tmp = Printf.sprintf "$let_tmp_%d" !let_tmp_counter in "let " ^ tmp ^ "; " ^ gen_assign_tail ctx tmp sl_value ^ " " ^ kw ^ " " ^ gen_pattern ctx sl_pat ^ " = " ^ tmp ^ ";" end else diff --git a/lib/interp.ml b/lib/interp.ml index 2b0edc96..459628d0 100644 --- a/lib/interp.ml +++ b/lib/interp.ml @@ -555,15 +555,12 @@ and eval_binop (op : binary_op) (left : value) (right : value) : value result = | (VFloat a, VFloat b) -> binop_float op a b | (VString a, VString b) -> binop_string op a b | (VBool a, VBool b) -> binop_bool op a b - (* The checker accepts `++` on arrays, `==`/`!=` at every type and the - relational operators on Char; without these arms such well-typed - programs stopped with "Type mismatch in binary operation" at run - time (kyq's line splitter, `out ++ [cur]` and `ch == '\n'`). *) + (* The checker accepts `++` on arrays and `==`/`!=` at every type; without + these arms such well-typed programs stopped with "Type mismatch in + binary operation" at run time (kyq's line splitter, `out ++ [cur]` and + `ch == '\n'`). Relational operators on Char are not added: the checker + rejects them (`'a' < 'b'` is a type error), so they cannot reach here. *) | (VArray a, VArray b) when op = OpConcat -> Ok (VArray (Array.append a b)) - | (VChar a, VChar b) when op = OpLt -> Ok (VBool (a < b)) - | (VChar a, VChar b) when op = OpLe -> Ok (VBool (a <= b)) - | (VChar a, VChar b) when op = OpGt -> Ok (VBool (a > b)) - | (VChar a, VChar b) when op = OpGe -> Ok (VBool (a >= b)) | _ when op = OpEq -> Ok (VBool (Value.value_eq left right)) | _ when op = OpNe -> Ok (VBool (not (Value.value_eq left right))) | _ -> Error (TypeMismatch "Type mismatch in binary operation") diff --git a/lib/module_loader.ml b/lib/module_loader.ml index 6a46364b..6a6ce83c 100644 --- a/lib/module_loader.ml +++ b/lib/module_loader.ml @@ -131,6 +131,23 @@ let default_config () : config = current_dir = Sys.getcwd (); } +(** [dir] with `.` segments and empty segments removed and `..` resolved + lexically, so `/p/src/./Kyaml` and `/p/src/Kyaml` match the same + module path. *) +let normalise_dir (dir : string) : string = + let absolute = String.length dir > 0 && dir.[0] = '/' in + let segs = + List.fold_left (fun acc seg -> + match seg with + | "" | "." -> acc + | ".." -> (match acc with _ :: rest -> rest | [] -> acc) + | s -> s :: acc) + [] (String.split_on_char '/' dir) + |> List.rev + in + let body = String.concat "/" segs in + if absolute then "/" ^ body else if body = "" then "." else body + (** The directory module paths are resolved from, for a root file in [file_dir] that declares [mod_path]. @@ -143,6 +160,7 @@ let default_config () : config = imports working (#642). *) let module_root_dir ~(file_dir : string) (mod_path : string list option) : string = + let file_dir = normalise_dir file_dir in match mod_path with | None | Some [] | Some [_] -> file_dir | Some segs -> @@ -157,6 +175,36 @@ let module_root_dir ~(file_dir : string) (mod_path : string list option) | Some root -> root | None -> file_dir +(** Loader configuration for a root file at [file_path] that declares + [mod_path]. The package root (see {!module_root_dir}) resolves full + paths (`use A.C;` from `A/B.affine`); the file's own directory stays + searchable, so a short sibling import (`use Helper;` for + `A/Helper.affine`) keeps working (#642). *) +let config_for_file ~(file_path : string) (mod_path : string list option) + : config = + let absolute_path = + if Filename.is_relative file_path + then Filename.concat (Sys.getcwd ()) file_path + else file_path + in + let file_dir = normalise_dir (Filename.dirname absolute_path) in + let root = module_root_dir ~file_dir mod_path in + let config = default_config () in + { config with + current_dir = root; + search_paths = + (if root = file_dir then config.search_paths + else file_dir :: config.search_paths) } + +(** Whether [prog] itself declares `main`. `eval` auto-calls `main` only + then: flattening inlines imported declarations, so a dependency's + `pub fn main` must not run because the root has none. *) +let declares_main (prog : program) : bool = + List.exists (function + | TopFn fd -> fd.fd_name.name = "main" + | _ -> false) + prog.prog_decls + (** Convert module path to file path candidates For module path ["Math", "Geometry"], try: @@ -178,11 +226,11 @@ let path_to_candidates (mod_path : string list) : string list = (** Search for a module file in search paths *) let find_module_file (loader : t) (mod_path : string list) : string option = let candidates = path_to_candidates mod_path in - let search_in_dir dir = + let search_in_dir cands dir = List.find_map (fun candidate -> let full_path = Filename.concat dir candidate in if Sys.file_exists full_path then Some full_path else None - ) candidates + ) cands in (* Search order: 1) current dir, 2) stdlib, 3) additional search paths *) let all_paths = @@ -190,7 +238,18 @@ let find_module_file (loader : t) (mod_path : string list) : string option = loader.config.stdlib_path :: loader.config.search_paths in - List.find_map search_in_dir all_paths + (* Every directory is searched for the exact file (`A/B.affine`) before + any falls back to the parent file (`A.affine`): a parent file that + declares a different module is rejected when loaded + (ModuleDeclMismatch), so it must not shadow a real `A/B.affine` in a + later search directory. *) + let exact, parent = match candidates with + | [ nested; up ] -> ([ nested ], [ up ]) + | cands -> (cands, []) + in + match List.find_map (search_in_dir exact) all_paths with + | Some _ as found -> found + | None -> List.find_map (search_in_dir parent) all_paths (** Load and parse a module file *) let parse_module_file (file_path : string) : (program, load_error) result = @@ -224,11 +283,11 @@ let rec load_module (loader : t) (mod_path : string list) : (loaded_module, load (* Find the module file *) match find_module_file loader mod_path with - | None -> Error (ModuleNotFound mod_path) + | None -> Hashtbl.remove loader.loading mod_path; Error (ModuleNotFound mod_path) | Some file_path -> (* Parse the module *) match parse_module_file file_path with - | Error e -> Error e + | Error e -> Hashtbl.remove loader.loading mod_path; Error e | Ok prog -> let declared = Option.map (List.map (fun (id : ident) -> id.name)) prog.prog_module @@ -238,8 +297,14 @@ let rec load_module (loader : t) (mod_path : string list) : (loaded_module, load Hashtbl.remove loader.loading mod_path; Error (ModuleDeclMismatch (mod_path, d, file_path)) | _ -> - (* Load dependencies first (imports) *) - let* () = load_dependencies loader prog.prog_imports in + (* Load dependencies first (imports). On failure, clear this + module's loading marker, or a later load of it would be + reported as a cycle. *) + let* () = + match load_dependencies loader prog.prog_imports with + | Ok () -> Ok () + | Error e -> Hashtbl.remove loader.loading mod_path; Error e + in let loaded_mod = { mod_path; @@ -296,6 +361,63 @@ 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 = + "__priv_" ^ String.concat "_" mod_path ^ "__" ^ name + +(** 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_ident (id : ident) = + match List.assoc_opt id.name ren with + | Some n -> { id with name = n } + | None -> id + in + let rename_decl = function + | 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 } + | other -> other + 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. @@ -351,12 +473,46 @@ let rec flatten_imports_from 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 add_imported name decl_kind = + 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.) *) + failwith + (Printf.sprintf + "cannot flatten imports: `%s` is defined in both %s and %s, \ + and at least one is a private helper" + name (show_module_path other) (show_module_path 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_by_name name decl_kind; + Hashtbl.replace imported_origin name origin end in List.iter (fun imp -> @@ -373,8 +529,10 @@ let rec flatten_imports_from 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 + 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. *) @@ -383,6 +541,7 @@ let rec flatten_imports_from | 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 @@ -547,9 +706,16 @@ let rec flatten_imports_from |> Option.map (fun (_, found) -> (target, found)) ) items in - List.map - (fun (n, dk) -> (bound_name_of n, rename_decl (bound_name_of n) dk)) - (close_over direct) + let closed = close_over direct in + (* An aliased item (`use M::{helper as h, api}`) is carried under + its alias for the importer, and also under its own name when + it is renamed, because the module's other declarations (`api`) + still call it by that name. *) + List.concat_map (fun (n, dk) -> + let b = bound_name_of n in + if b = n then [ (n, dk) ] + else [ (n, dk); (b, rename_decl b dk) ]) + closed in (* Public enums the importer names directly. A constructor in the import list (`use Geometry::{Up, Down}`) or a type name the @@ -577,8 +743,8 @@ let rec flatten_imports_from public_enums | ImportSimple _ | ImportGlob _ -> public_enums in - List.iter (fun (ty, _, decl) -> add_imported ty (`Type decl)) named; - List.iter (fun (name, decl_kind) -> add_imported name decl_kind) select + 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 diff --git a/test/test_module_root_flatten.ml b/test/test_module_root_flatten.ml index 61bcf5ba..598b8dc0 100644 --- a/test/test_module_root_flatten.ml +++ b/test/test_module_root_flatten.ml @@ -144,7 +144,6 @@ let binop_cases () = (Interp.eval_binop Ast.OpConcat (VArray [| VInt 1 |]) (VArray [| VInt 2 |])); ok "char == char" (VBool true) (Interp.eval_binop Ast.OpEq (VChar 'a') (VChar 'a')); ok "char != char" (VBool true) (Interp.eval_binop Ast.OpNe (VChar 'a') (VChar 'b')); - ok "char < char" (VBool true) (Interp.eval_binop Ast.OpLt (VChar 'a') (VChar 'b')); ok "tuple == tuple" (VBool false) (Interp.eval_binop Ast.OpEq (VTuple [ VInt 1; VInt 2 ]) (VTuple [ VInt 1; VInt 3 ])); ok "variant == variant" (VBool true) @@ -240,6 +239,204 @@ let return_in_operand_refused () = Alcotest.(check bool) ("refusal names the cause: " ^ msg) true (Str.string_match (Str.regexp ".*return. inside a match arm") msg 0)) +(* ---- Review round 1 (affinescript#788) ---------------------------------- *) + +let normalise_cases () = + Alcotest.(check string) "a `.` segment is dropped" "/p/src/Kyaml" + (Module_loader.normalise_dir "/p/src/./Kyaml"); + Alcotest.(check string) "a `..` segment is resolved" "/p/src" + (Module_loader.normalise_dir "/p/src/Kyaml/.."); + Alcotest.(check string) "the root is found through a `.` segment" "/p/src" + (Module_loader.module_root_dir ~file_dir:"/p/src/./Kyaml" + (Some [ "Kyaml"; "Ijson" ])) + +(* Rooting a nested module at its package root must not stop a short + sibling import from resolving (#642). *) +let sibling_import_from_nested () = + let dir = fresh_dir "sibling" in + write_file dir "A/Helper.affine" "module Helper;\npub fn h() -> Int { 3 }\n"; + write_file dir "A/Main.affine" + "module A.Main;\nuse Helper::{h};\npub fn main() -> Int { h() }\n"; + let config = + Module_loader.config_for_file + ~file_path:(Filename.concat dir "A/Main.affine") (Some [ "A"; "Main" ]) + in + Alcotest.(check string) "rooted at the package root" + (Module_loader.normalise_dir dir) config.current_dir; + match Module_loader.load_module (Module_loader.create config) [ "Helper" ] with + | Ok _ -> () + | Error e -> + Alcotest.failf "sibling Helper must load: %s" (Module_loader.show_load_error e) + +(* A parent file near the source must not shadow the exact file later on + the search path. *) +let exact_file_beats_parent () = + let near = fresh_dir "near" and far = fresh_dir "far" in + write_file near "P.affine" "module P;\npub fn p() -> Int { 1 }\n"; + write_file far "P/M.affine" "module P.M;\npub fn m() -> Int { 2 }\n"; + let loader = + Module_loader.create + { (Module_loader.default_config ()) with + current_dir = near; search_paths = [ far ] } + in + match Module_loader.load_module loader [ "P"; "M" ] with + | Ok lm -> + Alcotest.(check string) "loaded far/P/M.affine" + (Filename.concat far "P/M.affine") lm.mod_file + | Error e -> + Alcotest.failf "P.M must load from the search path: %s" + (Module_loader.show_load_error e) + +let failed_dependency_is_not_a_cycle () = + let dir = fresh_dir "marker" in + write_file dir "Q.affine" "module Q;\nuse NoSuch::*;\npub fn q() -> Int { 1 }\n"; + let loader = loader_in dir in + ignore (Module_loader.load_module loader [ "Q" ]); + match Module_loader.load_module loader [ "Q" ] with + | Error (Module_loader.ModuleCycle _) -> + Alcotest.fail "a failed load left Q marked as loading (reported as a cycle)" + | Error _ -> () + | Ok _ -> Alcotest.fail "Q cannot load without NoSuch" + +let eval_int dir src expected = + match eval_main dir src with + | Value.VInt n -> Alcotest.(check int) "result" expected n + | v -> Alcotest.failf "expected %d, got %s" expected (Value.show_value v) + +(* `api` calls `helper` by its own name even when the importer renames it. *) +let alias_keeps_module_binding () = + let dir = fresh_dir "alias" in + write_file dir "P/N.affine" + "module P.N;\npub fn helper() -> Int { 4 }\n\ + pub fn api() -> Int { helper() + 1 }\n"; + eval_int dir + "module D;\nuse P.N::{helper as h, api};\n\ + pub fn main() -> Int { api() * 10 + h() }\n" + 54 + +let two_modules_same_private_helper () = + let dir = fresh_dir "collide" in + write_file dir "Q/A.affine" + "module Q.A;\nfn helper() -> Int { 1 }\npub fn a() -> Int { helper() }\n"; + write_file dir "Q/B.affine" + "module Q.B;\nfn helper() -> Int { 2 }\npub fn b() -> Int { helper() }\n"; + (* Each module calls its own helper. *) + eval_int dir + "module D;\nuse Q.A::*;\nuse Q.B::*;\n\ + pub fn main() -> Int { a() * 10 + b() }\n" + 12; + (* The importer's own `helper` neither replaces nor is replaced by Q.A's. *) + eval_int dir + "module D;\nuse Q.A::{a};\nfn helper() -> Int { 99 }\n\ + pub fn main() -> Int { a() + helper() }\n" + 100 + +(* Qualifying a module's private names leaves a local of the same name + alone. *) +let qualification_respects_shadowing () = + let dir = fresh_dir "shadow" in + write_file dir "Q/C.affine" + "module Q.C;\nfn helper() -> Int { 1 }\n\ + pub fn c(k: Int) -> Int { let helper = 50; helper + k }\n"; + eval_int dir "module D;\nuse Q.C::{c};\npub fn main() -> Int { c(1) }\n" 51 + +let diamond_import () = + let dir = fresh_dir "diamond" in + write_file dir "P/M.affine" helper_module; + write_file dir "J.affine" + "module J;\nuse P.M::{api};\npub fn j() -> Int { api(\"x\") + 1 }\n"; + eval_int dir + "module D;\nuse P.M::{api};\nuse J::{j};\n\ + pub fn main() -> Int { api(\"a\") + j() }\n" + 15 + +let main_only_from_root () = + let parse src = Parse_driver.parse_string ~file:"
" src in + Alcotest.(check bool) "a root with main" true + (Module_loader.declares_main (parse "module R;\npub fn main() -> Int { 0 }\n")); + Alcotest.(check bool) "a root without main" false + (Module_loader.declares_main (parse "module R;\npub fn other() -> Int { 0 }\n")) + +let id name = { Ast.name; span = Span.dummy } + +let free_vars_see_records () = + let e = + Ast.ExprRecord + { er_fields = [ (id "k", None); (id "f", Some (Ast.ExprVar (id "v"))) ]; + er_spread = Some (Ast.ExprVar (id "base")) } + in + let free = Ast.find_free_vars [] e in + List.iter (fun n -> + Alcotest.(check bool) (n ^ " is free") true (List.mem n free)) + [ "k"; "v"; "base" ]; + Alcotest.(check bool) "a bound shorthand is not free" false + (List.mem "k" (Ast.find_free_vars [ "k" ] e)) + +let contains_return_sees_aggregates () = + let ret = Ast.ExprReturn None in + List.iter (fun (what, e) -> + Alcotest.(check bool) what true (Ast.expr_contains_return e)) + [ ("tuple", Ast.ExprTuple [ Ast.ExprVar (id "x"); ret ]); + ("array", Ast.ExprArray [ ret ]); + ("record", Ast.ExprRecord { er_fields = [ (id "f", Some ret) ]; er_spread = None }); + ("index", Ast.ExprIndex (Ast.ExprArray [ ret ], Ast.ExprVar (id "i"))) ] + +let compile_deno src = + let prog = Parse_driver.parse_string ~file:"" src in + let ld = Module_loader.create (Module_loader.default_config ()) in + match Resolve.resolve_program_with_loader prog ld with + | Error (e, _) -> Error ("resolve: " ^ Resolve.show_resolve_error e) + | Ok (rctx, _) -> + Codegen_deno.codegen_deno (Module_loader.flatten_imports ld prog) rctx.symbols + +let guards_wildcards_and_temporaries () = + if not (node_available ()) then + Alcotest.fail "node is not on PATH (this test runs the emitted module)"; + let src = {| +module guards; +pub type R = Good(Int) | Bad(Int) +pub fn f(r: R) -> Int { + let v = match r { Good(n) if n > 10 => n, Good(n) => n + 100, Bad(e) => return e }; + v +} +pub fn g(r: R) -> Int { + let _ = match r { Good(n) => n, Bad(e) => return e + 1000 }; + 7 +} +pub fn h() -> Int { + let __let_tmp_1 = 5; + let w = match Good(1) { Good(n) => n, Bad(e) => return e }; + __let_tmp_1 + w +} +|} in + match compile_deno src with + | Error e -> Alcotest.failf "deno codegen: %s" e + | Ok js -> + (* f: the guard reads its own binding (Good(5) fails the guard and takes + the next arm); g: `let _` keeps the early return; h: a source name + that looks like a temporary does not collide with one. *) + Alcotest.(check string) "f f f g g h" "105 20 3 1001 7 6" + (run_node (js ^ "\nconsole.log(f(Good(5)), f(Good(20)), f(Bad(3)), \ + g(Bad(1)), g(Good(1)), h());\n")) + +let return_in_condition_refused () = + let prelude = + "module refuse;\npub type R = Good(Int) | Bad(Int)\n\ + fn id(x: Int) -> Int { x }\nfn yes(x: Int) -> Bool { true }\n" + in + List.iter (fun (what, body) -> + match compile_deno (prelude ^ body) with + | Ok _ -> Alcotest.failf "%s: a return there must be refused" what + | Error msg -> + Alcotest.(check bool) (what ^ ": " ^ msg) true + (Str.string_match (Str.regexp ".*return. inside") msg 0)) + [ ("if condition", + "pub fn f(r: R) -> Int { let v = if yes(return 1) { 2 } else { 3 }; v }\n"); + ("scrutinee", + "pub fn f(r: R) -> Int { let v = match id(return 1) { 0 => 2, _ => 3 }; v }\n"); + ("guard", + "pub fn f(r: R) -> Int { let v = match r { Good(n) if yes(return 9) => n, _ => 3 }; v }\n") ] + let tests = [ Alcotest.test_case "module_root_dir" `Quick root_cases; @@ -254,4 +451,28 @@ let tests = return_in_match_arm; Alcotest.test_case "Bun-ESM: return in an operand match is refused" `Quick return_in_operand_refused; + Alcotest.test_case "simple import carries private helper" `Quick + (flatten_carries_helpers "use P.M;"); + Alcotest.test_case "normalise_dir" `Quick normalise_cases; + Alcotest.test_case "sibling import from a nested module" `Quick + sibling_import_from_nested; + Alcotest.test_case "exact file beats parent file" `Quick exact_file_beats_parent; + Alcotest.test_case "failed dependency is not a cycle" `Quick + failed_dependency_is_not_a_cycle; + Alcotest.test_case "alias keeps the module binding" `Quick + alias_keeps_module_binding; + Alcotest.test_case "same-named private helpers stay distinct" `Quick + two_modules_same_private_helper; + Alcotest.test_case "qualification respects shadowing" `Quick + qualification_respects_shadowing; + Alcotest.test_case "diamond import" `Quick diamond_import; + Alcotest.test_case "main only from the root" `Quick main_only_from_root; + Alcotest.test_case "free vars see record shorthand and spread" `Quick + free_vars_see_records; + Alcotest.test_case "contains_return sees aggregates" `Quick + contains_return_sees_aggregates; + Alcotest.test_case "Bun-ESM: guards, let _, temporaries" `Quick + guards_wildcards_and_temporaries; + Alcotest.test_case "Bun-ESM: return in condition, scrutinee, guard refused" `Quick + return_in_condition_refused; ] From 974f08a845751d579eddfef8e800cead0b406daf Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 19:11:36 +0000 Subject: [PATCH 2/2] fix: guard int tracking, swallowed returns, import name hygiene (#797 review) Four findings from the CodeRabbit review of #797, each verified and pinned by a test that fails without its fix. Bun-ESM backend: - A match arm's guard is emitted before its body, and a tail-position block runs in its own scope of integer tracking (`with_int_scope`), so a `let n = 0.5` in an arm's body no longer turns the guard's `n / 2` into float division. - `gen_tail_to` refuses a `return` in a form it emits as an expression (`[{ return 7; 0 }]`), where the IIFE would have swallowed it. Module flattening: - A public declaration carried only for its module's own callers (the original of an aliased import, or a helper only a selected function calls) gets an internal name, with its references rewritten. With `use A::{helper as ah, api}` and `use B::{helper as bh}`, both `helper`s were emitted under `helper` and `api` called B's. - Internal names length-prefix each module path component, so `A_B.C` and `A.B_C` no longer give the same name. - Aliasing a mutable module binding (`const mut`) is refused: the alias would be a second, separate copy. - Both errors are a structured `Flatten_error`, reported by `eval` and `compile` as a diagnostic (E0206 with `--json`), instead of an uncaught `Failure`. Tests: five new cases in test/test_module_root_flatten.ml. Reverting each fix (the int scoping, the fallback refusal, the internal names, the injective encoding, the mutable-alias refusal) makes its test fail. Full suite: no failures beyond the 33 walker-* tests that need tree-sitter (same on main). kyq parser and lint drivers: 0 failures under Bun. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P4yFkxW5Heb8p2YW7ZYs9F --- bin/main.ml | 12 +++ docs/COMPLETION-PLAN.adoc | 2 +- lib/codegen_deno.ml | 50 +++++++--- lib/json_output.ml | 14 +++ lib/module_loader.ml | 166 +++++++++++++++++++++++-------- test/test_module_root_flatten.ml | 95 ++++++++++++++++++ 6 files changed, 285 insertions(+), 54 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 189e3ef1..15ee4fca 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -339,6 +339,8 @@ let eval_file face json path = add (Affinescript.Json_output.of_lexer_error msg pos path) | Affinescript.Parse_driver.Parse_error (msg, span) -> add (Affinescript.Json_output.of_parse_error msg span) + | Affinescript.Module_loader.Flatten_error e -> + add (Affinescript.Json_output.of_flatten_error e path) end; json_finish (List.rev !diags) end else begin @@ -400,6 +402,10 @@ let eval_file face json path = Format.eprintf "@[%a: parse error: %s@]@." Affinescript.Span.pp_short span msg; `Error (false, "Parse error") + | Affinescript.Module_loader.Flatten_error e -> + Format.eprintf "@[%s: %s@]@." path + (Affinescript.Module_loader.flatten_error_message e); + `Error (false, "Import error") end (** Generate the AffineScript TEA bridge Wasm module. @@ -747,6 +753,8 @@ let compile_file face json wasm_gc vscode_ext vscode_adapter vscode_no_lc add (Affinescript.Json_output.of_lexer_error msg pos path) | Affinescript.Parse_driver.Parse_error (msg, span) -> add (Affinescript.Json_output.of_parse_error msg span) + | Affinescript.Module_loader.Flatten_error e -> + add (Affinescript.Json_output.of_flatten_error e path) end; json_finish (List.rev !diags) end else begin @@ -1000,6 +1008,10 @@ let compile_file face json wasm_gc vscode_ext vscode_adapter vscode_no_lc Format.eprintf "@[%a: parse error: %s@]@." Affinescript.Span.pp_short span msg; `Error (false, "Parse error") + | Affinescript.Module_loader.Flatten_error e -> + Format.eprintf "@[%s: %s@]@." path + (Affinescript.Module_loader.flatten_error_message e); + `Error (false, "Import error") end (** Format a file. Only canonical face is supported — non-canonical face diff --git a/docs/COMPLETION-PLAN.adoc b/docs/COMPLETION-PLAN.adoc index 084e5869..cacbd129 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, and an aliased import emits the original before the alias. 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. 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/lib/codegen_deno.ml b/lib/codegen_deno.ml index eadf2e95..42613c7b 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -1897,14 +1897,17 @@ and gen_tail_to ctx (sink : string -> string) e = | ExprSpan (inner, _) -> gen_tail_to ctx sink inner | ExprReturn _ -> gen_stmt_expr ctx e | ExprBlock blk -> - let b = Buffer.create 64 in - List.iter (fun s -> - Buffer.add_string b (gen_stmt ctx s); - Buffer.add_char b ' ') blk.blk_stmts; - (match blk.blk_expr with - | Some tail -> Buffer.add_string b (gen_tail_to ctx sink tail) - | None -> Buffer.add_string b (sink "Unit")); - "{ " ^ Buffer.contents b ^ " }" + (* The block is a JS block scope: its `let`s must not change the + integer tracking of the code after it (a guard, a later arm). *) + with_int_scope ctx (fun () -> + let b = Buffer.create 64 in + List.iter (fun s -> + Buffer.add_string b (gen_stmt ctx s); + Buffer.add_char b ' ') blk.blk_stmts; + (match blk.blk_expr with + | Some tail -> Buffer.add_string b (gen_tail_to ctx sink tail) + | None -> Buffer.add_string b (sink "Unit")); + "{ " ^ Buffer.contents b ^ " }") | ExprIf { ei_cond; ei_then; ei_else } -> (* The condition is emitted as an expression, where a `return` would leave an IIFE instead of the function. *) @@ -1935,17 +1938,40 @@ and gen_tail_to ctx (sink : string -> string) e = let arm_js arm = let cond = gen_pattern_test sv arm.ma_pat in let binds = gen_pattern_bindings sv arm.ma_pat in - let body = gen_tail_to ctx sink arm.ma_body ^ " break " ^ label ^ ";" in - let guarded = match arm.ma_guard with + (* The guard is emitted before the body, against the integer + tracking the guard actually runs under. *) + let guard = Option.map (gen_expr ctx) arm.ma_guard in + let body = + with_int_scope ctx (fun () -> gen_tail_to ctx sink arm.ma_body) + ^ " break " ^ label ^ ";" + in + let guarded = match guard with | None -> body - | Some g -> "if (" ^ gen_expr ctx g ^ ") { " ^ body ^ " }" + | Some g -> "if (" ^ g ^ ") { " ^ body ^ " }" in "if (" ^ cond ^ ") { " ^ binds ^ guarded ^ " }" in label ^ ": { const " ^ sv ^ " = " ^ gen_expr ctx em_scrutinee ^ "; " ^ String.concat " " (List.map arm_js em_arms) ^ " throw new Error(\"non-exhaustive match\"); }" - | other -> sink (gen_expr ctx other) + | other -> + (* Any other form is emitted as an expression, where a nested block + becomes an IIFE and a `return` in it would leave only the IIFE + (`[{ return 7; 0 }]`). Refuse it rather than drop the return. *) + if expr_contains_return other then refuse_return "this expression"; + sink (gen_expr ctx other) + +(* Run [f] in a nested scope of [ctx]'s integer tracking: bindings it records + are dropped afterwards, so they cannot leak into the code that follows. *) +and with_int_scope : 'a. codegen_ctx -> (unit -> 'a) -> 'a = fun ctx f -> + let saved = Hashtbl.copy ctx.int_vars in + let restore () = + Hashtbl.reset ctx.int_vars; + Hashtbl.iter (Hashtbl.replace ctx.int_vars) saved + in + match f () with + | v -> restore (); v + | exception e -> restore (); raise e (* Fail loudly where a `return` would otherwise be compiled into an IIFE and leave it instead of the enclosing function. *) diff --git a/lib/json_output.ml b/lib/json_output.ml index 6fa770a2..83112de5 100644 --- a/lib/json_output.ml +++ b/lib/json_output.ml @@ -136,6 +136,20 @@ let of_parse_error (msg : string) (span : Span.t) : diagnostic = labels = []; } +(** Convert an import-flattening error to a diagnostic. It names no source + position, so it is placed at the start of the root file. *) +let of_flatten_error (e : Module_loader.flatten_error) (file : string) + : diagnostic = + let start = { Span.line = 1; col = 1; offset = 0 } in + { + severity = Error; + code = "E0206"; + message = Module_loader.flatten_error_message e; + span = Span.make ~file ~start_pos:start ~end_pos:start; + help = Some "import the binding under its own name, or rename one of them"; + labels = []; + } + (** Convert a resolve error to a diagnostic. [Resolve.resolve_error] variants carry [Ast.ident] values which diff --git a/lib/module_loader.ml b/lib/module_loader.ml index 6a6ce83c..13282b9a 100644 --- a/lib/module_loader.ml +++ b/lib/module_loader.ml @@ -21,6 +21,20 @@ type load_error = would bind [A]'s exports under a name that does not exist. *) [@@deriving show] +(** Why the declarations of a program's imports cannot be flattened into one + program. Raised as {!Flatten_error} by {!flatten_imports}; the CLI reports + it as a diagnostic. *) +type flatten_error = + | ImportCollision of string * string list * string list + (** One flattened name, carried by two different modules' declarations. *) + | MutableAlias of string * string * string list + (** A mutable module binding, the alias it was imported under, and its + module. An alias is a second declaration, so it would be a second, + separate copy of the binding. *) +[@@deriving show] + +exception Flatten_error of flatten_error + (** Helper for Result bind *) let ( let* ) = Result.bind @@ -369,7 +383,53 @@ let clear_cache (loader : t) : unit = 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 = - "__priv_" ^ String.concat "_" mod_path ^ "__" ^ name + (* 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_" + ^ String.concat "" + (List.map (fun c -> string_of_int (String.length c) ^ c) mod_path) + ^ "__" ^ name + +(** The diagnostic text of a {!flatten_error}. *) +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) + | 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 + +(** 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. *) +let rename_top_level (ren : (string * string) list) (decl : top_level) + : top_level = + if ren = [] then decl + else + let rename_ident (id : ident) = + match List.assoc_opt id.name ren with + | 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 } + | other -> other (** Give [prog]'s own private functions and constants (those of [own], the module's unflattened declarations) their {!qualified_private_name}, and @@ -390,28 +450,7 @@ let qualify_private (mod_path : string list) (own : top_level list) in if ren = [] then prog else - let rename_ident (id : ident) = - match List.assoc_opt id.name ren with - | Some n -> { id with name = n } - | None -> id - in - let rename_decl = function - | 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 } - | other -> other - in + let rename_decl = rename_top_level ren in let is_own d = List.exists (fun o -> o == d) own in { prog with prog_decls = @@ -503,11 +542,7 @@ let rec flatten_imports_from make the other module call the wrong function. (Public collisions are rejected by the resolver; #743's last-wins rule still applies to them.) *) - failwith - (Printf.sprintf - "cannot flatten imports: `%s` is defined in both %s and %s, \ - and at least one is a private helper" - name (show_module_path other) (show_module_path origin)) + raise (Flatten_error (ImportCollision (name, other, origin))) | _ -> ()); if not (Hashtbl.mem imported_by_name name) then imported_order := name :: !imported_order; @@ -674,13 +709,29 @@ let rec flatten_imports_from the bodies present. *) close_over public_decls | ImportList (_, items) -> - (* The name a directly-named item is imported under (alias - honoured). *) - let bound_name_of target = - match List.find_opt (fun item -> item.ii_name.name = target) items with - | Some { ii_alias = Some a; _ } -> a.name - | _ -> target + (* 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 -> @@ -707,14 +758,47 @@ let rec flatten_imports_from ) items in let closed = close_over direct in - (* An aliased item (`use M::{helper as h, api}`) is carried under - its alias for the importer, and also under its own name when - it is renamed, because the module's other declarations (`api`) - still call it by that name. *) + (* 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 b = bound_name_of n in - if b = n then [ (n, dk) ] - else [ (n, dk); (b, rename_decl b 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 in (* Public enums the importer names directly. A constructor in the diff --git a/test/test_module_root_flatten.ml b/test/test_module_root_flatten.ml index 598b8dc0..7c5717b5 100644 --- a/test/test_module_root_flatten.ml +++ b/test/test_module_root_flatten.ml @@ -437,8 +437,103 @@ let return_in_condition_refused () = ("guard", "pub fn f(r: R) -> Int { let v = match r { Good(n) if yes(return 9) => n, _ => 3 }; v }\n") ] +(* A guard is compiled against the integer tracking it runs under, not the + one the arm's body leaves behind: the body's `let n = 0.5` must not turn + the guard's `n / 2` into float division (CodeRabbit on #797). *) +let guard_ignores_body_int_tracking () = + if not (node_available ()) then + Alcotest.fail "node is not on PATH (this test runs the emitted module)"; + let src = {| +module guardint; +pub fn k(n: Int) -> Int { + let _ = match n { m if n / 2 == 2 => { let n = 0.5; 0 }, _ => return 9 }; + 1 +} +|} in + match compile_deno src with + | Error e -> Alcotest.failf "deno codegen: %s" e + | Ok js -> + (* 5 / 2 truncates to 2: the guard holds, so k(5) falls through to 1. *) + Alcotest.(check string) "k(5) k(9)" "1 9" + (run_node (js ^ "\nconsole.log(k(5), k(9));\n")) + +(* A `return` the statement lowering cannot keep (inside an array, so inside + an IIFE) is refused rather than dropped. *) +let return_in_array_arm_refused () = + let src = + "module arr;\n\ + pub fn f(b: Bool) -> Int {\n\ + \ let _ = match b { true => [{ return 7; 0 }], false => return 9 };\n\ + \ 11\n}\n" + in + match compile_deno src with + | Ok _ -> Alcotest.fail "a return inside an array arm must be refused" + | Error msg -> + Alcotest.(check bool) msg true + (Str.string_match (Str.regexp ".*return. inside") msg 0) + +(* An original a module carries for its own callers, but which the importer + binds only under an alias, does not occupy the importer's namespace: two + modules' `helper`s stay apart (CodeRabbit on #797). *) +let aliased_originals_stay_apart () = + let dir = fresh_dir "aliasapart" in + write_file dir "R/A.affine" + "module R.A;\npub fn helper() -> Int { 1 }\npub fn api() -> Int { helper() }\n"; + write_file dir "R/B.affine" "module R.B;\npub fn helper() -> Int { 2 }\n"; + eval_int dir + "module D;\nuse R.A::{helper as ah, api};\nuse R.B::{helper as bh};\n\ + pub fn main() -> Int { api() * 100 + ah() * 10 + bh() }\n" + 112 + +(* Module paths that join to the same text (`S_T.U`, `S.T_U`) still give + 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"); + 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"; + write_file dir "S/T_U.affine" + "module S.T_U;\nfn h() -> Int { 2 }\npub fn u2() -> Int { h() }\n"; + eval_int dir + "module D;\nuse S_T.U::{u1};\nuse S.T_U::{u2};\n\ + pub fn main() -> Int { u1() * 10 + u2() }\n" + 12 + +(* Aliasing a mutable module binding would make a second, separate copy: it + is a structured flattening error, which the CLI reports as a diagnostic. *) +let mutable_alias_refused () = + let dir = fresh_dir "mutalias" in + write_file dir "V/M.affine" + "module V.M;\npub const mut counter: Int = 0;\n\ + pub fn get() -> Int { counter }\n"; + let src = + "module D;\nuse V.M::{counter as c, get};\npub fn main() -> Int { get() }\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 "aliasing a mutable binding must be refused" + | exception Module_loader.Flatten_error + (Module_loader.MutableAlias ("counter", "c", [ "V"; "M" ])) -> + ()) + let tests = [ + 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 + return_in_array_arm_refused; + Alcotest.test_case "aliased originals stay apart" `Quick + aliased_originals_stay_apart; + Alcotest.test_case "internal names are injective" `Quick + internal_names_are_injective; + Alcotest.test_case "aliasing a mutable binding is refused" `Quick + mutable_alias_refused; Alcotest.test_case "module_root_dir" `Quick root_cases; Alcotest.test_case "declared path must match" `Quick decl_mismatch_rejected; Alcotest.test_case "missing dependency is named" `Quick missing_dependency_named;