feat: add block scoping for if/elif/else and match arms#254
Merged
Conversation
Scope is-bindings and pattern bindings to their own block frame, reusing the while-block scope machinery: each if/elif/else branch and each match arm pushes a frame on entry and pops it at the next branch boundary or `fi`/`end`. A binding is visible only inside its branch/arm, never in a sibling branch, the else, or after the block. Implicit bindings that would shadow an accessible block-local or function-level variable are rejected (DuplicateName); globals remain a separate, exempt namespace and explicit `= x` may still mutate an outer variable. Refactor every compiler, stdlib, and lsp site that relied on a binding leaking past its block (the early-return guard idiom) to the positive form `if X Variant(b) is then <body> fi` or an `.unwrap` hoist, so the compiler bootstraps under the stricter rules. Closes #248 Closes #249
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements strict block scoping for
if/elif/else/fi(#248) andmatcharms (#249), reusing the while-block scope machinery from #251.if/elif/elsebranch and eachmatcharm gets its own scope frame: push on entry, pop at the next branch boundary orfi/end.is-binding or pattern binding is visible only inside its own branch/arm — not in a sibling branch, not in theelse, not after the block.DuplicateName. Globals are a separate, exempt namespace; explicit= xmay still mutate an outer variable, and a new= xwith no outer binding is block-local.To bootstrap under the stricter rules, every compiler / stdlib / lsp site that relied on a binding leaking past its block (the early-return guard idiom) was refactored to the positive form
if X Variant(b) is then <body> fior an.unwraphoist — no behavioral change, just no leak.Test plan
test_scope: 13 new cases — is-binding then-only / not-in-else / not-after-fi, elif independence, outer mutation, block-local new var, shadow error; match per-arm visibility, cross-arm invisibility, shadow error, outer mutation, block-local new varif_is_binding_out_of_scope,match_arm_binding_out_of_scope(UNDEFINED_NAME);if_binding_shadow,match_binding_shadow(DUPLICATE_NAME)lsp.casaandformatter/format.casabuild; all changed filescasafmt-cleanCloses #248
Closes #249