SIGN IN SIGN UP

Rollup merge of #159232 - Rani367:fix-nested-hrtb-binder-printing, r=jackh726

Don't list escaping bound regions in nested `for<...>` binders of E0308 notes

The expected/found notes of "one type is more general than the other" errors could print types that are not even valid syntax, repeating lifetimes from an outer binder inside nested `for<...>` lists:

```
= note: expected mutable reference `&mut for<'a> fn(for<'a> fn(&'a ()))`
           found mutable reference `&mut fn(fn(&()))`
```

The expected type here is actually `&mut for<'a> fn(fn(&'a ()))`, the inner fn pointer binds nothing. In the example from rust-lang/rust#111365 the note printed `for<'o> fn(for<'a, 'o> fn(&'a (), &'o ()))` even though the inner binder only binds `'a`.

The cause is in how `cmp_fn_sig` builds its `for<...>` prefixes. It calls `name_all_regions` and joins every region of the returned map into the list. But `RegionFolder` folded every region at or above the depth of the binder being printed (`db >= self.current_index`), so regions that are bound by an enclosing binder and merely escape through the current one also ended up in the map. The pretty printer's own text output already skipped those regions via a special case in the naming closure (which is why the labels of these diagnostics were correct, see rust-lang/rust#102392), but the returned map kept them, and `cmp_fn_sig` recurses through nested fn pointers one binder at a time, so every nested `for<...>` list picked up all the outer lifetimes. That special case also rebound the escaping region to `self.current_index`, i.e. shifted its De Bruijn index down so that it looked like it was bound by the current binder.

This PR makes `RegionFolder` only fold regions bound by the binder being named (`db == self.current_index`). Escaping regions are left untouched: they keep their index, and they already carry a name because the enclosing binder named them when it was folded (`cmp_fn_sig` recurses on the folded value). This makes the rust-lang/rust#102392 special case in the naming closure dead, so it is removed along with the two `DebruijnIndex` parameters it needed.

With this change, the two examples above print:

```
= note: expected mutable reference `&mut for<'a> fn(fn(&'a ()))`
           found mutable reference `&mut fn(fn(&()))`
```

```
= note: expected fn pointer `for<'o> fn(for<'a> fn(&'a (), &'o ()))`
           found fn pointer `fn(for<'a> fn(&'a (), &'a ()))`
```

The `found` line of `tests/ui/nll/relate_tys/placeholder-outlives-existential.rs` also loses its leaked binders and now matches the type written in the test's source.

Fixes rust-lang/rust#134410

This also fixes the leaked outer lifetimes reported in rust-lang/rust#111365, but I left that issue open since it additionally asks for renaming of shadowed lifetimes (`for<'a> fn(for<'a> ...)` for two distinct lifetimes both named `'a` in the source), which is a separate problem.
J
Jonathan Brouwer committed
894a7f07a1dc438a98d6b09c0f0d0a7170683d26
Committed by GitHub <noreply@github.com> on 8/25/2026, 9:01:09 PM