Skip to content

Parenthesize or-patterns in prefix pattern positions in pretty printer#154085

Merged
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
aytey:fix-at-or-pattern-parens
Mar 23, 2026
Merged

Parenthesize or-patterns in prefix pattern positions in pretty printer#154085
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
aytey:fix-at-or-pattern-parens

Conversation

@aytey
Copy link
Contributor

@aytey aytey commented Mar 19, 2026

The AST pretty printer was dropping parentheses around or-patterns when they appeared inside @ bindings, & references, or box patterns. For example:

  • v @ (1 | 2 | 3) was printed as v @ 1 | 2 | 3
  • &(1 | 2 | 3) was printed as &1 | 2 | 3
  • box (1 | 2 | 3) was printed as box 1 | 2 | 3

Since | has the lowest precedence among pattern operators, all of these are parsed incorrectly without parentheses — e.g. v @ 1 | 2 | 3 becomes (v @ 1) | 2 | 3, binding v only to the first alternative.

This caused E0408 ("variable not bound in all patterns") when the expanded output was fed back to the compiler, affecting crates like html5ever and wgpu-core that use macros expanding to or-patterns after @.

The fix adds a print_pat_paren_if_or helper that wraps PatKind::Or subpatterns in parentheses, and uses it in the @, &, and box printing arms. This is similar in spirit to the existing FixupContext parenthesization approach used for expression printing.

Example

before (rustc 1.96.0-nightly (3b1b0ef4d 2026-03-11)):

#![feature(prelude_import)]
#![no_std]
#![feature(box_patterns)]
extern crate std;
#[prelude_import]
use ::std::prelude::rust_2015::*;

//@ pretty-compare-only
//@ pretty-mode:expanded
//@ pp-exact:or-pattern-paren.pp

macro_rules! or_pat { ($($name:pat),+) => { $($name)|+ } }

fn check_at(x: Option<i32>) {
    match x {
        Some(v @ 1 | 2 | 3) =>


            {
            ::std::io::_print(format_args!("{0}\n", v));
        }
        _ => {}
    }
}
fn check_ref(x: &i32) { match x { &1 | 2 | 3 => {} _ => {} } }
fn check_box(x: Box<i32>) { match x { box 1 | 2 | 3 => {} _ => {} } }
fn main() { check_at(Some(2)); check_ref(&1); check_box(Box::new(1)); }

after (this branch):

#![feature(prelude_import)]
#![no_std]
#![feature(box_patterns)]
extern crate std;
#[prelude_import]
use ::std::prelude::rust_2015::*;

//@ pretty-compare-only
//@ pretty-mode:expanded
//@ pp-exact:or-pattern-paren.pp

macro_rules! or_pat { ($($name:pat),+) => { $($name)|+ } }

fn check_at(x: Option<i32>) {
    match x {
        Some(v @ (1 | 2 | 3)) =>


            {
            ::std::io::_print(format_args!("{0}\n", v));
        }
        _ => {}
    }
}
fn check_ref(x: &i32) { match x { &(1 | 2 | 3) => {} _ => {} } }
fn check_box(x: Box<i32>) { match x { box (1 | 2 | 3) => {} _ => {} } }
fn main() { check_at(Some(2)); check_ref(&1); check_box(Box::new(1)); }

Notice v @ 1 | 2 | 3 becomes v @ (1 | 2 | 3), &1 | 2 | 3 becomes &(1 | 2 | 3), and box 1 | 2 | 3 becomes box (1 | 2 | 3). Without parens, the or-pattern binds incorrectly — only the first alternative gets the @/&/box, causing E0408.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 19, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 19, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 15 candidates

The AST pretty printer was dropping parentheses around or-patterns
when they appeared inside `@` bindings, `&` references, or `box`
patterns. For example:

- `v @ (1 | 2 | 3)` was printed as `v @ 1 | 2 | 3`
- `&(1 | 2 | 3)` was printed as `&1 | 2 | 3`
- `box (1 | 2 | 3)` was printed as `box 1 | 2 | 3`

Since `|` has the lowest precedence among pattern operators, all of
these are parsed incorrectly without parentheses — e.g. `v @ 1 | 2 | 3`
becomes `(v @ 1) | 2 | 3`, binding `v` only to the first alternative.

This caused E0408 ("variable not bound in all patterns") when the
expanded output was fed back to the compiler, affecting crates like
html5ever and wgpu-core that use macros expanding to or-patterns
after `@`.

The fix adds a `print_pat_paren_if_or` helper that wraps `PatKind::Or`
subpatterns in parentheses, and uses it in the `@`, `&`, and `box`
printing arms. This is similar in spirit to the existing `FixupContext`
parenthesization approach used for expression printing.

Signed-off-by: Andrew V. Teylu <andrew.teylu@vector.com>
@aytey aytey force-pushed the fix-at-or-pattern-parens branch from d880685 to e15897f Compare March 19, 2026 10:35
@rust-log-analyzer

This comment has been minimized.

