Skip to content

The alternatives of an or-pattern share their bindings (D221, #823) - #3458

Merged
ehartford merged 7 commits into
mainfrom
or-pattern-bindings
Nov 19, 2026
Merged

ehartford merged 7 commits into
mainfrom
or-pattern-bindings

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #823. Delegated (D98; D221 records the brief): it adds no ownership rule. The arm means exactly what the same arm written once per alternative means.

match err:
    .WebModelCall(webc_error, ..) | .WebAdapterCall(webc_error, ..) =>
        webc_error.status()
    _ => 0

Rule (§9.7, spec v7.67). Every alternative binds the same names, each with one type; the guard and the body see one binding per name, made by the alternative that matched. A missing name, an extra name, or two types is an error that names the alternative.

Sema. The first alternative introduces the bindings; a later one names them again (check_or_pattern, and scope_put_at's shared path), where it used to be refused as shadowing.

MirLower.

  • Which alternative matched is recorded in a local; lower_or_pattern_bindings binds from it in that alternative's own block, into the first alternative's locals.
  • A place binding (an observed subject) is held by reference, because the place differs by alternative.
  • Only one alternative ran, so each path blanks what the others scheduled for drop.
  • On a failed guard, only the matched alternative's moves are put back (restore_pattern_move_span).

Evidence. --validate-all and audit:all clean on the test programs; under the debug allocator each value is dropped once and nothing leaks; a failed guard returns the value and the next arm binds it; a mutating call through the shared name writes an observed subject.

Not yet. A list pattern that takes elements inside an alternative is refused at lowering (its log is not split by alternative), never lowered wrongly. Alternatives nested in a payload, and if let with |, are not in the grammar.

Five tests. Battery green on the branch tip; :release-checks green (the guard-restore path every pattern uses was restructured).

@ehartford
ehartford merged commit 5ae80c1 into main Nov 19, 2026
@ehartford
ehartford deleted the or-pattern-bindings branch November 19, 2026 06:33
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.

Pattern sugar: allow shared bindings across OR-pattern alternatives

1 participant