Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/SOUNDNESS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions lib/typecheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
118 changes: 118 additions & 0 deletions test/test_block_scope.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
(* SPDX-License-Identifier: MPL-2.0 *)
(* Copyright (c) 2026 Jonathan D.A. Jewell <jonathan.jewell@open.ac.uk> *)
(** 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:"<test_block_scope>" 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;
]
1 change: 1 addition & 0 deletions test/test_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tools/soundness-anchors.sha256
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
ad1e66df361641660125a84fb4694785357d542b0a7856de8f28e4d81f8635b5 test:test_wasm_let_shadows_fn
b76c7824a9b1570d66752359a28d75ebdfaedb5de5ad68c343a0a2c26ad4221c fixture:test/e2e/fixtures/async_sync_fallback.affine
Expand Down
Loading