Show named lifetime in closure upvar diagnostics#153724
Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom Mar 16, 2026
Merged
Show named lifetime in closure upvar diagnostics#153724rust-bors[bot] merged 1 commit intorust-lang:mainfrom
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
Collaborator
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
d4a45ab to
99e1940
Compare
jieyouxu
approved these changes
Mar 16, 2026
Member
There was a problem hiding this comment.
Thanks, I think the change looks reasonable. There might be some false-matches, but the current diff is better than previous anyway.
@bors r+ rollup
Member
There was a problem hiding this comment.
Meta suggestion: for future PRs that does diagnostic changes like these, it's particularly helpful for the reviewer to evaluate the diagnostic difference if you can structure the PR into 2 commits:
- Commit 1 adds the test with a blessed stderr that reflects the current diagnostics (i.e. before your changes)
- Commit 2 contains your change and the diff in diagnostic output (i.e. after your change)
Member
There was a problem hiding this comment.
FWIW, the diff is
diff --git b/tests/ui/borrowck/closure-upvar-named-lifetime.stderr a/tests/ui/borrowck/closure-upvar-named-lifetime.stderr
index 5c2e17ef177..fc7c59fa97c 100644
--- b/tests/ui/borrowck/closure-upvar-named-lifetime.stderr
+++ a/tests/ui/borrowck/closure-upvar-named-lifetime.stderr
@@ -1,8 +1,8 @@
error[E0597]: `map` does not live long enough
--> $DIR/closure-upvar-named-lifetime.rs:19:21
|
-LL | f: Arc<dyn Fn(Entry<'a, String, String>) + 'a>,
- | - lifetime `'1` appears in the type of `f`
+LL | fn apply<'a>(
+ | -- lifetime `'a` defined here
...
LL | move |map| {
| --- binding `map` declared here
@@ -11,18 +11,18 @@ LL | let value = map.borrow_mut().entry("foo".to_string());
| ^^^ borrowed value does not live long enough
...
LL | f(wrapped_value);
- | ---------------- argument requires that `map` is borrowed for `'1`
+ | ---------------- argument requires that `map` is borrowed for `'a`
LL | }
| - `map` dropped here while still borrowed
|
-note: requirement that the value outlives `'1` introduced here
+note: requirement that the value outlives `'a` introduced here
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
error[E0716]: temporary value dropped while borrowed
--> $DIR/closure-upvar-named-lifetime.rs:19:21
|
-LL | f: Arc<dyn Fn(Entry<'a, String, String>) + 'a>,
- | - lifetime `'1` appears in the type of `f`
+LL | fn apply<'a>(
+ | -- lifetime `'a` defined here
...
LL | let value = map.borrow_mut().entry("foo".to_string());
| ^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
@@ -30,9 +30,9 @@ LL | let value = map.borrow_mut().entry("foo".to_string());
| creates a temporary value which is freed while still in use
...
LL | f(wrapped_value);
- | ---------------- argument requires that borrow lasts for `'1`
+ | ---------------- argument requires that borrow lasts for `'a`
|
-note: requirement that the value outlives `'1` introduced here
+note: requirement that the value outlives `'a` introduced here
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
error[E0700]: hidden type for `impl Fn(RefCell<HashMap<String, String>>)` captures lifetime that does not appear in bounds
@@ -60,8 +60,8 @@ LL | ) -> impl Fn(RefCell<HashMap<String, String>>) + use<'a>
error[E0597]: `map` does not live long enough
--> $DIR/closure-upvar-named-lifetime.rs:34:21
|
-LL | f: Box<dyn Fn(Entry<'a, String, String>) + 'a>,
- | - lifetime `'1` appears in the type of `f`
+LL | fn apply_box<'a>(
+ | -- lifetime `'a` defined here
...
LL | move |map| {
| --- binding `map` declared here
@@ -70,15 +70,15 @@ LL | let value = map.borrow_mut().entry("foo".to_string());
| ^^^ borrowed value does not live long enough
...
LL | f(value);
- | -------- argument requires that `map` is borrowed for `'1`
+ | -------- argument requires that `map` is borrowed for `'a`
LL | }
| - `map` dropped here while still borrowed
error[E0716]: temporary value dropped while borrowed
--> $DIR/closure-upvar-named-lifetime.rs:34:21
|
-LL | f: Box<dyn Fn(Entry<'a, String, String>) + 'a>,
- | - lifetime `'1` appears in the type of `f`
+LL | fn apply_box<'a>(
+ | -- lifetime `'a` defined here
...
LL | let value = map.borrow_mut().entry("foo".to_string());
| ^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
@@ -86,7 +86,7 @@ LL | let value = map.borrow_mut().entry("foo".to_string());
| creates a temporary value which is freed while still in use
...
LL | f(value);
- | -------- argument requires that borrow lasts for `'1`
+ | -------- argument requires that borrow lasts for `'a`
error[E0700]: hidden type for `impl Fn(RefCell<HashMap<String, String>>)` captures lifetime that does not appear in bounds
--> $DIR/closure-upvar-named-lifetime.rs:32:5
Contributor
rust-bors bot
pushed a commit
that referenced
this pull request
Mar 16, 2026
Rollup of 8 pull requests Successful merges: - #153639 (Remove `QueryInfo`.) - #153570 (MaybeUninit: mention common mistakes in assume_init docs; link to validity invariant docs) - #153724 (Show named lifetime in closure upvar diagnostics) - #153793 (Add overview documentation for `std::mem`.) - #153922 (rustc_mir_build only depends on rustc_lint_defs, not rustc_lint) - #153923 (run_make_support: Print the command and output even if it succeeds) - #153925 (Provide better suggestions for inference errors on `.collect()?`) - #153928 (remove several redundant tests)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 separateRegionVidfor each occurrence of the erased lifetime in the closure's upvar type. These newRegionVids have noexternal_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 hasReLateParam/ReEarlyParamwith real names, the closure hasReVarfrom renumbering. Walking them in parallel withfor_each_free_regionlets us find which named lifetime corresponds to the anonymousRegionVid.Before:
After:
The fix gracefully falls back to the existing
'1naming when it can't match, nested closures, local variables (not parameters), destructuring patterns, or partial captures where the type structures diverge.