From 39f8218fe1a3241fd9fab84f0a9469ca9c3d5cc7 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" Date: Mon, 30 Nov 2026 23:09:09 +0000 Subject: [PATCH 1/3] fix(typecheck): end a block's let bindings with the block synth_block left a block-local binding's type in name_types, so it replaced the type of an outer binding of the same name for the rest of the function. The resolver already scoped blocks; the type checker did not. So let x = "s"; if c { let x = 1; } x + 1 type-checked as an Int, and a function declared `-> Int` returned the string "s1" at run time on Bun-ESM and Wasm. The converse (an outer Int shadowed by a block-local String) was wrongly rejected. synth_block now saves the types of the names its lets bind and restores them when the block ends, on every path. test/test_block_scope.ml plants the unsound program for if, bare and while blocks, and the converse; five of its six cases fail without this change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- lib/typecheck.ml | 20 +++++++ test/test_block_scope.ml | 118 +++++++++++++++++++++++++++++++++++++++ test/test_main.ml | 1 + 3 files changed, 139 insertions(+) create mode 100644 test/test_block_scope.ml diff --git a/lib/typecheck.ml b/lib/typecheck.ml index 96af4cfa..4c32140d 100644 --- a/lib/typecheck.ml +++ b/lib/typecheck.ml @@ -1594,7 +1594,27 @@ and block_always_diverges (blk : block) : bool = | _ -> false end +(* A block is a scope: the names its `let`s bind end with it. Without the + restore, a block-local `let x = 1` replaced the type of an outer `x` for + the rest of the function, so `let x = "s"; if c { let x = 1; } x + 1` was + accepted as an [Int] and returned "s1" at run time. *) and synth_block (ctx : context) (blk : block) : ty result = + let names = + List.sort_uniq compare + (List.concat_map (function + | StmtLet { sl_pat; _ } -> pattern_binders sl_pat + | _ -> []) blk.blk_stmts) + in + let saved = List.map (fun n -> (n, Hashtbl.find_opt ctx.name_types n)) names in + let restore () = + List.iter (fun (n, sc) -> + match sc with + | Some sc -> Hashtbl.replace ctx.name_types n sc + | None -> Hashtbl.remove ctx.name_types n) saved + in + Fun.protect ~finally:restore (fun () -> synth_block_body ctx blk) + +and synth_block_body (ctx : context) (blk : block) : ty result = (* Type-check each statement for side effects *) let* () = List.fold_left (fun acc stmt -> let* () = acc in diff --git a/test/test_block_scope.ml b/test/test_block_scope.ml new file mode 100644 index 00000000..a37613c0 --- /dev/null +++ b/test/test_block_scope.ml @@ -0,0 +1,118 @@ +(* SPDX-License-Identifier: MPL-2.0 *) +(* Copyright (c) 2026 Jonathan D.A. Jewell *) +(** A block's [let] bindings end with the block. + + The resolver already scoped them, but the type checker did not: + [synth_block] left a block-local binding's type in [name_types], so it + replaced the type of an outer binding of the same name for the rest of + the function. [let x = "s"; if c { let x = 1; } x + 1] type-checked as + an [Int] and returned the string "s1" at run time, on both Bun-ESM and + Wasm. These tests plant that program and its relatives. *) + +open Affinescript + +(** parse -> resolve -> typecheck, as in [Test_generic_enum_kinds]. *) +let frontend (src : string) : (unit, string) result = + let ( let* ) = Result.bind in + let* prog = + try Ok (Parse_driver.parse_string ~file:"" src) + with + | Parse_driver.Parse_error (m, sp) -> + Error (Printf.sprintf "Parse error at %s: %s" (Span.show sp) m) + | e -> Error (Printf.sprintf "Unexpected: %s" (Printexc.to_string e)) + in + let dir = Sys.getcwd () in + let config = + { (Module_loader.default_config ()) with current_dir = dir; search_paths = [ dir ] } + in + let loader = Module_loader.create config in + let* resolve_ctx, type_ctx = + match Resolve.resolve_program_with_loader prog loader with + | Ok (rc, tc) -> Ok (rc, tc) + | Error (e, _) -> Error ("Resolution error: " ^ Resolve.show_resolve_error e) + in + match + Typecheck.check_program ~import_types:type_ctx.Typecheck.name_types + resolve_ctx.symbols prog + with + | Ok _ -> Ok () + | Error e -> Error ("Type error: " ^ Typecheck.format_type_error e) + +let passes src = + match frontend src with + | Ok () -> () + | Error m -> Alcotest.failf "expected Ok, got: %s" m + +let rejected src = + match frontend src with + | Ok () -> Alcotest.fail "expected a type error, got Ok" + | Error m -> + if not (String.length m >= 11 && String.sub m 0 11 = "Type error:") then + Alcotest.failf "expected a type error, got: %s" m + +(* The unsound program: the outer [x] is a String. *) +let if_block_shadow_does_not_leak () = + rejected + "pub fn f(c: Bool) -> Int {\n\ + \ let x = \"s\";\n\ + \ if c { let x = 1; }\n\ + \ x + 1\n\ + }\n" + +let bare_block_shadow_does_not_leak () = + rejected + "pub fn f() -> Int {\n\ + \ let x = \"s\";\n\ + \ { let x = 1; };\n\ + \ x + 1\n\ + }\n" + +let while_body_shadow_does_not_leak () = + rejected + "pub fn f(c: Bool) -> Int {\n\ + \ let x = \"s\";\n\ + \ while c { let x = 1; }\n\ + \ x + 1\n\ + }\n" + +(* The converse, which used to be rejected: after the block the outer + binding's type is back. *) +let outer_type_restored () = + passes + "pub fn f(c: Bool) -> Int {\n\ + \ let x = 1;\n\ + \ if c { let x = \"s\"; }\n\ + \ x + 1\n\ + }\n" + +(* A builtin shadowed inside a block is the builtin again after it. *) +let builtin_visible_after_block () = + passes + "pub fn f(c: Bool) -> Int {\n\ + \ let mut n = 0;\n\ + \ if c { let len = 10; n = len; }\n\ + \ n + len(\"ab\")\n\ + }\n" + +(* Inside the block, and in its tail, the inner binding still applies. *) +let inner_binding_in_scope () = + passes + "pub fn f() -> String {\n\ + \ let x = 1;\n\ + \ let y = { let x = \"s\"; x };\n\ + \ y\n\ + }\n" + +let tests = + [ + Alcotest.test_case "if-block shadow does not leak" `Quick + if_block_shadow_does_not_leak; + Alcotest.test_case "bare-block shadow does not leak" `Quick + bare_block_shadow_does_not_leak; + Alcotest.test_case "while-body shadow does not leak" `Quick + while_body_shadow_does_not_leak; + Alcotest.test_case "outer type restored after block" `Quick outer_type_restored; + Alcotest.test_case "builtin visible after block" `Quick builtin_visible_after_block; + Alcotest.test_case "inner binding in scope in the block" `Quick + inner_binding_in_scope; + ] diff --git a/test/test_main.ml b/test/test_main.ml index b87c8e62..e59d9f6d 100644 --- a/test/test_main.ml +++ b/test/test_main.ml @@ -28,6 +28,7 @@ let () = ("TW L13 isolation (#10)", Test_tw_isolation.tests); ("Qualified paths (#228, ADR-014)", Test_qualified_paths.tests); ("Generic enum kinds", Test_generic_enum_kinds.tests); + ("Block-scoped let in the type checker", Test_block_scope.tests); ("Module search path ($AFFINESCRIPT_PATH)", Test_module_search_path.tests); ("Module root, decl match, flattening, interp binops, Bun-ESM return-in-arm", Test_module_root_flatten.tests); ("Record update + imported types", Test_records_and_imports.tests); From 5ff584ed2da0b69431e2048a3325aafd4f4c955f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 23:09:43 +0000 Subject: [PATCH 2/3] docs(soundness): ledger row for the block-scope hole (#826) Adds the #826 row (fixed, anchored by test/test_block_scope.ml), reseals tools/soundness-anchors.sha256 and moves the ground-truth stamp to ee211d3, the main commit this fix was verified against. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- docs/SOUNDNESS.adoc | 13 +++++++++++-- tools/soundness-anchors.sha256 | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/SOUNDNESS.adoc b/docs/SOUNDNESS.adoc index 2e80af4e..f3d89646 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: 70379fe -:ground-truth-date: 2026-11-19 +:ground-truth-sha: ee211d3 +:ground-truth-date: 2026-11-30 :stamp-verified-by: tools/check-soundness-ledger.sh :toc: macro :toclevels: 2 @@ -238,6 +238,15 @@ 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`). + +| #826 +| A block's `let` bindings end with the block in the type checker, as they +already did in the resolver. `let x = "s"; if c { let x = 1; } x + 1` used +to type-check as an `Int` and return the string `"s1"` at run time; it is now +a type error, and an outer binding's type is back after any block. +| `fixed` +| `test/test_block_scope.ml` (if, bare and while blocks; the converse; a +builtin shadowed in a block; the inner binding in scope). |=== == Still open — read before claiming "sound" diff --git a/tools/soundness-anchors.sha256 b/tools/soundness-anchors.sha256 index 441c434a..e0ad5392 100644 --- a/tools/soundness-anchors.sha256 +++ b/tools/soundness-anchors.sha256 @@ -13,6 +13,7 @@ 80541ee00580b4dccee4657cbd31d11a1ddaa18cc49813b48f686c9509f314dc suite:test/test_borrow_polonius.ml 832d4c8b64176706535334a5dbbb16345f42b75ac9b1a93f0a5dc371d84b5a0d test:test_801_same_name_in_two_modules 874a5531cd8bc402adf4b66277b21b15d921efe06a7128ea6583bcd6e55781ba test:test_resume_nontail_xfail +89b920568ab703102c224d40a1846efef67d60b88aed4cddc9a84e6f095a47d1 suite:test/test_block_scope.ml ac1da10c3206605304f11e869f6aa07acba57f6f8248bb2dfacca19984f86c31 test:test_coherence_duplicate_rejected b76c7824a9b1570d66752359a28d75ebdfaedb5de5ad68c343a0a2c26ad4221c fixture:test/e2e/fixtures/async_sync_fallback.affine c743b86cfc50512e5867ddd792b4b4273b5bfb6f40205b4a07019031d7e7b102 fixture:test/e2e/fixtures/handle_resume_multishot.affine From eec3a6a3f3e50e83245cdaf9e2a47d2ea47a9af2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 17:29:11 +0000 Subject: [PATCH 3/3] docs(soundness): restore the #826 ledger row lost in the main merge The merge of main (c33da4f) kept #838's row and dropped #826's, while tools/soundness-anchors.sha256 still pinned test/test_block_scope.ml, so the soundness-ledger gate failed (property 3). Both rows now stand; the gate passes and the manifest needs no reseal. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- docs/SOUNDNESS.adoc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/SOUNDNESS.adoc b/docs/SOUNDNESS.adoc index 92c9479b..22050505 100644 --- a/docs/SOUNDNESS.adoc +++ b/docs/SOUNDNESS.adoc @@ -239,6 +239,15 @@ this reason until impl methods get distinct names. | `test_801_impl_methods_refused` (the AOT smoke pins `traits.affine`'s refusal in `expected_refusals`). +| #826 +| A block's `let` bindings end with the block in the type checker, as they +already did in the resolver. `let x = "s"; if c { let x = 1; } x + 1` used +to type-check as an `Int` and return the string `"s1"` at run time; it is now +a type error, and an outer binding's type is back after any block. +| `fixed` +| `test/test_block_scope.ml` (if, bare and while blocks; the converse; a +builtin shadowed in a block; the inner binding in scope). + | #838 | On core wasm a local (parameter, let-bound closure) shadows a top-level function of the same name, as on the interpreter and Bun-ESM. The generic call