Skip to content

Commit e37c367

Browse files
committed
Improve pretty printing of if/else.
By removing some of the over-indenting. AST pretty printing now looks correct. HIR pretty printing is better, but still over-indents some.
1 parent ee43aa3 commit e37c367

File tree

7 files changed

+136
-132
lines changed

7 files changed

+136
-132
lines changed

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

+5-3
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);
24+
self.cbox(0);
2525
self.ibox(0);
2626
self.word(" else if ");
2727
self.print_expr_as_cond(i);
@@ -31,7 +31,7 @@ impl<'a> State<'a> {
3131
}
3232
// Final `else` block.
3333
ast::ExprKind::Block(b, _) => {
34-
self.cbox(INDENT_UNIT);
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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ impl<'a> State<'a> {
10651065
match els_inner.kind {
10661066
// Another `else if` block.
10671067
hir::ExprKind::If(i, then, e) => {
1068-
self.cbox(INDENT_UNIT);
1068+
self.cbox(0);
10691069
self.ibox(0);
10701070
self.word(" else if ");
10711071
self.print_expr_as_cond(i);
@@ -1075,7 +1075,7 @@ impl<'a> State<'a> {
10751075
}
10761076
// Final `else` block.
10771077
hir::ExprKind::Block(b, _) => {
1078-
self.cbox(INDENT_UNIT);
1078+
self.cbox(0);
10791079
self.ibox(0);
10801080
self.word(" else ");
10811081
self.print_block(b);
@@ -1094,7 +1094,9 @@ 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();
11001102
self.print_expr(blk);

tests/pretty/hir-if-else.pp

+31-31
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@
1212
let mut a = 0;
1313
if x > y { a = 1; } else { a = 2; }
1414

15-
if x < 1
16-
{
17-
a = 1;
18-
} else if x < 2
19-
{
20-
a = 2;
21-
} else if x < 3
22-
{
23-
a = 3;
24-
} else if x < 4 { a = 4; } else { a = 5; }
15+
if x < 1
16+
{
17+
a = 1;
18+
} else if x < 2
19+
{
20+
a = 2;
21+
} else if x < 3
22+
{
23+
a = 3;
24+
} else if x < 4 { a = 4; } else { a = 5; }
2525

26-
if x < y
27-
{
28-
a += 1;
29-
a += 1;
30-
a += 1;
31-
a += 1;
32-
a += 1;
33-
a += 1;
34-
} else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; }
26+
if x < y
27+
{
28+
a += 1;
29+
a += 1;
30+
a += 1;
31+
a += 1;
32+
a += 1;
33+
a += 1;
34+
} else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; }
3535

36-
if x < 1
37-
{
38-
if x < 2
39-
{
40-
if x < 3
41-
{
42-
a += 1;
43-
} else if x < 4
44-
{ a += 1; if x < 5 { a += 1; } }
45-
} else if x < 6 { a += 1; } }
46-
}
36+
if x < 1
37+
{
38+
if x < 2
39+
{
40+
if x < 3
41+
{
42+
a += 1;
43+
} else if x < 4
44+
{ a += 1; if x < 5 { a += 1; } }
45+
} else if x < 6 { a += 1; } }
46+
}
4747

48-
fn main() { f(3, 4); }
48+
fn main() { f(3, 4); }

tests/pretty/if-else.pp

+30-30
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,40 @@
1313
if x > y { a = 1; } else { a = 2; }
1414

1515
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; }
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; }
2020

2121
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-
}
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+
}
4242

4343
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-
}
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+
}
5050
}
5151

5252
fn main() { f(3, 4); }

tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout

