Repository navigation
Fix #1302: a match on a place observes it; only a pattern that binds an owned value consumes the subject - #1314
Merged
Merged
Conversation
added 2 commits
September 22, 2026 11:10
…an owned value consumes the subject `match self.cur:` on a non-Copy enum field moved the field into a scratch temp, reset it to all-zero, and dropped it in the arms; the next match decoded the sentinel as variant 0 of variant 0 (`Some(.LBrace)`). The same lowering consumed a local subject (`match x` twice read the sentinel the second time), `x.field`, nested fields, elements, `if let` and `let-else`. Root cause (MirLower.lower_match, `--dump-mir` of the issue's repro): `lower_expr(scrutinee)` lowers a place subject to a lazy `OK_MOVE _1.cur` and `materialize_operand` assigns it into `_4`, whose assign_operand_to_place → consume_moved_operand queues the field reset (`_1.cur = const zst`); the `_` arm then moves `_4` into a drop local. Sema's check_match_expr never marked the subject consumed at all, so the two phases disagreed: Sema observed, MIR consumed, and no validator can see that (validate-all and validate-ownership both say ok — the MIR is a well-formed move + reset). Rule (§2.2 D32, §9.7 D22/D27): a pattern is structural projection. One predicate in one home: Sema.pattern_binds_owned_value — an arm CONSUMES its subject only when it binds a non-Copy value by value (or is the #1272 disarm of a Drop type). A consuming arm marks the subject before its body: a local becomes moved (§2.2 flow rules govern later uses), a field is the D32 implicit-field-move error with its `move`/`.clone()` fix-its, a temporary or explicit `move` subject needs nothing. Sema records the verdict in consuming_pattern_subjects; MirLower reads it and matches a non-consumed, non-Copy, by-value place (local, field chain, element) IN PLACE — no temp, no reset, no cancelled drop — and `_`/`..` on an observed subject move nothing into drop locals. The flag wraps only each match's own pattern lowering so a nested match on a temporary keeps its payload drops. After: `_5 = discriminant(_1.cur)` and the field is untouched; the subject matrix (mut fn / fn receiver, local, nested field, element, if-let, let-else, Copy payload binding, `move` subject) runs clean under --debug-alloc. Tests: behav_match_place_observes (twice-in-a-row matches on every subject shape still see the variant; `move` transfers exactly once, counted by Drop), da_match_place_observes (leak count=0 with str payloads), err_match_field_binds_owned (D32 on a by-value payload binding out of a field), err_match_local_consumed_then_used (use-after-move on the local).
…ove envelope.message`) The generated workspace-API fixtures (build/selfhost.w) and the pcre2 cohesive check bind non-Copy payloads (`decls`, `command`, `message`, `done`) out of `envelope.message`, a field of a `let` local — the implicit field move §2.2 (D32) forbids, which the match lowering let through silently until #1302. Apply the diagnostic's own fix-it: a `var` base and the explicit `move` subject. Mechanical (`with -p` over both files).
ehartford
marked this pull request as ready for review
September 22, 2026 15:30
Contributor
Author
|
Verified the |
ehartford
added a commit
that referenced
this pull request
Sep 22, 2026
…g (release-only segfault); da_match_place_observes never allocated (#1322) * ComptimeTransform: own the texts taken from intern.resolve — the derive generators held a view across interning `out/release/bin/with check test/behavior/derive_enum_debug_clone.w` segfaulted (rc 139, no output); the same test passed on the stage1 built by the pinned seed, so main went red only in the battery (behavior-tests lane, release binary) after #1311 and #1314 merged. Root cause (lldb on the cffb1cb release binary, frame Sema.ct_generate_debug_derive +1432): -> ldp x8, x9, [x20] ; x20 = [sp+0x28], the `type_name` &str ... bl with_str_concat_n ; type_name ++ ".debug_str" EXC_BAD_ACCESS address == x20; x27 (the live symbol_texts buffer) is in another region; the page under x20 reads as zeros. `InternPool.resolve` returns `&str`, a view into `symbol_texts: Vec[str]` (InternPool.w:136). Every generator bound it — `let type_name = intern.resolve(type_name_sym)` — then interned new texts (`ct_build_string_lit`, `intern.intern`) before its last use, so the table grew, the old buffer was freed, and the tail read through the dangling view. Which compiler generation lowers that `let` as a true D27 view (the current one) versus a header copy (the older seed) is the "layout" that made it release-only; which derives run in one module is what made the growth land inside this function. Fix: own the text at every such binding (17 sites, one `with -p` rewrite: `intern.resolve(sym).clone()`), the same spelling SemaDecl.w:907 already uses for its own pool text. No generator logic changes. Evidence: the rebuilt release binary compiles and passes derive_enum_debug_clone.w and the derive/match suites; :move-audit and :drop-audit ran on the batch (results in the PR). * da_match_place_observes: build the str payloads at runtime so the allocator report exists The fixture's payloads were all-literal `++` chains (`"one" ++ "two"`), which MirLower.try_fold_literal_str_concat folds to one constant (`_1 = const "sym…"` in --dump-mir), so the program never allocated, `with_debug_alloc_report_leaks` returned before printing (dbg_base == 0), and the lane failed on `want: 'leak count=0'` with no report line to read. The match-observes rule under test was never exercised over heap payloads, which is the point of the fixture. A two-parameter helper (`fn heap(a: str, b: str): a ++ b`) keeps every asserted length and produces a runtime concat (`str_concat_n` in the MIR); `run --debug-alloc` now prints `leak count=0` and the lane driver passes the fixture. --------- Co-authored-by: Eric Hartford <[email protected]>
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.
Fixes #1302.
Symptom
match self.field:(andx.field, a nested field, an element, a plain local,if let,let-else) on a non-Copy enum moved the subject out of its place, reset the place to all-zero, and dropped the value in the arms. The next match decoded the sentinel as variant 0 of variant 0 (Some(.LBrace)), so the corruption read as a plausible stale token. Sema never flagged it: the latershow(&x)compiled.Root cause (tool evidence)
with check q5.w --dump-mir,P.outerbefore:Sema's
check_match_exprnever marks a match subject consumed at all, whilelower_matchunconditionally materializes it and cancels its drops — the two phases disagreed (Sema: observe; MIR: consume).--validate-all/--validate-ownershipboth sayokon that MIR: it is a well-formed move + reset, so no validator can see the contract mismatch (noted below).After:
_5 = discriminant(_1.cur), the field untouched, no reset, no drop.Rule (spec §2.2 D32, §9.7 D22/D27)
A pattern is structural projection, not an owned demand. One predicate, one home —
Sema.pattern_binds_owned_value: an arm consumes its subject only when it binds a non-Copy value by value (or is the #1272 Drop disarm). A consuming arm marks the subject before its body: a local becomes moved (§2.2 flow rules govern later uses), a field is the D32 implicit-field-move error with itsmove/.clone()fix-its, a temporary or explicitmovesubject needs nothing. Sema records the verdict (consuming_pattern_subjects); MirLower reads it and matches a non-consumed, non-Copy, by-value place in place;_/..on an observed subject move nothing.Evidence
false false(wasfalse true); subject matrix (mut fn / fn receiver, local, nested, element, if-let, let-else, Copy payload,move) runs with--debug-alloc: leak count=0.behav_match_place_observes,da_match_place_observes,err_match_field_binds_owned(D32 on a by-value payload binding out of a field),err_match_local_consumed_then_used(use-after-move on the local). Sema: a pattern never skips a destructor — destructuring a Drop value errors outside the type's own move fn, total inside it (#1272) #1291's four pattern tests andbehav_match_returning_arm_movestill pass.with check src/main.wclean;src/main buildgreen;:move-auditgreen;:drop-auditgreen;:cli-selfhost-build-w-testsgreen.build/selfhost.wandbuild/pcre2.w, which bounddecls/command/message/doneout ofenvelope.message— a field of aletlocal, the exact silent field move D32 forbids. Second commit applies the diagnostic's own fix-it (var+match move envelope.message), mechanically viawith -p.Open points
:examples-testsis red onexamples/fizzbuzz.w("match expression of typestrcannot produce&str"). Reproduced with the installed pre-fix compiler; the expression is a Copy tuple temporary with no owned bindings, outside every path this change touches. Verifying on a clean origin/main build before un-drafting.--validate-allon the 20-line repro ran 15 min at 7 GB RSS (release binary: 0.07 s). Filed separately.analyzeaudit joiningconsuming_pattern_subjectsagainst MIR move operands on pattern subjects would close the class.