Skip to content

Commit 9a31dfb

Browse files
committed
Filter elided lifetimes in HIR pretty printing
1 parent 56a8502 commit 9a31dfb

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
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

+18-3
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,9 @@ impl<'a> State<'a> {
22842284
GenericBound::Use(args, _) => {
22852285
self.word("use <");
22862286

2287-
self.commasep(Inconsistent, args, |s, arg| s.print_precise_capturing_arg(*arg));
2287+
self.commasep(Inconsistent, *args, |s, arg| {
2288+
s.print_precise_capturing_arg(*arg)
2289+
});
22882290

22892291
self.word(">");
22902292
}
@@ -2300,10 +2302,23 @@ impl<'a> State<'a> {
23002302
}
23012303

23022304
fn print_generic_params(&mut self, generic_params: &[GenericParam<'_>]) {
2303-
if !generic_params.is_empty() {
2305+
let is_lifetime_elided = |generic_param: &GenericParam<'_>| {
2306+
matches!(
2307+
generic_param.kind,
2308+
GenericParamKind::Lifetime { kind: LifetimeParamKind::Elided(_) }
2309+
)
2310+
};
2311+
2312+
// We don't want to show elided lifetimes as they are compiler-inserted and unexpreseble
2313+
// in surface levlter them out and make sure to not.
2314+
if !generic_params.is_empty() && !generic_params.iter().all(is_lifetime_elided) {
23042315
self.word("<");
23052316

2306-
self.commasep(Inconsistent, generic_params, |s, param| s.print_generic_param(param));
2317+
self.commasep(
2318+
Inconsistent,
2319+
generic_params.iter().filter(|gp| !is_lifetime_elided(gp)),
2320+
|s, param| s.print_generic_param(param),
2321+
);
23072322

23082323
self.word(">");
23092324
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ struct Bar {
1313
}
1414

1515
impl fmt::Debug for Bar {
16-
fn fmt<'_, '_, '_>(self: &'_ Self, f: &'_ mut fmt::Formatter<'_>)
16+
fn fmt(self: &'_ Self, f: &'_ mut fmt::Formatter<'_>)
1717
->
1818
fmt::Result {
1919
debug_struct_field2_finish(f, "Bar", "a", &self.a, "b", &&self.b)
2020
}
2121
}
2222

23-
fn debug_struct_field2_finish<'a, '_, '_,
24-
'_>(name: &'_ str, name1: &'_ str, value1: &'a dyn fmt::Debug,
25-
name2: &'_ str, value2: &'a dyn fmt::Debug) -> fmt::Result { loop { } }
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 { } }

0 commit comments

Comments
 (0)