+47-47
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ fn arbitrary_consuming_method_for_demonstration_purposes() {
1818
let mut __capture0 = ::core::asserting::Capture::new();
1919
let __local_bind0 = &elem;
2020
if ::core::intrinsics::unlikely(!(*{
21-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
22-
__local_bind0
23-
} as usize)) {
21+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
22+
__local_bind0
23+
} as usize)) {
2424

2525

2626

2727

28-
{
29-
::std::rt::panic_fmt(format_args!("Assertion failed: elem as usize\nWith captures:\n elem = {0:?}\n",
30-
__capture0));
31-
}
28+
{
29+
::std::rt::panic_fmt(format_args!("Assertion failed: elem as usize\nWith captures:\n elem = {0:?}\n",
30+
__capture0));
3231
}
32+
}
3333
};
3434
}
3535
fn addr_of() {
@@ -40,12 +40,12 @@ fn addr_of() {
4040
let mut __capture0 = ::core::asserting::Capture::new();
4141
let __local_bind0 = &elem;
4242
if ::core::intrinsics::unlikely(!&*__local_bind0) {
43-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
44-
{
45-
::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n",
46-
__capture0));
47-
}
43+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
44+
{
45+
::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n",
46+
__capture0));
4847
}
48+
}
4949
};
5050
}
5151
fn binary() {
@@ -56,77 +56,77 @@ fn binary() {
5656
let mut __capture0 = ::core::asserting::Capture::new();
5757
let __local_bind0 = &elem;
5858
if ::core::intrinsics::unlikely(!(*__local_bind0 == 1)) {
59-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
60-
{
61-
::std::rt::panic_fmt(format_args!("Assertion failed: elem == 1\nWith captures:\n elem = {0:?}\n",
62-
__capture0));
63-
}
59+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
60+
{
61+
::std::rt::panic_fmt(format_args!("Assertion failed: elem == 1\nWith captures:\n elem = {0:?}\n",
62+
__capture0));
6463
}
64+
}
6565
};
6666
{
6767
#[allow(unused_imports)]
6868
use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
6969
let mut __capture0 = ::core::asserting::Capture::new();
7070
let __local_bind0 = &elem;
7171
if ::core::intrinsics::unlikely(!(*__local_bind0 >= 1)) {
72-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
73-
{
74-
::std::rt::panic_fmt(format_args!("Assertion failed: elem >= 1\nWith captures:\n elem = {0:?}\n",
75-
__capture0));
76-
}
72+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
73+
{
74+
::std::rt::panic_fmt(format_args!("Assertion failed: elem >= 1\nWith captures:\n elem = {0:?}\n",
75+
__capture0));
7776
}
77+
}
7878
};
7979
{
8080
#[allow(unused_imports)]
8181
use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
8282
let mut __capture0 = ::core::asserting::Capture::new();
8383
let __local_bind0 = &elem;
8484
if ::core::intrinsics::unlikely(!(*__local_bind0 > 0)) {
85-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
86-
{
87-
::std::rt::panic_fmt(format_args!("Assertion failed: elem > 0\nWith captures:\n elem = {0:?}\n",
88-
__capture0));
89-
}
85+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
86+
{
87+
::std::rt::panic_fmt(format_args!("Assertion failed: elem > 0\nWith captures:\n elem = {0:?}\n",
88+
__capture0));
9089
}
90+
}
9191
};
9292
{
9393
#[allow(unused_imports)]
9494
use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
9595
let mut __capture0 = ::core::asserting::Capture::new();
9696
let __local_bind0 = &elem;
9797
if ::core::intrinsics::unlikely(!(*__local_bind0 < 3)) {
98-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
99-
{
100-
::std::rt::panic_fmt(format_args!("Assertion failed: elem < 3\nWith captures:\n elem = {0:?}\n",
101-
__capture0));
102-
}
98+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
99+
{
100+
::std::rt::panic_fmt(format_args!("Assertion failed: elem < 3\nWith captures:\n elem = {0:?}\n",
101+
__capture0));
103102
}
103+
}
104104
};
105105
{
106106
#[allow(unused_imports)]
107107
use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
108108
let mut __capture0 = ::core::asserting::Capture::new();
109109
let __local_bind0 = &elem;
110110
if ::core::intrinsics::unlikely(!(*__local_bind0 <= 3)) {
111-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
112-
{
113-
::std::rt::panic_fmt(format_args!("Assertion failed: elem <= 3\nWith captures:\n elem = {0:?}\n",
114-
__capture0));
115-
}
111+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
112+
{
113+
::std::rt::panic_fmt(format_args!("Assertion failed: elem <= 3\nWith captures:\n elem = {0:?}\n",
114+
__capture0));
116115
}
116+
}
117117
};
118118
{
119119
#[allow(unused_imports)]
120120
use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
121121
let mut __capture0 = ::core::asserting::Capture::new();
122122
let __local_bind0 = &elem;
123123
if ::core::intrinsics::unlikely(!(*__local_bind0 != 3)) {
124-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
125-
{
126-
::std::rt::panic_fmt(format_args!("Assertion failed: elem != 3\nWith captures:\n elem = {0:?}\n",
127-
__capture0));
128-
}
124+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
125+
{
126+
::std::rt::panic_fmt(format_args!("Assertion failed: elem != 3\nWith captures:\n elem = {0:?}\n",
127+
__capture0));
129128
}
129+
}
130130
};
131131
}
132132
fn unary() {
@@ -137,12 +137,12 @@ fn unary() {
137137
let mut __capture0 = ::core::asserting::Capture::new();
138138
let __local_bind0 = &elem;
139139
if ::core::intrinsics::unlikely(!**__local_bind0) {
140-
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
141-
{
142-
::std::rt::panic_fmt(format_args!("Assertion failed: *elem\nWith captures:\n elem = {0:?}\n",
143-
__capture0));
144-
}
140+
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
141+
{
142+
::std::rt::panic_fmt(format_args!("Assertion failed: *elem\nWith captures:\n elem = {0:?}\n",
143+
__capture0));
145144
}
145+
}
146146
};
147147
}
148148
fn main() {}

tests/ui/match/issue-82392.stdout

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ extern crate std;
99

1010
fn main() ({
1111
(if (true as bool)
12-
({ } as
13-
()) else if (let Some(a) =
14-
((Some as
15-
fn(i32) -> Option<i32> {Option::<i32>::Some})((3 as i32)) as
16-
Option<i32>) as bool) ({ } as ()) as ())
17-
} as ())
12+
({ } as
13+
()) else if (let Some(a) =
14+
((Some as
15+
fn(i32) -> Option<i32> {Option::<i32>::Some})((3 as i32)) as
16+
Option<i32>) as bool) ({ } as ()) as ())
17+
} as ())

0 commit comments

Comments
 (0)