Repository navigation
Sema: a pattern never skips a destructor — destructuring a Drop value errors outside the type's own move fn, total inside it (#1272) - #1291
Merged
Conversation
… errors outside the type's own move fn, and must be total inside it (#1272) A struct/enum pattern over a by-value subject consumes the whole and MirLower cancels the scratch drop so the arms own the payloads (#605/#606 block, lower_let_else / lower_match / lower_if_let). For a type with its own Drop that skipped the destructor silently: `let { repr } = r` leaked `r` for the cost of a pattern. Ruling (Eric, 2026-09-22): the only way to skip a destructor is a spelling visible at the type's own boundary. A pattern applied to a Drop value is a compile error everywhere except inside that type's own `move fn` methods (`drop` included), where the destructure is the visible disarm and must be total — every field bound by name or explicitly `_`. One rule for `let` patterns, `match` arms and `if let` (a match). `let t = r`, `let _ = r` and a `_` arm keep the value whole and still run Drop; a borrowed subject (`match &r`) binds views and never moves. Implemented as a Sema check at pattern-subject resolution (`drop_pattern_gate`: `type_has_drop_impl` on the subject, the enclosing method's receiver mode and owner from `current_fn_symbol`). MirLower is unchanged: inside the type's own move fn the bypass is the intended behavior. Outside, the error names the type and its Drop with fix-its `let t = r` (keep it whole) and `r.field` (read a field; `match &e` for an enum); inside, a partial pattern names the fields left unbound and offers `field: _` for each. A rejected pattern still binds its names, so the error stands alone. Tests: compile errors for `let`/`match`/`if let`/enum match outside the type (each asserting the fix-it text) and for a partial struct/variant pattern inside a `move fn`; a counting-destructor behavior test proving Drop runs exactly once for `let t = r` and `match r: _ =>`, that a borrowed subject runs no Drop early, and that a total destructure inside the type's own `move fn` (and inside `drop`) runs no Drop. Sweep: every Drop-declaring file in test/, lib/std, tools/, examples/ checks clean under the new stage1; the only sites the gate trips are the new error tests.
ehartford
force-pushed
the
fix-1272-drop-destructure-rule
branch
from
September 22, 2026 13:37
0059333 to
1ecdbd3
Compare
Contributor
Author
|
Rebased onto main |
This was referenced Sep 22, 2026
ehartford
added a commit
that referenced
this pull request
Sep 22, 2026
…attern in let) (#1312) * Fix #1295: a char or byte literal is a match pattern §22 makes a char/byte literal an integer literal value and §29 lists CHAR_LIT under LITERAL_PAT, but parse_pattern only knew TK_INT_LIT and is_arm_token (the match-arm start predicate) did not list TK_CHAR_LIT, so the arm list ended silently before `'{' =>` and the line fell through to the statement parser ("expected expression" at `=>`). - char_literal_value(text) is the one decoder for 'X' / b'X' and its escapes; parse_char_literal and the pattern parser both call it. - parse_pattern treats TK_CHAR_LIT like TK_INT_LIT: bare (NK_PAT_INT) and as either endpoint of a range ('a'..='z', b'0'..=b'9'). - is_arm_token and the for/parameter pattern-start predicates admit TK_CHAR_LIT. test/behavior/behav_match_char_literal_pattern.w covers bare, escaped, ranged and constructor-nested (Some(b'{')) forms. * Fix #1299: a named struct pattern parses in let `let Req { name, email } = req` failed with "let binding requires initializer": parse_let_binding routed a destructuring let only when the pattern began with (, { or [, so an uppercase name followed by { fell into the plain-binding path, which then found no `=`. §29's STRUCT_PAT is `[ TYPE ] '{' ... '}'` and §9.7 says every pattern form is available in let. An uppercase name followed by `{` now builds the pattern with parse_struct_pattern — the same NK_PAT_STRUCT node a match arm builds, so Sema's Drop gate (#1291) and MirLower see one form — and the `= value [else: body]` tail is shared with the anonymous forms via parse_let_pattern_rest. Tests: behav_let_named_struct_pattern.w (by value on a non-Drop struct, view bindings over &req, rename, .., nested named patterns); err_drop_destructure_let_named.w (the named form is refused on a Drop type with the same fix-it as the anonymous form). --------- 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 #1272.
Ruling (Eric, 2026-09-22)
A struct/enum pattern applied to a value whose type implements
Dropis a compile error everywhere except inside that type's ownmove fnmethods (dropincluded), where the destructure is the visible disarm — and there it must be total: every field bound by name or explicitly_; a partial pattern (let R { fd, .. } = self) is an error. One rule shared byletpatterns,matcharms andif let.let t = r,let _ = r,match r: _ =>(no destructuring) are unaffected and keep running Drop.Principle for the spec: the only way to skip a destructor is a spelling that's visible at the type's own boundary.
Implementation
drop_pattern_gateinsrc/SemaCheck.w): keyed ontype_has_drop_implof the by-value subject (a&Tsubject is untouched — it binds views and never moves) and on the enclosing method's receiver mode and owner (current_fn_symbol→sig_receiver_mode == Move, owner prefix). Hooked from the struct-pattern branch and the variant/positional-struct branch ofcheck_pattern, solet,match,if let(a match) and nested patterns share it.lower_let_else/lower_match/lower_if_let) is unchanged: inside the type's ownmove fnthe bypass is the intended behavior.cannot destructure `R` by pattern: `R` implements Drop, and taking it apart would skip its destructorwith helpkeep it whole (`let t = r`) or read a field (`r.repr`); only a `move fn` of `R` may take it apart(match &ein place of the field read for an enum). Inside amove fn, a partial pattern:partial pattern on `R` inside its own `move fn` leaves `fd` unboundwith helpadd `fd: _`(payload positions for a variant..). A rejected pattern still binds its names, so the error stands alone.docs/decisions.mdD54 records the ruling and the reference comparison (Rust E0509 / Vale / Swiftdiscard self/ Mojo__disable_del). No normative spec text is edited: proposed §2.5.1 / §Patterns wording is below for Eric's blessing.Tests
test/compile_errors/err_drop_destructure_{let,match,if_let,enum_match}.w— error outside the type, each asserting the fix-it text.test/compile_errors/err_drop_destructure_partial_in_move_fn.w,..._enum_partial_in_move_fn.w— partial pattern inside amove fnnames the unbound field / payload.test/behavior/behav_drop_destructure_rule.w— counting destructors (R adds 1, E adds 10): Drop runs exactly once forlet t = randmatch r: _ =>;match &r/match &erun nothing early; a total destructure inside the type's ownmove fn(and insidedropitself) runs no Drop.Sweep
Every Drop-declaring file in
test/,lib/std,tools/,examples/checked under the new stage1: the only sites the gate trips are the new error tests.with check src/main.wok. (behav_match_arm_binding_drop_scope.wfails identically on the installed compiler and main's stage2 — pre-existing, verified.)Gates (seed-driven,
WITH=$PWD/src/main)src/main build— green (stage2 89.9s, all targets green)src/main build :move-audit— greensrc/main build :drop-audit— green:fixpoint/:test/ battery not run per instruction; needed before merge.Proposed spec wording (for Eric's blessing, not landed)