Repository navigation
Description
Proposal
Allow every alternative of an OR-pattern to bind the same names and expose one logical set of bindings to the arm guard and body.
This should be valid With:
match err:
.WebModelCall(webc_error, ..) | .WebAdapterCall(webc_error, ..) =>
webc_error.status()
_ => 0
It is sugar for the currently required duplication:
match err:
.WebModelCall(webc_error, ..) => webc_error.status()
.WebAdapterCall(webc_error, ..) => webc_error.status()
_ => 0
This comes up naturally when several variants carry the same semantic payload but have different secondary fields. Rust supports the equivalent form:
Error::WebModelCall { webc_error, .. }
| Error::WebAdapterCall { webc_error, .. }
=> webc_error.status()Binding rules
- Check each OR-pattern alternative in an independent binding environment.
- Every alternative must introduce exactly the same binding names.
_and bare..introduce no binding. - A corresponding binding must have the same resolved type, ownership/binding mode, and view origin in every alternative.
- The arm guard and body see one arm-local binding for each agreed name.
- The selected alternative initializes each logical binding exactly once. A failed alternative must not move or drop anything.
- Binding position does not need to agree; names define the correspondence.
Examples that should be rejected:
.A(x) | .B(_) => use(x) // `x` is absent from `.B`
.A(x) | .B(y) => use(x) // alternatives bind different names
.A(x) | .B(x) => use(x) // reject if the two payload types/modes differ
The diagnostic should name the alternative and explain the first missing or incompatible binding, rather than report shadowing.
Current behavior
The proposed pattern parses, but current main rejects it with:
error: shadowing is not allowed for 'webc_error'
The failure was reproduced with the official v0.15.1.3 compiler. Inspection of current origin/main at 65927768 shows the same cause: Sema.check_pattern handles NK_PAT_OR by checking every alternative sequentially in the same scope. The first alternative inserts webc_error; the second reaches scope_put_at, sees that name already present, and emits the ordinary shadowing diagnostic. There is no alternative-local binding collection and merge step.
Acceptance tests
- Shared
Copypayload binding works in the body. - Shared non-
Copypayload binding moves exactly once and drops exactly once. - Shared view payload preserves its exact projected type and view origin.
- A shared binding is available to an arm guard.
- Nested OR-patterns merge bindings correctly.
- Variant payload rest (
..) composes with shared bindings. - Missing-name, different-name, different-type, and different-mode alternatives are rejected with targeted diagnostics.
- Existing binding-free OR-patterns retain their behavior.
with buildandwith build :fixpointpass.
Activity
ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): still reproduces. Both alternatives binding the same name is refused as shadowing:The spec does not decide it: §9.7 says only that "Or-patterns share a body", with a binding-free example. Whether alternatives may bind the same names (and must bind the same set, with one type each) is a language decision.
ehartford commentedon Nov 18, 2026
Taken under delegation (D98; prediction 85%): alternatives of an or-pattern may bind the same names, and must bind the same set with one type each.
What the code needs, found by reading it: Sema checks every alternative in one scope (hence 'shadowing is not allowed'), and that error is load-bearing today, because the lowering binds only the first alternative's bindings whichever alternative matched. So the work is in two places: Sema admits a re-binding of exactly the names the first alternative bound (same type, same binding mode), and the lowering gives each alternative its own binding block that writes the shared locals before the arm's body. A binding that is a place alias (D144) cannot share a local across different places and needs its own answer. Drop and move audits run with it.
The alternatives of an or-pattern share their bindings (D221, #823) (#…