Skip to content

Commit 1fe9ade

Browse files
committed
Pretty-print parenthesis around binary in postfix match
Signed-off-by: Sasha Pourcelot <[email protected]>
1 parent 7f2fc33 commit 1fe9ade

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,19 @@ impl<'a> State<'a> {
488488
self.space();
489489
}
490490
MatchKind::Postfix => {
491+
let need_par = expr.precedence().order() >= parser::PREC_RANGE
492+
&& expr.precedence().order() < parser::PREC_PREFIX;
493+
494+
if need_par {
495+
// avoid printing `a + b.match {}`, print
496+
// `(a + b).match {}` instead.
497+
self.popen();
498+
}
491499
self.print_expr_as_cond(expr);
500+
if need_par {
501+
self.pclose();
502+
}
503+
492504
self.word_nbsp(".match");
493505
}
494506
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
#![feature(postfix_match)]
4+
#[prelude_import]
5+
use ::std::prelude::rust_2015::*;
6+
#[macro_use]
7+
extern crate std;
8+
9+
//@ pretty-mode:expanded
10+
//@ pp-exact:precedence.pp
11+
12+
macro_rules! repro { ($e:expr) => { $e.match { _ => {} } }; }
13+
14+
pub fn main() {
15+
let a;
16+
(
17+
{ 1 } + 1).match {
18+
_ => {}
19+
};
20+
(4 as usize).match { _ => {} };
21+
(return).match { _ => {} };
22+
(a = 42).match { _ => {} };
23+
(|| {}).match { _ => {} };
24+
(42..101).match { _ => {} };
25+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(postfix_match)]
2+
3+
//@ pretty-mode:expanded
4+
//@ pp-exact:precedence.pp
5+
6+
macro_rules! repro {
7+
($e:expr) => {
8+
$e.match {
9+
_ => {}
10+
}
11+
};
12+
}
13+
14+
pub fn main() {
15+
let a;
16+
17+
repro!({ 1 } + 1);
18+
repro!(4 as usize);
19+
repro!(return);
20+
repro!(a = 42);
21+
repro!(|| {});
22+
repro!(42..101);
23+
}

0 commit comments

Comments
 (0)