Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,23 @@ impl<'a> State<'a> {
}
}

/// Print a pattern, parenthesizing it if it is an or-pattern (`A | B`).
///
/// Or-patterns have the lowest precedence among patterns, so they need
/// parentheses when nested inside `@` bindings, `&` references, or `box`
/// patterns — otherwise `x @ A | B` parses as `(x @ A) | B`, `&A | B`
/// parses as `(&A) | B`, etc.
fn print_pat_paren_if_or(&mut self, pat: &ast::Pat) {
let needs_paren = matches!(pat.kind, PatKind::Or(..));
if needs_paren {
self.popen();
}
self.print_pat(pat);
if needs_paren {
self.pclose();
}
}

fn print_pat(&mut self, pat: &ast::Pat) {
self.maybe_print_comment(pat.span.lo());
self.ann.pre(self, AnnNode::Pat(pat));
Expand Down Expand Up @@ -1776,7 +1793,7 @@ impl<'a> State<'a> {
if let Some(p) = sub {
self.space();
self.word_space("@");
self.print_pat(p);
self.print_pat_paren_if_or(p);
}
}
PatKind::TupleStruct(qself, path, elts) => {
Expand Down Expand Up @@ -1848,7 +1865,7 @@ impl<'a> State<'a> {
}
PatKind::Box(inner) => {
self.word("box ");
self.print_pat(inner);
self.print_pat_paren_if_or(inner);
}
PatKind::Deref(inner) => {
self.word("deref!");
Expand All @@ -1872,7 +1889,7 @@ impl<'a> State<'a> {
self.print_pat(inner);
self.pclose();
} else {
self.print_pat(inner);
self.print_pat_paren_if_or(inner);
}
}
PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/unnested_or_patterns2.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ fn main() {
//~^ unnested_or_patterns
if let box (0 | 1 | 2 | 3 | 4) = Box::new(0) {}
//~^ unnested_or_patterns
if let box box (0 | 2 | 4) = Box::new(Box::new(0)) {}
if let box (box (0 | 2 | 4)) = Box::new(Box::new(0)) {}
//~^ unnested_or_patterns
}
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/unnested_or_patterns2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ LL | if let box box 0 | box (box 2 | box 4) = Box::new(Box::new(0)) {}
help: nest the patterns
|
LL - if let box box 0 | box (box 2 | box 4) = Box::new(Box::new(0)) {}
LL + if let box box (0 | 2 | 4) = Box::new(Box::new(0)) {}
LL + if let box (box (0 | 2 | 4)) = Box::new(Box::new(0)) {}
|

error: aborting due to 8 previous errors
Expand Down
27 changes: 27 additions & 0 deletions tests/pretty/or-pattern-paren.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![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)); }
36 changes: 36 additions & 0 deletions tests/pretty/or-pattern-paren.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![feature(box_patterns)]

//@ 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 @ or_pat!(1, 2, 3)) => println!("{v}"),
_ => {}
}
}

fn check_ref(x: &i32) {
match x {
&or_pat!(1, 2, 3) => {}
_ => {}
}
}

fn check_box(x: Box<i32>) {
match x {
box or_pat!(1, 2, 3) => {}
_ => {}
}
}

fn main() {
check_at(Some(2));
check_ref(&1);
check_box(Box::new(1));
}
Loading