Skip to content

Show named lifetime in closure upvar diagnostics#153724

Open
arferreira wants to merge 1 commit intorust-lang:mainfrom
arferreira:fix-lifetime-naming-in-closures
Open

Show named lifetime in closure upvar diagnostics#153724
arferreira wants to merge 1 commit intorust-lang:mainfrom
arferreira:fix-lifetime-naming-in-closures

Conversation

@arferreira
Copy link
Contributor

@arferreira arferreira commented Mar 11, 2026

Fixes #153545.

When a closure captures a variable whose type involves a named lifetime from the parent function (e.g., 'a), the borrow checker's region renumbering creates a separate RegionVid for each occurrence of the erased lifetime in the closure's upvar type. These new RegionVids have no external_name, so diagnostics fall back to synthetic names like '1.

This PR recovers the original lifetime name by matching the closure's upvar type against the parent function's parameter type (obtained via liberate_late_bound_regions). Both types have the same structure but different region representations, the parent has ReLateParam/ReEarlyParam with real names, the closure has ReVar from renumbering. Walking them in parallel with for_each_free_region lets us find which named lifetime corresponds to the anonymous RegionVid.

Before:

error[E0716]: temporary value dropped while borrowed
 --> test.rs:11:21
  |
6 | fn apply<'a>(
  |          -- lifetime `'1` defined here

After:

error[E0716]: temporary value dropped while borrowed
 --> test.rs:11:21
  |
6 | fn apply<'a>(
  |          -- lifetime `'a` defined here

The fix gracefully falls back to the existing '1 naming when it can't match, nested closures, local variables (not parameters), destructuring patterns, or partial captures where the type structures diverge.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@arferreira arferreira force-pushed the fix-lifetime-naming-in-closures branch from d4a45ab to 99e1940 Compare March 11, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

E0716 without "let's call this" and spurious "requirement introduced here"

3 participants