Skip to content

Bindings in | patterns - #1969

Open
sbillig wants to merge 11 commits into
argotorg:masterfrom
sbillig:feat/or-pattern-bindings
Open

sbillig wants to merge 11 commits into
argotorg:masterfrom
sbillig:feat/or-pattern-bindings

Conversation

@sbillig

@sbillig sbillig commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

The alternatives of a | pattern may now bind names, as in Rust:

match self.kind {
    ChangeKind::Account { existed, .. } | ChangeKind::Sender { existed, .. } => !existed,
    _ => false,
}

This used to fail with 8-0032 duplicate binding and 8-0076 "bindings in | patterns are not supported".

  • Type checking joins each name the alternatives share into the first alternative's binding. Each alternative must bind the same names with one type and one binding mode; new errors 8-0159 (name missing from an alternative), 8-0160 (types differ) and 8-0161 (binding modes differ) replace 8-0076.
  • The decision tree expands a binding | where a switch tests its column, and a leaf binds what the alternative it took matched.
  • In if let, while let, let, let ... else and for, the matching leaves meet in one block. Bindings every leaf shares bind there once; values that depend on the alternative bind per leaf. An access (mut x, ref x) bound at a different part in each alternative would need two targets, which normalized IR doesn't allow, so it is reported as 8-0162 with a hint to use match (it previously failed with an internal 16-0004).
  • Find-references and rename cover every alternative's binding site.

Also:

  • A match arm reached from several decision-tree leaves (each | alternative, or each path to a catch-all arm) lowers its body once; the leaves bind and jump to it. An arm that binds an access at a different part per alternative keeps a body per leaf. A probe contract's runtime code goes from 1259 to 743 bytes.
  • Alternatives after one that matches anything are left out of the exhaustiveness check and the decision tree, so each such | column no longer doubles the matrix.

Docs in docs/language/patterns.md.

`A { x, .. } | B { x, .. } => x` failed with duplicate-binding errors.
Each alternative now binds its names afresh, and a name both bind is
the left alternative's binding. The alternatives must bind the same
names with one type and binding mode (8-0159, 8-0160, 8-0161), and
8-0076 is gone.

The decision tree gives each binding alternative rows of its own, so a
leaf binds what its alternative matched; `match` lowers each arm per
leaf. Outside `match` the matching leaves meet in one block: bindings
every leaf shares bind there once, and values that depend on the
alternative bind per leaf. An access bound at a different part in each
alternative can't meet there, so it is reported (8-0162). Irrefutable
patterns with binding alternatives are tested like `let ... else`.
References and rename found the uses of a binding and one binding site,
so renaming `x` in `A(x) | B(x)` left the other alternative's `x`. The
first alternative's site is the definition, and the others count as
references.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T02:56:04.800012Z 87bcebc New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e406e15cc5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Each `|` pattern that binds names was expanded up front into a row per
combination of alternatives. `(a | a, b | b, ...)` over 20 fields took
44 s and 12 GB before the decision tree dropped the redundant rows, and
checking an `if let`, `let` or `for` expanded the same way.

A row now expands a binding `|` pattern only when a switch tests its
column, and records the alternative it takes; a leaf binds by walking
the arm's pattern through those choices. The check for accesses bound
apart compares each binding's occurrences, once the body's types are
known.
…dings

# Conflicts:
#	crates/language-server/src/functionality/rename.rs
`V { n: x | x }` and `T { t: (true, x, _) | (false, _, x) }` run, and a
name missing from an alternative is reported at the field.
Each row carried its checked pattern for every column, so a switch could
find the binding `|` patterns it tests, and swapping or specializing the
matrix rebuilt it to keep them in step. A row's pattern at a column now
comes from its root, the alternatives it took and the column's occurrence,
and the matrix operations are matchcov's again.
A `match` lowered an arm's body at every decision-tree leaf that reaches
it: once per `|` alternative, and once per path to a catch-all arm. An arm
reached from several leaves now has one body. Each leaf binds what depends
on its path and jumps to it, and the body binds what every leaf binds at
one path. An arm whose leaves bind an access at different parts keeps a
body per leaf, since an access has one definition.

With a four-alternative arm and a two-leaf catch-all arm, runtime code at
-O1 drops from 1259 to 743 bytes.
Pattern checking expanded every alternative of a `|`, so each `|` column
doubled the rows of the exhaustiveness matrix: `(a | a, ...)` over 22
columns took 1.7 GB in `fe check`, and `((a, _) | (_, a), ...)` over 16
took 480 MB. The decision tree expanded `(a, _) | (_, a)` the same way.

An alternative after one that matches anything (a wildcard, or the only
constructor of its type with such fields) matches nothing more. Leave such
alternatives out of the patterns matchcov sees and of the decision tree's
expansion. Thirty such columns now check in 0.1 s.

This branch has not been deployed

No deployments
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.

1 participant