Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 405c8af

Browse files
Rollup merge of #140280 - nnethercote:improve-if-else-printing, r=Urgau
Improve if/else pretty printing AST/HIR pretty printing of if/else is currently pretty bad. This PR improves it a lot. r? `@Nadrieril`
2 parents 4f7aed6 + 7ac2d1f commit 405c8af

File tree

9 files changed

+302
-78
lines changed

9 files changed

+302
-78
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<'a> State<'a> {
2121
match &_else.kind {
2222
// Another `else if` block.
2323
ast::ExprKind::If(i, then, e) => {
24-
self.cbox(INDENT_UNIT - 1);
24+
self.cbox(0);
2525
self.ibox(0);
2626
self.word(" else if ");
2727
self.print_expr_as_cond(i);
@@ -30,8 +30,8 @@ impl<'a> State<'a> {
3030
self.print_else(e.as_deref())
3131
}
3232
// Final `else` block.
33-
ast::ExprKind::Block(b, _) => {
34-
self.cbox(INDENT_UNIT - 1);
33+
ast::ExprKind::Block(b, None) => {
34+
self.cbox(0);
3535
self.ibox(0);
3636
self.word(" else ");
3737
self.print_block(b)
@@ -45,7 +45,9 @@ impl<'a> State<'a> {
4545
}
4646

4747
fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) {
48-
self.head("if");
48+
self.cbox(0);
49+
self.ibox(0);
50+
self.word_nbsp("if");
4951
self.print_expr_as_cond(test);
5052
self.space();
5153
self.print_block(blk);

‎compiler/rustc_hir_pretty/src/lib.rs‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,18 +1064,18 @@ impl<'a> State<'a> {
10641064
if let Some(els_inner) = els {
10651065
match els_inner.kind {
10661066
// Another `else if` block.
1067-
hir::ExprKind::If(i, then, e) => {
1068-
self.cbox(INDENT_UNIT - 1);
1067+
hir::ExprKind::If(i, hir::Expr{kind: hir::ExprKind::Block(t,None), .. }, e) => {
1068+
self.cbox(0);
10691069
self.ibox(0);
10701070
self.word(" else if ");
10711071
self.print_expr_as_cond(i);
10721072
self.space();
1073-
self.print_expr(then);
1073+
self.print_block(t);
10741074
self.print_else(e);
10751075
}
10761076
// Final `else` block.
1077-
hir::ExprKind::Block(b, _) => {
1078-
self.cbox(INDENT_UNIT - 1);
1077+
hir::ExprKind::Block(b, None) => {
1078+
self.cbox(0);
10791079
self.ibox(0);
10801080
self.word(" else ");
10811081
self.print_block(b);
@@ -1094,11 +1094,18 @@ impl<'a> State<'a> {
10941094
blk: &hir::Expr<'_>,
10951095
elseopt: Option<&hir::Expr<'_>>,
10961096
) {
1097-
self.head("if");
1097+
self.cbox(0);
1098+
self.ibox(0);
1099+
self.word_nbsp("if");
10981100
self.print_expr_as_cond(test);
10991101
self.space();
1100-
self.print_expr(blk);
1101-
self.print_else(elseopt)
1102+
match blk.kind {
1103+
hir::ExprKind::Block(blk, None) => {
1104+
self.print_block(blk);
1105+
self.print_else(elseopt)
1106+
}
1107+
_ => panic!("non-block then expr"),
1108+
}
11021109
}
11031110

11041111
fn print_anon_const(&mut self, constant: &hir::AnonConst) {

‎tests/pretty/hir-if-else.pp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
//@ pretty-compare-only
6+
//@ pretty-mode:hir
7+
//@ pp-exact:hir-if-else.pp
8+
9+
fn f(x: u32,
10+
y:
11+
u32) {
12+
let mut a = 0;
13+
if x > y { a = 1; } else { a = 2; }
14+
15+
if x < 1 {
16+
a = 1;
17+
} else if x < 2 {
18+
a = 2;
19+
} else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; }
20+
21+
if x < y {
22+
a += 1;
23+
a += 1;
24+
a += 1;
25+
a += 1;
26+
a += 1;
27+
a += 1;
28+
} else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; }
29+
30+
if x < 1 {
31+
if x < 2 {
32+
if x < 3 {
33+
a += 1;
34+
} else if x < 4 { a += 1; if x < 5 { a += 1; } }
35+
} else if x < 6 { a += 1; }
36+
}
37+
}
38+
39+
fn main() { f(3, 4); }

‎tests/pretty/hir-if-else.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//@ pretty-compare-only
2+
//@ pretty-mode:hir
3+
//@ pp-exact:hir-if-else.pp
4+
5+
fn f(x: u32, y: u32) {
6+
let mut a = 0;
7+
if x > y {
8+
a = 1;
9+
} else {
10+
a = 2;
11+
}
12+
13+
if x < 1 {
14+
a = 1;
15+
} else if x < 2 {
16+
a = 2;
17+
} else if x < 3 {
18+
a = 3;
19+
} else if x < 4 {
20+
a = 4;
21+
} else {
22+
a = 5;
23+
}
24+
25+
if x < y {
26+
a += 1;
27+
a += 1;
28+
a += 1;
29+
a += 1;
30+
a += 1;
31+
a += 1;
32+
} else {
33+
a += 1;
34+
a += 1;
35+
a += 1;
36+
a += 1;
37+
a += 1;
38+
a += 1;
39+
}
40+
41+
if x < 1 {
42+
if x < 2 {
43+
if x < 3 {
44+
a += 1;
45+
} else if x < 4 {
46+
a += 1;
47+
if x < 5 {
48+
a += 1;
49+
}
50+
}
51+
} else if x < 6 {
52+
a += 1;
53+
}
54+
}
55+
}
56+
57+
fn main() {
58+
f(3, 4);
59+
}

‎tests/pretty/if-else.pp‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
#[prelude_import]
4+
use ::std::prelude::rust_2015::*;
5+
#[macro_use]
6+
extern crate std;
7+
//@ pretty-compare-only
8+
//@ pretty-mode:expanded
9+
//@ pp-exact:if-else.pp
10+
11+
fn f(x: u32, y: u32) {
12+
let mut a = 0;
13+
if x > y { a = 1; } else { a = 2; }
14+
15+
if x < 1 {
16+
a = 1;
17+
} else if x < 2 {
18+
a = 2;
19+
} else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; }
20+
21+
if x < y {
22+
a += 1;
23+
a += 1;
24+
a += 1;
25+
} else {
26+
a += 1;
27+
a += 1;
28+
a += 1;
29+
a += 1;
30+
a += 1;
31+
a += 1;
32+
a += 1;
33+
a += 1;
34+
a += 1;
35+
a += 1;
36+
a += 1;
37+
a += 1;
38+
a += 1;
39+
a += 1;
40+
a += 1;
41+
}
42+
43+
if x < 1 {
44+
if x < 2 {
45+
if x < 3 {
46+
a += 1;
47+
} else if x < 4 { a += 1; if x < 5 { a += 1; } }
48+
} else if x < 6 { a += 1; }
49+
}
50+
}
51+
52+
fn main() { f(3, 4); }

‎tests/pretty/if-else.rs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//@ pretty-compare-only
2+
//@ pretty-mode:expanded
3+
//@ pp-exact:if-else.pp
4+
5+
fn f(x: u32, y: u32) {
6+
let mut a = 0;
7+
if x > y {
8+
a = 1;
9+
} else {
10+
a = 2;
11+
}
12+
13+
if x < 1 {
14+
a = 1;
15+
} else if x < 2 {
16+
a = 2;
17+
} else if x < 3 {
18+
a = 3;
19+
} else if x < 4 {
20+
a = 4;
21+
} else {
22+
a = 5;
23+
}
24+
25+
if x < y {
26+
a += 1;
27+
a += 1;
28+
a += 1;
29+
} else {
30+
a += 1;
31+
a += 1;
32+
a += 1;
33+
a += 1;
34+
a += 1;
35+
a += 1;
36+
a += 1;
37+
a += 1;
38+
a += 1;
39+
a += 1;
40+
a += 1;
41+
a += 1;
42+
a += 1;
43+
a += 1;
44+
a += 1;
45+
}
46+
47+
if x < 1 {
48+
if x < 2 {
49+
if x < 3 {
50+
a += 1;
51+
} else if x < 4 {
52+
a += 1;
53+
if x < 5 {
54+
a += 1;
55+
}
56+
}
57+
} else if x < 6 {
58+
a += 1;
59+
}
60+
}
61+
}
62+
63+
fn main() {
64+
f(3, 4);
65+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /