let bindings destructure, and so does let! - #57
Merged
Conversation
`let (r, c) = parseCoord tok` did not parse anywhere in the language, so a function that wanted two names out of one value carried a `match` that existed for no other reason. `LetBinding` and the CE `let`/`let!` items now hold a `Pattern` where they held a `String`, with `bound_names`/`bound_vars` on the AST as the single place any phase asks what a binding introduces. The irrefutability rule already existed: `refutable_in_param`, renamed `refutable_shape` and shared, so a target admits exactly what a parameter does (a name, `_`, tuples, records, nested) and rejects the rest by pointing at `match`, which has somewhere to fall through to. A function binding and a `let mut` keep their single name, each with its own message. Lowering reuses `unpack_into`: `let (r, c) = e` emits `r, c = e` with no temp, nested targets read through a reserved base rather than a name derived from the user's, and a destructuring `let!` rides inside the `Ok` pattern `result` already matches, so it costs no extra statement. Each bound name generalizes on its own, as the single-name case does. Closes ROADMAP dogfooding finding 9.
This was referenced Aug 2, 2026
Merged
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.
Closes dogfooding finding 9.
let (r, c) = parseCoord tok,let Point { x, y } = originandlet (a, (b, c)) = nestednow parse at top level, in blocks, in an in-filemodule, and on a computation expression'slet/let!.What changed
LetBindingandCeItem::Let/LetBangcarry aPatternwhere they carried aString.Pattern::bound_names/bound_varson the AST are the one place any phase asks what a binding introduces, which is what every scope-tracking path (lowering's scan, the fold pass, the module-alias mangling, the LSP resolver, the type checker's free-variable walk) now consults instead of a single name.LetBinding::name()stays as the plain-name shortcut for the common case, mirroring theParam::name()that was already there.The irrefutability rule was already written:
refutable_in_param, renamedrefutable_shapeand now shared between the two binding positions. A target admits exactly what a parameter does (a name,_, tuples, records, nested) and rejects everything else with a message naming what was written and pointing atmatch. A function binding and alet mutkeep their single name, each with its own message.Emitted Python
Lowering reuses the existing
unpack_into, so the output is what a Python programmer would write:let (r, c) = er, c = elet (a, (b, c)) = pa, _pf_t0_1 = pthenb, c = _pf_t0_1let Point { x, y } = px = p.xtheny = p.ylet! (r, c) = einresultcase Ok((r, c)):Nested targets read through a reserved base rather than a name derived from the user's, so a binding called
pnever hasp_1invented next to it. Theresultcase is the one worth calling out: the target rides inside theOkpattern the bespoke lowering already matches, so a destructuring bind costs no extra statement.Each bound name generalizes on its own, the same let-generalization a single-name binding gets. There is no value restriction to trip over, since
mutbindings are monomorphic and named.Verification
cargo fmt --checkandcargo clippy --all-targetsclean.let!unwrapping before destructuring), lowering assertions on the emitted Python, three e2e programs, one LSP go-to-definition test landing on one element of a tuple target.docs/verify_lessons.pypasses on all 23 lessons, including the new lesson 8 section (its quoted diagnostic is checked against the real one).Docs
DESIGN.md§7 gains the canonical paragraph next to the parameter-destructuring one it shares its rule with;INTERNALS.mdrecords the AST change; lesson 8 ("Tuples and destructuring") gains a section;CLAUDE.mdstatus and the ROADMAP entry are updated.