The pretty printer now correctly parenthesizes or-patterns inside
`box` patterns, which changes the Clippy `unnested_or_patterns` lint
suggestion from `box box (0 | 2 | 4)` to `box (box (0 | 2 | 4))`.
The new output is more correct — the old suggestion was itself missing
parens and would have been parsed as `box (box 0) | 2 | 4`.

Signed-off-by: Andrew V. Teylu <andrew.teylu@vector.com>
@rustbot
Copy link
Collaborator

rustbot commented Mar 19, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Mar 19, 2026
@chenyukang
Copy link
Member

@bors r=chenyukang

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 23, 2026

📌 Commit 1a1a0d2 has been approved by chenyukang

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 23, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 23, 2026
…chenyukang

Parenthesize or-patterns in prefix pattern positions in pretty printer

The AST pretty printer was dropping parentheses around or-patterns when they appeared inside `@` bindings, `&` references, or `box` patterns. For example:

- `v @ (1 | 2 | 3)` was printed as `v @ 1 | 2 | 3`
- `&(1 | 2 | 3)` was printed as `&1 | 2 | 3`
- `box (1 | 2 | 3)` was printed as `box 1 | 2 | 3`

Since `|` has the lowest precedence among pattern operators, all of these are parsed incorrectly without parentheses — e.g. `v @ 1 | 2 | 3` becomes `(v @ 1) | 2 | 3`, binding `v` only to the first alternative.

This caused E0408 ("variable not bound in all patterns") when the expanded output was fed back to the compiler, affecting crates like html5ever and wgpu-core that use macros expanding to or-patterns after `@`.

The fix adds a `print_pat_paren_if_or` helper that wraps `PatKind::Or` subpatterns in parentheses, and uses it in the `@`, `&`, and `box` printing arms. This is similar in spirit to the existing `FixupContext` parenthesization approach used for expression printing.

## Example

**before** (`rustc 1.96.0-nightly (3b1b0ef 2026-03-11)`):

```rust
#![feature(prelude_import)]
#![no_std]
#![feature(box_patterns)]
extern crate std;
#[prelude_import]
use ::std::prelude::rust_2015::*;

//@ pretty-compare-only
//@ pretty-mode:expanded
//@ pp-exact:or-pattern-paren.pp

macro_rules! or_pat { ($($name:pat),+) => { $($name)|+ } }

fn check_at(x: Option<i32>) {
    match x {
        Some(v @ 1 | 2 | 3) =>

            {
            ::std::io::_print(format_args!("{0}\n", v));
        }
        _ => {}
    }
}
fn check_ref(x: &i32) { match x { &1 | 2 | 3 => {} _ => {} } }
fn check_box(x: Box<i32>) { match x { box 1 | 2 | 3 => {} _ => {} } }
fn main() { check_at(Some(2)); check_ref(&1); check_box(Box::new(1)); }
```

**after** (this branch):

```rust
#![feature(prelude_import)]
#![no_std]
#![feature(box_patterns)]
extern crate std;
#[prelude_import]
use ::std::prelude::rust_2015::*;

//@ pretty-compare-only
//@ pretty-mode:expanded
//@ pp-exact:or-pattern-paren.pp

macro_rules! or_pat { ($($name:pat),+) => { $($name)|+ } }

fn check_at(x: Option<i32>) {
    match x {
        Some(v @ (1 | 2 | 3)) =>

            {
            ::std::io::_print(format_args!("{0}\n", v));
        }
        _ => {}
    }
}
fn check_ref(x: &i32) { match x { &(1 | 2 | 3) => {} _ => {} } }
fn check_box(x: Box<i32>) { match x { box (1 | 2 | 3) => {} _ => {} } }
fn main() { check_at(Some(2)); check_ref(&1); check_box(Box::new(1)); }
```

Notice `v @ 1 | 2 | 3` becomes `v @ (1 | 2 | 3)`, `&1 | 2 | 3` becomes `&(1 | 2 | 3)`, and `box 1 | 2 | 3` becomes `box (1 | 2 | 3)`. Without parens, the or-pattern binds incorrectly — only the first alternative gets the `@`/`&`/`box`, causing E0408.
rust-bors bot pushed a commit that referenced this pull request Mar 23, 2026
…uwer

Rollup of 13 pull requests

Successful merges:

 - #154241 (`rust-analyzer` subtree update)
 - #153686 (`std`: include `dlmalloc` for all non-wasi Wasm targets)
 - #154105 (bootstrap: Pass `--features=rustc` to rustc_transmute)
 - #153069 ([BPF] add target feature allows-misaligned-mem-access)
 - #154085 (Parenthesize or-patterns in prefix pattern positions in pretty printer)
 - #154191 (refactor RangeFromIter overflow-checks impl)
 - #154207 (Refactor query loading)
 - #153540 (drop derive helpers during attribute parsing)
 - #154140 (Document consteval behavior of ub_checks, overflow_checks, is_val_statically_known.)
 - #154161 (On E0277 tweak help when single type impls traits)
 - #154218 (interpret/validity: remove unreachable error kind)
 - #154225 (diagnostics: avoid ICE in confusable_method_name for associated functions)
 - #154228 (Improve inline assembly error messages)
@rust-bors rust-bors bot merged commit 9e25329 into rust-lang:main Mar 23, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants