Skip to content

Commit 4bed9ec

Browse files
authored
Rollup merge of rust-lang#137423 - Urgau:imprv-pretty-hir, r=compiler-errors
Improve a bit HIR pretty printer This PR improve (a bit) the HIR pretty printer. It does so by: - Not printing elided lifetimes (those are not expressible in surface Rust anyway) - And by rendering implicit self with the shorthand syntax I also tried fixing some indentation and other things but gave up for now. Best reviewed commit by commit.
2 parents 553fde4 + d0e7bfd commit 4bed9ec

File tree

6 files changed

+152
-21
lines changed

6 files changed

+152
-21
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -424,20 +424,23 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
424424
self.ann_post(ident)
425425
}
426426

427-
fn strsep<T, F>(
427+
fn strsep<'x, T: 'x, F, I>(
428428
&mut self,
429429
sep: &'static str,
430430
space_before: bool,
431431
b: Breaks,
432-
elts: &[T],
432+
elts: I,
433433
mut op: F,
434434
) where
435435
F: FnMut(&mut Self, &T),
436+
I: IntoIterator<Item = &'x T>,
436437
{
438+
let mut it = elts.into_iter();
439+
437440
self.rbox(0, b);
438-
if let Some((first, rest)) = elts.split_first() {
441+
if let Some(first) = it.next() {
439442
op(self, first);
440-
for elt in rest {
443+
for elt in it {
441444
if space_before {
442445
self.space();
443446
}
@@ -448,9 +451,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
448451
self.end();
449452
}
450453

451-
fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], op: F)
454+
fn commasep<'x, T: 'x, F, I>(&mut self, b: Breaks, elts: I, op: F)
452455
where
453456
F: FnMut(&mut Self, &T),
457+
I: IntoIterator<Item = &'x T>,
454458
{
455459
self.strsep(",", false, b, elts, op)
456460
}

compiler/rustc_hir_pretty/src/lib.rs

+60-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use rustc_ast_pretty::pprust::state::MacHeader;
1818
use rustc_ast_pretty::pprust::{Comments, PrintState};
1919
use rustc_hir::{
2020
BindingMode, ByRef, ConstArgKind, GenericArg, GenericBound, GenericParam, GenericParamKind,
21-
HirId, LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term, TyPatKind,
21+
HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term,
22+
TyPatKind,
2223
};
2324
use rustc_span::source_map::SourceMap;
2425
use rustc_span::{FileName, Ident, Span, Symbol, kw};
@@ -2086,6 +2087,28 @@ impl<'a> State<'a> {
20862087
self.print_pat(arg.pat);
20872088
}
20882089

2090+
fn print_implicit_self(&mut self, implicit_self_kind: &hir::ImplicitSelfKind) {
2091+
match implicit_self_kind {
2092+
ImplicitSelfKind::Imm => {
2093+
self.word("self");
2094+
}
2095+
ImplicitSelfKind::Mut => {
2096+
self.print_mutability(hir::Mutability::Mut, false);
2097+
self.word("self");
2098+
}
2099+
ImplicitSelfKind::RefImm => {
2100+
self.word("&");
2101+
self.word("self");
2102+
}
2103+
ImplicitSelfKind::RefMut => {
2104+
self.word("&");
2105+
self.print_mutability(hir::Mutability::Mut, false);
2106+
self.word("self");
2107+
}
2108+
ImplicitSelfKind::None => unreachable!(),
2109+
}
2110+
}
2111+
20892112
fn print_arm(&mut self, arm: &hir::Arm<'_>) {
20902113
// I have no idea why this check is necessary, but here it
20912114
// is :(
@@ -2151,27 +2174,33 @@ impl<'a> State<'a> {
21512174
// Make sure we aren't supplied *both* `arg_names` and `body_id`.
21522175
assert!(arg_names.is_empty() || body_id.is_none());
21532176
let mut i = 0;
2154-
let mut print_arg = |s: &mut Self| {
2155-
if let Some(arg_name) = arg_names.get(i) {
2156-
s.word(arg_name.to_string());
2157-
s.word(":");
2158-
s.space();
2159-
} else if let Some(body_id) = body_id {
2160-
s.ann.nested(s, Nested::BodyParamPat(body_id, i));
2161-
s.word(":");
2162-
s.space();
2177+
let mut print_arg = |s: &mut Self, ty: Option<&hir::Ty<'_>>| {
2178+
if i == 0 && decl.implicit_self.has_implicit_self() {
2179+
s.print_implicit_self(&decl.implicit_self);
2180+
} else {
2181+
if let Some(arg_name) = arg_names.get(i) {
2182+
s.word(arg_name.to_string());
2183+
s.word(":");
2184+
s.space();
2185+
} else if let Some(body_id) = body_id {
2186+
s.ann.nested(s, Nested::BodyParamPat(body_id, i));
2187+
s.word(":");
2188+
s.space();
2189+
}
2190+
if let Some(ty) = ty {
2191+
s.print_type(ty);
2192+
}
21632193
}
21642194
i += 1;
21652195
};
21662196
self.commasep(Inconsistent, decl.inputs, |s, ty| {
21672197
s.ibox(INDENT_UNIT);
2168-
print_arg(s);
2169-
s.print_type(ty);
2198+
print_arg(s, Some(ty));
21702199
s.end();
21712200
});
21722201
if decl.c_variadic {
21732202
self.word(", ");
2174-
print_arg(self);
2203+
print_arg(self, None);
21752204
self.word("...");
21762205
}
21772206
self.pclose();
@@ -2284,7 +2313,9 @@ impl<'a> State<'a> {
22842313
GenericBound::Use(args, _) => {
22852314
self.word("use <");
22862315

2287-
self.commasep(Inconsistent, args, |s, arg| s.print_precise_capturing_arg(*arg));
2316+
self.commasep(Inconsistent, *args, |s, arg| {
2317+
s.print_precise_capturing_arg(*arg)
2318+
});
22882319

22892320
self.word(">");
22902321
}
@@ -2300,10 +2331,23 @@ impl<'a> State<'a> {
23002331
}
23012332

23022333
fn print_generic_params(&mut self, generic_params: &[GenericParam<'_>]) {
2303-
if !generic_params.is_empty() {
2334+
let is_lifetime_elided = |generic_param: &GenericParam<'_>| {
2335+
matches!(
2336+
generic_param.kind,
2337+
GenericParamKind::Lifetime { kind: LifetimeParamKind::Elided(_) }
2338+
)
2339+
};
2340+
2341+
// We don't want to show elided lifetimes as they are compiler-inserted and not
2342+
// expressible in surface level Rust.
2343+
if !generic_params.is_empty() && !generic_params.iter().all(is_lifetime_elided) {
23042344
self.word("<");
23052345

2306-
self.commasep(Inconsistent, generic_params, |s, param| s.print_generic_param(param));
2346+
self.commasep(
2347+
Inconsistent,
2348+
generic_params.iter().filter(|gp| !is_lifetime_elided(gp)),
2349+
|s, param| s.print_generic_param(param),
2350+
);
23072351

23082352
self.word(">");
23092353
}

tests/ui/unpretty/debug-fmt-hir.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ compile-flags: -Zunpretty=hir
2+
//@ check-pass
3+
4+
use std::fmt;
5+
6+
pub struct Bar {
7+
a: String,
8+
b: u8,
9+
}
10+
11+
impl fmt::Debug for Bar {
12+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13+
debug_struct_field2_finish(f, "Bar", "a", &self.a, "b", &&self.b)
14+
}
15+
}
16+
17+
fn debug_struct_field2_finish<'a>(
18+
name: &str,
19+
name1: &str,
20+
value1: &'a dyn fmt::Debug,
21+
name2: &str,
22+
value2: &'a dyn fmt::Debug,
23+
) -> fmt::Result
24+
{
25+
loop {}
26+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
//@ compile-flags: -Zunpretty=hir
6+
//@ check-pass
7+
8+
use std::fmt;
9+
10+
struct Bar {
11+
a: String,
12+
b: u8,
13+
}
14+
15+
impl fmt::Debug for Bar {
16+
fn fmt(&self, f: &'_ mut fmt::Formatter<'_>)
17+
->
18+
fmt::Result {
19+
debug_struct_field2_finish(f, "Bar", "a", &self.a, "b", &&self.b)
20+
}
21+
}
22+
23+
fn debug_struct_field2_finish<'a>(name: &'_ str, name1: &'_ str,
24+
value1: &'a dyn fmt::Debug, name2: &'_ str, value2: &'a dyn fmt::Debug)
25+
-> fmt::Result { loop { } }

tests/ui/unpretty/self-hir.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: -Zunpretty=hir
2+
//@ check-pass
3+
4+
pub struct Bar {
5+
a: String,
6+
b: u8,
7+
}
8+
9+
impl Bar {
10+
fn imm_self(self) {}
11+
fn mut_self(mut self) {}
12+
fn refimm_self(&self) {}
13+
fn refmut_self(&mut self) {}
14+
}

tests/ui/unpretty/self-hir.stdout

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
//@ compile-flags: -Zunpretty=hir
6+
//@ check-pass
7+
8+
struct Bar {
9+
a: String,
10+
b: u8,
11+
}
12+
13+
impl Bar {
14+
fn imm_self(self) { }
15+
fn mut_self(mut self) { }
16+
fn refimm_self(&self) { }
17+
fn refmut_self(&mut self) { }
18+
}

0 commit comments

Comments
 (0)