From 3d488f8e0c953f10852431f4c195e6b24cc6eec7 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 25 Apr 2025 12:25:04 +1000 Subject: [PATCH 1/4] Add pretty printing tests for if/else. The AST pretty printing is a bit wonky. The HIR pretty printing is extremely wonky. --- tests/pretty/hir-if-else.pp | 48 +++++++++++++++++++++++++++ tests/pretty/hir-if-else.rs | 59 +++++++++++++++++++++++++++++++++ tests/pretty/if-else.pp | 52 +++++++++++++++++++++++++++++ tests/pretty/if-else.rs | 65 +++++++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 tests/pretty/hir-if-else.pp create mode 100644 tests/pretty/hir-if-else.rs create mode 100644 tests/pretty/if-else.pp create mode 100644 tests/pretty/if-else.rs diff --git a/tests/pretty/hir-if-else.pp b/tests/pretty/hir-if-else.pp new file mode 100644 index 0000000000000..551f24c690057 --- /dev/null +++ b/tests/pretty/hir-if-else.pp @@ -0,0 +1,48 @@ +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +//@ pretty-compare-only +//@ pretty-mode:hir +//@ pp-exact:hir-if-else.pp + +fn f(x: u32, + y: + u32) { + let mut a = 0; + if x > y { a = 1; } else { a = 2; } + + if x < 1 + { + a = 1; + } else if x < 2 + { + a = 2; + } else if x < 3 + { + a = 3; + } else if x < 4 { a = 4; } else { a = 5; } + + if x < y + { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; } + + if x < 1 + { + if x < 2 + { + if x < 3 + { + a += 1; + } else if x < 4 + { a += 1; if x < 5 { a += 1; } } + } else if x < 6 { a += 1; } } + } + + fn main() { f(3, 4); } diff --git a/tests/pretty/hir-if-else.rs b/tests/pretty/hir-if-else.rs new file mode 100644 index 0000000000000..a1cc7504f89a2 --- /dev/null +++ b/tests/pretty/hir-if-else.rs @@ -0,0 +1,59 @@ +//@ pretty-compare-only +//@ pretty-mode:hir +//@ pp-exact:hir-if-else.pp + +fn f(x: u32, y: u32) { + let mut a = 0; + if x > y { + a = 1; + } else { + a = 2; + } + + if x < 1 { + a = 1; + } else if x < 2 { + a = 2; + } else if x < 3 { + a = 3; + } else if x < 4 { + a = 4; + } else { + a = 5; + } + + if x < y { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } else { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } + + if x < 1 { + if x < 2 { + if x < 3 { + a += 1; + } else if x < 4 { + a += 1; + if x < 5 { + a += 1; + } + } + } else if x < 6 { + a += 1; + } + } +} + +fn main() { + f(3, 4); +} diff --git a/tests/pretty/if-else.pp b/tests/pretty/if-else.pp new file mode 100644 index 0000000000000..c70a78ade42f8 --- /dev/null +++ b/tests/pretty/if-else.pp @@ -0,0 +1,52 @@ +#![feature(prelude_import)] +#![no_std] +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +//@ pretty-compare-only +//@ pretty-mode:expanded +//@ pp-exact:if-else.pp + +fn f(x: u32, y: u32) { + let mut a = 0; + if x > y { a = 1; } else { a = 2; } + + if x < 1 { + a = 1; + } else if x < 2 { + a = 2; + } else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; } + + if x < y { + a += 1; + a += 1; + a += 1; + } else { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } + + if x < 1 { + if x < 2 { + if x < 3 { + a += 1; + } else if x < 4 { a += 1; if x < 5 { a += 1; } } + } else if x < 6 { a += 1; } + } +} + +fn main() { f(3, 4); } diff --git a/tests/pretty/if-else.rs b/tests/pretty/if-else.rs new file mode 100644 index 0000000000000..b4085ea560677 --- /dev/null +++ b/tests/pretty/if-else.rs @@ -0,0 +1,65 @@ +//@ pretty-compare-only +//@ pretty-mode:expanded +//@ pp-exact:if-else.pp + +fn f(x: u32, y: u32) { + let mut a = 0; + if x > y { + a = 1; + } else { + a = 2; + } + + if x < 1 { + a = 1; + } else if x < 2 { + a = 2; + } else if x < 3 { + a = 3; + } else if x < 4 { + a = 4; + } else { + a = 5; + } + + if x < y { + a += 1; + a += 1; + a += 1; + } else { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } + + if x < 1 { + if x < 2 { + if x < 3 { + a += 1; + } else if x < 4 { + a += 1; + if x < 5 { + a += 1; + } + } + } else if x < 6 { + a += 1; + } + } +} + +fn main() { + f(3, 4); +} From ee43aa356aa228b5cc8da41622855f4f2c07f8ab Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 25 Apr 2025 13:06:03 +1000 Subject: [PATCH 2/4] Fix some pretty printing indents. Indents for `cbox` and `ibox` are 0 or `INDENT_UNIT` (4) except for a couple of places which are `INDENT_UNIT - 1` for no clear reason. This commit changes the three space indents to four spaces. --- .../rustc_ast_pretty/src/pprust/state/expr.rs | 4 +- compiler/rustc_hir_pretty/src/lib.rs | 4 +- tests/pretty/hir-if-else.pp | 54 +++++++++---------- tests/pretty/if-else.pp | 36 ++++++------- tests/ui/match/issue-82392.stdout | 8 +-- tests/ui/proc-macro/quote/debug.stdout | 8 +-- 6 files changed, 57 insertions(+), 57 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index df848a26d390a..37df567081fa2 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -21,7 +21,7 @@ impl<'a> State<'a> { match &_else.kind { // Another `else if` block. ast::ExprKind::If(i, then, e) => { - self.cbox(INDENT_UNIT - 1); + self.cbox(INDENT_UNIT); self.ibox(0); self.word(" else if "); self.print_expr_as_cond(i); @@ -31,7 +31,7 @@ impl<'a> State<'a> { } // Final `else` block. ast::ExprKind::Block(b, _) => { - self.cbox(INDENT_UNIT - 1); + self.cbox(INDENT_UNIT); self.ibox(0); self.word(" else "); self.print_block(b) diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index ff4385c3bccec..65b008dfc957c 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1065,7 +1065,7 @@ impl<'a> State<'a> { match els_inner.kind { // Another `else if` block. hir::ExprKind::If(i, then, e) => { - self.cbox(INDENT_UNIT - 1); + self.cbox(INDENT_UNIT); self.ibox(0); self.word(" else if "); self.print_expr_as_cond(i); @@ -1075,7 +1075,7 @@ impl<'a> State<'a> { } // Final `else` block. hir::ExprKind::Block(b, _) => { - self.cbox(INDENT_UNIT - 1); + self.cbox(INDENT_UNIT); self.ibox(0); self.word(" else "); self.print_block(b); diff --git a/tests/pretty/hir-if-else.pp b/tests/pretty/hir-if-else.pp index 551f24c690057..072e050d3e078 100644 --- a/tests/pretty/hir-if-else.pp +++ b/tests/pretty/hir-if-else.pp @@ -16,33 +16,33 @@ { a = 1; } else if x < 2 - { - a = 2; - } else if x < 3 - { - a = 3; - } else if x < 4 { a = 4; } else { a = 5; } + { + a = 2; + } else if x < 3 + { + a = 3; + } else if x < 4 { a = 4; } else { a = 5; } - if x < y - { - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - } else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; } + if x < y + { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; } - if x < 1 - { - if x < 2 - { - if x < 3 - { - a += 1; - } else if x < 4 - { a += 1; if x < 5 { a += 1; } } - } else if x < 6 { a += 1; } } - } + if x < 1 + { + if x < 2 + { + if x < 3 + { + a += 1; + } else if x < 4 + { a += 1; if x < 5 { a += 1; } } + } else if x < 6 { a += 1; } } + } - fn main() { f(3, 4); } + fn main() { f(3, 4); } diff --git a/tests/pretty/if-else.pp b/tests/pretty/if-else.pp index c70a78ade42f8..14c715a8793de 100644 --- a/tests/pretty/if-else.pp +++ b/tests/pretty/if-else.pp @@ -15,30 +15,30 @@ if x < 1 { a = 1; } else if x < 2 { - a = 2; - } else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; } + a = 2; + } else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; } if x < y { a += 1; a += 1; a += 1; } else { - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - } + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } if x < 1 { if x < 2 { diff --git a/tests/ui/match/issue-82392.stdout b/tests/ui/match/issue-82392.stdout index 8b7edabf004ed..34d2c7aa2ba57 100644 --- a/tests/ui/match/issue-82392.stdout +++ b/tests/ui/match/issue-82392.stdout @@ -11,7 +11,7 @@ fn main() ({ (if (true as bool) ({ } as ()) else if (let Some(a) = - ((Some as - fn(i32) -> Option {Option::::Some})((3 as i32)) as - Option) as bool) ({ } as ()) as ()) - } as ()) + ((Some as + fn(i32) -> Option {Option::::Some})((3 as i32)) as + Option) as bool) ({ } as ()) as ()) + } as ()) diff --git a/tests/ui/proc-macro/quote/debug.stdout b/tests/ui/proc-macro/quote/debug.stdout index 6ebb3a3795114..b2a9f031b289a 100644 --- a/tests/ui/proc-macro/quote/debug.stdout +++ b/tests/ui/proc-macro/quote/debug.stdout @@ -36,8 +36,8 @@ fn main() { lit.set_span(crate::Span::recover_proc_macro_span(2)); lit } else { - ::core::panicking::panic("internal error: entered unreachable code") - } + ::core::panicking::panic("internal error: entered unreachable code") + } }), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';', crate::Spacing::Alone)), &mut ts); @@ -55,8 +55,8 @@ fn main() { lit.set_span(crate::Span::recover_proc_macro_span(5)); lit } else { - ::core::panicking::panic("internal error: entered unreachable code") - } + ::core::panicking::panic("internal error: entered unreachable code") + } }), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';', crate::Spacing::Alone)), &mut ts); From e37c3674828a75ffdc2b94d9d273e9c02a9ee11f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 25 Apr 2025 13:51:49 +1000 Subject: [PATCH 3/4] 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. --- .../rustc_ast_pretty/src/pprust/state/expr.rs | 8 +- compiler/rustc_hir_pretty/src/lib.rs | 8 +- tests/pretty/hir-if-else.pp | 62 ++++++------ tests/pretty/if-else.pp | 60 ++++++------ ...ming-methods-have-optimized-codegen.stdout | 94 +++++++++---------- tests/ui/match/issue-82392.stdout | 12 +-- tests/ui/proc-macro/quote/debug.stdout | 24 ++--- 7 files changed, 136 insertions(+), 132 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index 37df567081fa2..f2a9a688d8b85 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -21,7 +21,7 @@ impl<'a> State<'a> { match &_else.kind { // Another `else if` block. ast::ExprKind::If(i, then, e) => { - self.cbox(INDENT_UNIT); + self.cbox(0); self.ibox(0); self.word(" else if "); self.print_expr_as_cond(i); @@ -31,7 +31,7 @@ impl<'a> State<'a> { } // Final `else` block. ast::ExprKind::Block(b, _) => { - self.cbox(INDENT_UNIT); + self.cbox(0); self.ibox(0); self.word(" else "); self.print_block(b) @@ -45,7 +45,9 @@ impl<'a> State<'a> { } fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) { - self.head("if"); + self.cbox(0); + self.ibox(0); + self.word_nbsp("if"); self.print_expr_as_cond(test); self.space(); self.print_block(blk); diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 65b008dfc957c..3e4d4f82322c9 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1065,7 +1065,7 @@ impl<'a> State<'a> { match els_inner.kind { // Another `else if` block. hir::ExprKind::If(i, then, e) => { - self.cbox(INDENT_UNIT); + self.cbox(0); self.ibox(0); self.word(" else if "); self.print_expr_as_cond(i); @@ -1075,7 +1075,7 @@ impl<'a> State<'a> { } // Final `else` block. hir::ExprKind::Block(b, _) => { - self.cbox(INDENT_UNIT); + self.cbox(0); self.ibox(0); self.word(" else "); self.print_block(b); @@ -1094,7 +1094,9 @@ impl<'a> State<'a> { blk: &hir::Expr<'_>, elseopt: Option<&hir::Expr<'_>>, ) { - self.head("if"); + self.cbox(0); + self.ibox(0); + self.word_nbsp("if"); self.print_expr_as_cond(test); self.space(); self.print_expr(blk); diff --git a/tests/pretty/hir-if-else.pp b/tests/pretty/hir-if-else.pp index 072e050d3e078..6626bf9cd28bd 100644 --- a/tests/pretty/hir-if-else.pp +++ b/tests/pretty/hir-if-else.pp @@ -12,37 +12,37 @@ let mut a = 0; if x > y { a = 1; } else { a = 2; } - if x < 1 - { - a = 1; - } else if x < 2 - { - a = 2; - } else if x < 3 - { - a = 3; - } else if x < 4 { a = 4; } else { a = 5; } + if x < 1 + { + a = 1; + } else if x < 2 + { + a = 2; + } else if x < 3 + { + a = 3; + } else if x < 4 { a = 4; } else { a = 5; } - if x < y - { - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - } else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; } + if x < y + { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; } - if x < 1 - { - if x < 2 - { - if x < 3 - { - a += 1; - } else if x < 4 - { a += 1; if x < 5 { a += 1; } } - } else if x < 6 { a += 1; } } - } + if x < 1 + { + if x < 2 + { + if x < 3 + { + a += 1; + } else if x < 4 + { a += 1; if x < 5 { a += 1; } } + } else if x < 6 { a += 1; } } + } - fn main() { f(3, 4); } + fn main() { f(3, 4); } diff --git a/tests/pretty/if-else.pp b/tests/pretty/if-else.pp index 14c715a8793de..d4ff02c544159 100644 --- a/tests/pretty/if-else.pp +++ b/tests/pretty/if-else.pp @@ -13,40 +13,40 @@ if x > y { a = 1; } else { a = 2; } if x < 1 { - a = 1; - } else if x < 2 { - a = 2; - } else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; } + a = 1; + } else if x < 2 { + a = 2; + } else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; } if x < y { - a += 1; - a += 1; - a += 1; - } else { - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - } + a += 1; + a += 1; + a += 1; + } else { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } if x < 1 { - if x < 2 { - if x < 3 { - a += 1; - } else if x < 4 { a += 1; if x < 5 { a += 1; } } - } else if x < 6 { a += 1; } - } + if x < 2 { + if x < 3 { + a += 1; + } else if x < 4 { a += 1; if x < 5 { a += 1; } } + } else if x < 6 { a += 1; } + } } fn main() { f(3, 4); } diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout b/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout index 9300f610f8e20..33193c78334c9 100644 --- a/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout +++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout @@ -18,18 +18,18 @@ fn arbitrary_consuming_method_for_demonstration_purposes() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!(*{ - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - __local_bind0 - } as usize)) { + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + __local_bind0 + } as usize)) { - { - ::std::rt::panic_fmt(format_args!("Assertion failed: elem as usize\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + { + ::std::rt::panic_fmt(format_args!("Assertion failed: elem as usize\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; } fn addr_of() { @@ -40,12 +40,12 @@ fn addr_of() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!&*__local_bind0) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; } fn binary() { @@ -56,12 +56,12 @@ fn binary() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!(*__local_bind0 == 1)) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: elem == 1\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: elem == 1\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; { #[allow(unused_imports)] @@ -69,12 +69,12 @@ fn binary() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!(*__local_bind0 >= 1)) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: elem >= 1\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: elem >= 1\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; { #[allow(unused_imports)] @@ -82,12 +82,12 @@ fn binary() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!(*__local_bind0 > 0)) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: elem > 0\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: elem > 0\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; { #[allow(unused_imports)] @@ -95,12 +95,12 @@ fn binary() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!(*__local_bind0 < 3)) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: elem < 3\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: elem < 3\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; { #[allow(unused_imports)] @@ -108,12 +108,12 @@ fn binary() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!(*__local_bind0 <= 3)) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: elem <= 3\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: elem <= 3\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; { #[allow(unused_imports)] @@ -121,12 +121,12 @@ fn binary() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!(*__local_bind0 != 3)) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: elem != 3\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: elem != 3\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; } fn unary() { @@ -137,12 +137,12 @@ fn unary() { let mut __capture0 = ::core::asserting::Capture::new(); let __local_bind0 = &elem; if ::core::intrinsics::unlikely(!**__local_bind0) { - (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); - { - ::std::rt::panic_fmt(format_args!("Assertion failed: *elem\nWith captures:\n elem = {0:?}\n", - __capture0)); - } + (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0); + { + ::std::rt::panic_fmt(format_args!("Assertion failed: *elem\nWith captures:\n elem = {0:?}\n", + __capture0)); } + } }; } fn main() {} diff --git a/tests/ui/match/issue-82392.stdout b/tests/ui/match/issue-82392.stdout index 34d2c7aa2ba57..6a68bbdfd5685 100644 --- a/tests/ui/match/issue-82392.stdout +++ b/tests/ui/match/issue-82392.stdout @@ -9,9 +9,9 @@ extern crate std; fn main() ({ (if (true as bool) - ({ } as - ()) else if (let Some(a) = - ((Some as - fn(i32) -> Option {Option::::Some})((3 as i32)) as - Option) as bool) ({ } as ()) as ()) - } as ()) + ({ } as + ()) else if (let Some(a) = + ((Some as + fn(i32) -> Option {Option::::Some})((3 as i32)) as + Option) as bool) ({ } as ()) as ()) + } as ()) diff --git a/tests/ui/proc-macro/quote/debug.stdout b/tests/ui/proc-macro/quote/debug.stdout index b2a9f031b289a..3acb472d9c0fb 100644 --- a/tests/ui/proc-macro/quote/debug.stdout +++ b/tests/ui/proc-macro/quote/debug.stdout @@ -32,12 +32,12 @@ fn main() { let mut iter = "\"world\"".parse::().unwrap().into_iter(); if let (Some(crate::TokenTree::Literal(mut lit)), None) = - (iter.next(), iter.next()) { - lit.set_span(crate::Span::recover_proc_macro_span(2)); - lit - } else { - ::core::panicking::panic("internal error: entered unreachable code") - } + (iter.next(), iter.next()) { + lit.set_span(crate::Span::recover_proc_macro_span(2)); + lit + } else { + ::core::panicking::panic("internal error: entered unreachable code") + } }), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';', crate::Spacing::Alone)), &mut ts); @@ -51,12 +51,12 @@ fn main() { let mut iter = "r#\"raw\"literal\"#".parse::().unwrap().into_iter(); if let (Some(crate::TokenTree::Literal(mut lit)), None) = - (iter.next(), iter.next()) { - lit.set_span(crate::Span::recover_proc_macro_span(5)); - lit - } else { - ::core::panicking::panic("internal error: entered unreachable code") - } + (iter.next(), iter.next()) { + lit.set_span(crate::Span::recover_proc_macro_span(5)); + lit + } else { + ::core::panicking::panic("internal error: entered unreachable code") + } }), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';', crate::Spacing::Alone)), &mut ts); From 7ac2d1f1bdb2348b13eb4f5fb9c7d620e316034f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 25 Apr 2025 19:12:15 +1000 Subject: [PATCH 4/4] 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. --- .../rustc_ast_pretty/src/pprust/state/expr.rs | 2 +- compiler/rustc_hir_pretty/src/lib.rs | 15 ++++-- tests/pretty/hir-if-else.pp | 53 ++++++++----------- tests/ui/match/issue-82392.stdout | 8 +-- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index f2a9a688d8b85..9546ffafb1904 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -30,7 +30,7 @@ impl<'a> State<'a> { self.print_else(e.as_deref()) } // Final `else` block. - ast::ExprKind::Block(b, _) => { + ast::ExprKind::Block(b, None) => { self.cbox(0); self.ibox(0); self.word(" else "); diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 3e4d4f82322c9..4608531f1d904 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1064,17 +1064,17 @@ impl<'a> State<'a> { if let Some(els_inner) = els { match els_inner.kind { // Another `else if` block. - hir::ExprKind::If(i, then, e) => { + hir::ExprKind::If(i, hir::Expr { kind: hir::ExprKind::Block(t, None), .. }, e) => { self.cbox(0); self.ibox(0); self.word(" else if "); self.print_expr_as_cond(i); self.space(); - self.print_expr(then); + self.print_block(t); self.print_else(e); } // Final `else` block. - hir::ExprKind::Block(b, _) => { + hir::ExprKind::Block(b, None) => { self.cbox(0); self.ibox(0); self.word(" else "); @@ -1099,8 +1099,13 @@ impl<'a> State<'a> { self.word_nbsp("if"); self.print_expr_as_cond(test); self.space(); - self.print_expr(blk); - self.print_else(elseopt) + match blk.kind { + hir::ExprKind::Block(blk, None) => { + self.print_block(blk); + self.print_else(elseopt) + } + _ => panic!("non-block then expr"), + } } fn print_anon_const(&mut self, constant: &hir::AnonConst) { diff --git a/tests/pretty/hir-if-else.pp b/tests/pretty/hir-if-else.pp index 6626bf9cd28bd..200e34ac4f5a4 100644 --- a/tests/pretty/hir-if-else.pp +++ b/tests/pretty/hir-if-else.pp @@ -12,37 +12,28 @@ let mut a = 0; if x > y { a = 1; } else { a = 2; } - if x < 1 - { - a = 1; - } else if x < 2 - { - a = 2; - } else if x < 3 - { - a = 3; - } else if x < 4 { a = 4; } else { a = 5; } + if x < 1 { + a = 1; + } else if x < 2 { + a = 2; + } else if x < 3 { a = 3; } else if x < 4 { a = 4; } else { a = 5; } - if x < y - { - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - a += 1; - } else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; } + if x < y { + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + a += 1; + } else { a += 1; a += 1; a += 1; a += 1; a += 1; a += 1; } - if x < 1 - { - if x < 2 - { - if x < 3 - { - a += 1; - } else if x < 4 - { a += 1; if x < 5 { a += 1; } } - } else if x < 6 { a += 1; } } - } + if x < 1 { + if x < 2 { + if x < 3 { + a += 1; + } else if x < 4 { a += 1; if x < 5 { a += 1; } } + } else if x < 6 { a += 1; } + } +} - fn main() { f(3, 4); } +fn main() { f(3, 4); } diff --git a/tests/ui/match/issue-82392.stdout b/tests/ui/match/issue-82392.stdout index 6a68bbdfd5685..a0d83d962e73e 100644 --- a/tests/ui/match/issue-82392.stdout +++ b/tests/ui/match/issue-82392.stdout @@ -8,10 +8,10 @@ extern crate std; //@ edition:2015 fn main() ({ - (if (true as bool) - ({ } as - ()) else if (let Some(a) = + (if (true as bool) { + } else if (let Some(a) = ((Some as fn(i32) -> Option {Option::::Some})((3 as i32)) as - Option) as bool) ({ } as ()) as ()) + Option) as bool) { } as ()) +} as ())