Skip to content

Pattern sugar: allow shared bindings across OR-pattern alternatives #823

Closed

Description

@ropoctl

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

  1. Check each OR-pattern alternative in an independent binding environment.
  2. Every alternative must introduce exactly the same binding names. _ and bare .. introduce no binding.
  3. A corresponding binding must have the same resolved type, ownership/binding mode, and view origin in every alternative.
  4. The arm guard and body see one arm-local binding for each agreed name.
  5. The selected alternative initializes each logical binding exactly once. A failed alternative must not move or drop anything.
  6. 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 Copy payload binding works in the body.
  • Shared non-Copy payload 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 build and with build :fixpoint pass.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): still reproduces. Both alternatives binding the same name is refused as shadowing:

    enum E { A(i32, i32), B(i32, i32), C }
    fn f(e: E) -> i32:
        match e:
            .A(x, _) | .B(x, _) => x
            _ => 0
    
    error: shadowing is not allowed for 'x' (bound earlier in this scope)
    

    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.

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. ehartford commented on Nov 18, 2026

    @ehartford
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions