Repository navigation
fix(typecheck): end a block's let bindings with the block - #826
Conversation
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
|
Review these changes at https://app.gitnotebooks.com/hyperpolymath/affinescript/pull/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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 42 minutes. View limit details
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
Signed-off-by: Jonathan D.A. Jewell <6759885+hyperpolymath@users.noreply.github.com>
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
…-k0mc5l-block-scope
|
…bles (#827) Two Bun-ESM miscompiles on `main`, found while answering review on #798. They are separate from the type-checker hole in #826. ## 1. A `let` that reads or shadows an enclosing name A JS `const x` is in its temporal dead zone (TDZ) for its whole block, so the old emission failed in two ways: | Source | Emitted on `main` | Result | |---|---|---| | `let y = { let x = x + 1; x };` | `const x = (x + 1)` in the block | `ReferenceError` | | `pub fn p(x: Int) -> Int { let x = x + 1; x }` | `function p(x) { const x = (x + 1); … }` | `SyntaxError`: **the whole module fails to load** | **Fix.** A `let` binder is emitted under a fresh `name$N` when its initializer mentions the name, or when it shadows a name already bound in the function. `$` cannot occur in a source identifier, so fresh names never collide. References resolve through a scoped rename table in `resolve_var`. Binders that keep their own name clear the rename for their scope: parameters, lambda parameters, match arms and loops. Destructuring and punned record patterns are renamed the same way. ## 2. An Int-typed lambda parameter ```affine pub fn lambda_int(s: String) -> Int { let f = fn(len: String -> Int) => len(s) / 2; f(fn(t: String) => 5) } ``` This returned `2.5` on `main`: a function declared `-> Int` returned a non-integer. CodeRabbit left this open on #798. Lambda parameters are now seeded the way function parameters are. An untyped or non-Int parameter also stops a same-named outer `Int` from truncating. ## Evidence - `tests/codegen-bun/let_rebind` has 7 cases. On `main` its module fails to load (`"x" was originally declared here`). - `dune build @test/runtest`: 662 tests pass. Bun-ESM: 9 harnesses. Legacy codegen-deno corpus: 42. Wasm: 48. All pass. - `tools/check-soundness-ledger.sh` passes. A ledger row follows in the next commit. --- _Generated by [Claude Code](https://claude.ai/code)_ --------- Co-authored-by: Claude <noreply@anthropic.com>



A type-checker soundness hole found while answering review on #798.
The hole
synth_blocknever removed a block-local binding's type fromname_types. The binding therefore replaced the type of any outer binding with the same name for the rest of the function. The resolver scopes blocks correctly, so only the types were wrong:On
main(ee211d3) this type-checks, andunsound(false)returns the string"s1"on Bun-ESM; it also compiles to Wasm. The converse (an outerIntshadowed by a block-localString) is wrongly rejected.The fix
synth_blocknow saves the types of the names itslets bind and restores them when the block ends, throughFun.protect.Evidence
test/test_block_scope.mladds 6 cases: the unsound program forif, bare andwhileblocks; the converse; a builtin shadowed inside a block; and the inner binding staying in scope. Five fail without the change. The sixth passes either way.dune build @test/runtest: 662 tests pass.Not in this PR
On Bun-ESM,
let x = x + 1in a nested block reads the innerconst xbefore it is initialised. JavaScript's temporal dead zone (TDZ) forconstmakes that aReferenceError. The fix is to rename the inner binding in codegen. It is pre-existing and separate from this hole; it will follow in its own PR.Generated by Claude Code