Skip to content

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
ehartford merged 1 commit into
mainfrom
fix-1272-drop-destructure-rule
Sep 22, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1272-drop-destructure-rule

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1272.

Ruling (Eric, 2026-09-22)

A struct/enum pattern applied to a value whose type implements Drop is a compile error everywhere except inside that type's own move fn methods (drop included), 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 by let patterns, match arms and if 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

  • Sema check at pattern-subject resolution (drop_pattern_gate in src/SemaCheck.w): keyed on type_has_drop_impl of the by-value subject (a &T subject 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 of check_pattern, so let, match, if let (a match) and nested patterns share it.
  • MirLower's cancellation (lower_let_else / lower_match / lower_if_let) is unchanged: inside the type's own move fn the bypass is the intended behavior.
  • Diagnostics — outside: cannot destructure `R` by pattern: `R` implements Drop, and taking it apart would skip its destructor with help keep it whole (`let t = r`) or read a field (`r.repr`); only a `move fn` of `R` may take it apart (match &e in place of the field read for an enum). Inside a move fn, a partial pattern: partial pattern on `R` inside its own `move fn` leaves `fd` unbound with help add `fd: _` (payload positions for a variant ..). A rejected pattern still binds its names, so the error stands alone.
  • docs/decisions.md D54 records the ruling and the reference comparison (Rust E0509 / Vale / Swift discard 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 a move fn names the unbound field / payload.
  • test/behavior/behav_drop_destructure_rule.w — counting destructors (R adds 1, E adds 10): Drop runs exactly once for let t = r and match r: _ =>; match &r / match &e run nothing early; a total destructure inside the type's own move fn (and inside drop itself) 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.w ok. (behav_match_arm_binding_drop_scope.w fails 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 — green
  • src/main build :drop-audit — green
  • :fixpoint / :test / battery not run per instruction; needed before merge.

Proposed spec wording (for Eric's blessing, not landed)

§2.5.1: The only way to skip a destructor is a spelling visible at the type's own boundary. A struct or enum pattern applied to a value whose type implements Drop is an error outside that type's own move fn methods; inside them it is the disarm and must be total — every field bound by name or _.

… 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
ehartford force-pushed the fix-1272-drop-destructure-rule branch from 0059333 to 1ecdbd3 Compare September 22, 2026 13:37
@ehartford

Copy link
Copy Markdown
Contributor Author

Rebased onto main 83b3f1f8; the duplicate D54 decisions entry was dropped (main's D55 ruling 3 already records the #1272 ruling). Battery re-running on the rebased tree; verdict below.

@ehartford
ehartford merged commit 02e618e into main Sep 22, 2026
0 of 5 checks passed
@ehartford
ehartford deleted the fix-1272-drop-destructure-rule branch September 22, 2026 13:41
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Destructuring a Drop-typed value by pattern (let { .. } / match) never runs its Drop — a silent leak

1 participant