From c8ff8a4dc7c9867c54df8f6d31120f4cd3923508 Mon Sep 17 00:00:00 2001 From: Sasha Pourcelot Date: Mon, 22 Apr 2024 18:48:41 +0200 Subject: [PATCH] Pretty-print parenthesis around binary in postfix match Signed-off-by: Sasha Pourcelot --- .../rustc_ast_pretty/src/pprust/state/expr.rs | 2 +- tests/pretty/postfix-match/precedence.pp | 34 +++++++++++++++++++ tests/pretty/postfix-match/precedence.rs | 34 +++++++++++++++++++ .../simple-matches.rs} | 0 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/pretty/postfix-match/precedence.pp create mode 100644 tests/pretty/postfix-match/precedence.rs rename tests/pretty/{postfix-match.rs => postfix-match/simple-matches.rs} (100%) diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index 4cbdc9f256dfe..b5bb781acdfe0 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -488,7 +488,7 @@ impl<'a> State<'a> { self.space(); } MatchKind::Postfix => { - self.print_expr_as_cond(expr); + self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup); self.word_nbsp(".match"); } } diff --git a/tests/pretty/postfix-match/precedence.pp b/tests/pretty/postfix-match/precedence.pp new file mode 100644 index 0000000000000..967aa7bc39ef3 --- /dev/null +++ b/tests/pretty/postfix-match/precedence.pp @@ -0,0 +1,34 @@ +#![feature(prelude_import)] +#![no_std] +#![feature(postfix_match)] +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; + +use std::ops::Add; + +//@ pretty-mode:expanded +//@ pp-exact:precedence.pp + +macro_rules! repro { ($e:expr) => { $e.match { _ => {} } }; } + +struct Struct {} + +impl Add for usize { + type Output = (); + fn add(self, _: Struct) -> () { () } +} +pub fn main() { + let a; + ( + { 1 } + 1).match { + _ => {} + }; + (4 as usize).match { _ => {} }; + (return).match { _ => {} }; + (a = 42).match { _ => {} }; + (|| {}).match { _ => {} }; + (42..101).match { _ => {} }; + (1 + Struct {}).match { _ => {} }; +} diff --git a/tests/pretty/postfix-match/precedence.rs b/tests/pretty/postfix-match/precedence.rs new file mode 100644 index 0000000000000..ee947e161ddcf --- /dev/null +++ b/tests/pretty/postfix-match/precedence.rs @@ -0,0 +1,34 @@ +#![feature(postfix_match)] + +use std::ops::Add; + +//@ pretty-mode:expanded +//@ pp-exact:precedence.pp + +macro_rules! repro { + ($e:expr) => { + $e.match { + _ => {} + } + }; +} + +struct Struct {} + +impl Add for usize { + type Output = (); + fn add(self, _: Struct) -> () { + () + } +} +pub fn main() { + let a; + + repro!({ 1 } + 1); + repro!(4 as usize); + repro!(return); + repro!(a = 42); + repro!(|| {}); + repro!(42..101); + repro!(1 + Struct {}); +} diff --git a/tests/pretty/postfix-match.rs b/tests/pretty/postfix-match/simple-matches.rs similarity index 100% rename from tests/pretty/postfix-match.rs rename to tests/pretty/postfix-match/simple-matches.rs