Repository: rust
Issue: Diagnostics: Nested higher-ranked lifetimes are incorrectly printed
Labels: A-pretty,A-diagnostics,A-lifetimes,T-compiler,D-incorrect,A-higher-ranked

For

```rs
type One = fn(HelperOne);
type HelperOne = for<'a> fn(&'a (), &'a ());

type Two = for<'o> fn(HelperTwo<'o>);
type HelperTwo<'x> = for<'a> fn(&'a (), &'x ());

fn accept(x: One) -> Two { x }
```

the output is

```
error[E0308]: mismatched types
 --> src/lib.rs:7:28
  |
7 | fn accept(x: One) -> Two { x }
  |                      ---   ^ one type is more general than the other
  |                      |
  |                      expected `for<'o> fn(for<'a> fn(&'a (), &'o ()))` because of return type
  |
  = note: expected fn pointer `for<'o> fn(for<'o, 'a> fn(&'a (), &'o ()))`
             found fn pointer `fn(for<'a> fn(&'a (), &'a ()))`
```

Here the type `for<'o> fn(for<'o, 'a> fn(&'a (), &'o ()))` shown after “*expected fn pointer*” contains
`for<'o, 'a>` which doesn't make sense. The binder contains the lifetime bound in the outer binder.
The inner binder should just be `for<'a>` just like in the type shown in the label.
Note that this doesn't happen when `-Zverbose` is passed [^1].

Further, if we change the definition of `Two` to be `type Two = for<'a> fn(HelperTwo<'a>); ` (`'o` => `'a`),
then the pretty-printer does not try to avoid **shadowing** through **renaming** of one of the lifetimes or
through other means. Output:

```
error[E0308]: mismatched types
 --> src/lib.rs:7:28
  |
7 | fn accept(x: One) -> Two { x }
  |                      ---   ^ one type is more general than the other
  |                      |
  |                      expected `for<'a> fn(for<'a> fn(&'a (), &'a ()))` because of return type
  |
  = note: expected fn pointer `for<'a> fn(for<'a, 'a> fn(&'a (), &'a ()))`
             found fn pointer `fn(for<'a> fn(&'a (), &'a ()))`
```

Ideally it should look something like

```diff
  error[E0308]: mismatched types
   --> src/lib.rs:7:28
    |
  7 | fn accept(x: One) -> Two { x }
    |                      ---   ^ one type is more general than the other
    |                      |
-   |                      expected `for<'a> fn(for<'a> fn(&'a (), &'a ()))` because of return type
+   |                      expected `for<'r> fn(for<'a> fn(&'a (), &'r ()))` because of return type
    |
-   = note: expected fn pointer `for<'a> fn(for<'a, 'a> fn(&'a (), &'a ()))`
+   = note: expected fn pointer `for<'r> fn(for<'a> fn(&'a (), &'r ()))`
               found fn pointer `fn(for<'a> fn(&'a (), &'a ()))`

```

[Here is a playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=1b9d721bd39c8594d211b4e38fc160c6) for a smaller reproducer of the last issue. In this case, the compiler could either
perform renaming or it could omit the inner binder entirely since the lifetime it binds is not referenced
in the inner type.

I've looked through all relevant <kbd>A-diagnostics</kbd> <kbd>A-lifetimes</kbd> issues and through some others and couldn't find anything that matches 100% what I was looking for (out of those maybe kinda #29061, #56423, #73457, #92281 but not really).

@rustbot label T-compiler A-diagnostics A-lifetimes D-incorrect

[^1]: It's `for<Region(BrNamed(DefId(0:7 ~ repro[8603]::Two::'o), 'o))> fn(for<Region(BrNamed(DefId(0:10 ~ repro[8603]::HelperTwo::'a), 'a))> fn(..., ...))`.