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 7ac2d1f

Browse files
Improve HIR pretty-printing of if/else some more.
In the AST the "then" block is represented as a `Block`. In HIR the "then" block is represented as an `Expr` that happens to always be. `ExprKind::Block`. By deconstructing the `ExprKind::Block` to extract the block within, things print properly. For `issue-82392.rs`, note that we no longer print a type after the "then" block. This is good, it now matches how we don't print a type for the "else" block. (Well, we do print a type after the "else" block, but it's for the whole if/else.) Also tighten up some of the pattern matching -- these block expressions within if/else will never have labels.
1 parent e37c367 commit 7ac2d1f

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a> State<'a> {
3030
self.print_else(e.as_deref())
3131
}
3232
// Final `else` block.
33-
ast::ExprKind::Block(b, _) => {
33+
ast::ExprKind::Block(b, None) => {
3434
self.cbox(0);
3535
self.ibox(0);
3636
self.word(" else ");

‎compiler/rustc_hir_pretty/src/lib.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,17 +1064,17 @@ 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) => {
1067+
hir::ExprKind::If(i, hir::Expr{kind: hir::ExprKind::Block(t,None), .. }, e) => {
10681068
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, _) => {
1077+
hir::ExprKind::Block(b, None) => {
10781078
self.cbox(0);
10791079
self.ibox(0);
10801080
self.word(" else ");
@@ -1099,8 +1099,13 @@ impl<'a> State<'a> {
10991099
self.word_nbsp("if");
11001100
self.print_expr_as_cond(test);
11011101
self.space();
1102-
self.print_expr(blk);
1103-
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+
}
11041109
}
11051110

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

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,28 @@
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+
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; }
2520

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; }
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; }
3529

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-
}
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+
}
4738

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

‎tests/ui/match/issue-82392.stdout‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ extern crate std;
88
//@ edition:2015
99

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

0 commit comments

Comments
(0)

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