-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Show named lifetime in closure upvar diagnostics #153724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
arferreira:fix-lifetime-naming-in-closures
Mar 16, 2026
+277
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Regression test for #153545. | ||
| // | ||
| // When a closure captures a variable whose type involves a named | ||
| // lifetime from the parent function, diagnostics should use the | ||
| // actual lifetime name (e.g., `'a`) instead of a synthetic name | ||
| // like `'1`. | ||
|
|
||
| use std::cell::RefCell; | ||
| use std::sync::Arc; | ||
| use std::collections::HashMap; | ||
| use std::collections::hash_map::Entry; | ||
|
|
||
| fn apply<'a>( | ||
| f: Arc<dyn Fn(Entry<'a, String, String>) + 'a>, | ||
| ) -> impl Fn(RefCell<HashMap<String, String>>) | ||
| { | ||
| move |map| { | ||
| //~^ ERROR hidden type for `impl Fn(RefCell<HashMap<String, String>>)` captures lifetime that does not appear in bounds | ||
| let value = map.borrow_mut().entry("foo".to_string()); | ||
| //~^ ERROR `map` does not live long enough | ||
| //~| ERROR temporary value dropped while borrowed | ||
| let wrapped_value = value; | ||
| f(wrapped_value); | ||
| } | ||
| } | ||
|
|
||
| // Also test with Box instead of Arc, a simpler captured type. | ||
| fn apply_box<'a>( | ||
| f: Box<dyn Fn(Entry<'a, String, String>) + 'a>, | ||
| ) -> impl Fn(RefCell<HashMap<String, String>>) | ||
| { | ||
| move |map| { | ||
| //~^ ERROR hidden type for `impl Fn(RefCell<HashMap<String, String>>)` captures lifetime that does not appear in bounds | ||
| let value = map.borrow_mut().entry("foo".to_string()); | ||
| //~^ ERROR `map` does not live long enough | ||
| //~| ERROR temporary value dropped while borrowed | ||
| f(value); | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| error[E0597]: `map` does not live long enough | ||
| --> $DIR/closure-upvar-named-lifetime.rs:19:21 | ||
| | | ||
| LL | fn apply<'a>( | ||
| | -- lifetime `'a` defined here | ||
| ... | ||
| LL | move |map| { | ||
| | --- binding `map` declared here | ||
| LL | | ||
| 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 `'a` | ||
| LL | } | ||
| | - `map` dropped here while still borrowed | ||
| | | ||
| 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 | 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 | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| ... | ||
| LL | f(wrapped_value); | ||
| | ---------------- argument requires that borrow lasts for `'a` | ||
| | | ||
| 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 | ||
| --> $DIR/closure-upvar-named-lifetime.rs:17:5 | ||
| | | ||
| LL | fn apply<'a>( | ||
| | -- hidden type `{closure@$DIR/closure-upvar-named-lifetime.rs:17:5: 17:15}` captures the lifetime `'a` as defined here | ||
| LL | f: Arc<dyn Fn(Entry<'a, String, String>) + 'a>, | ||
| LL | ) -> impl Fn(RefCell<HashMap<String, String>>) | ||
| | ----------------------------------------- opaque type defined here | ||
| LL | { | ||
| LL | / move |map| { | ||
| LL | | | ||
| LL | | let value = map.borrow_mut().entry("foo".to_string()); | ||
| ... | | ||
| LL | | f(wrapped_value); | ||
| LL | | } | ||
| | |_____^ | ||
| | | ||
| help: add a `use<...>` bound to explicitly capture `'a` | ||
| | | ||
| 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 | fn apply_box<'a>( | ||
| | -- lifetime `'a` defined here | ||
| ... | ||
| LL | move |map| { | ||
| | --- binding `map` declared here | ||
| LL | | ||
| 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 `'a` | ||
| LL | } | ||
| | - `map` dropped here while still borrowed | ||
|
|
||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/closure-upvar-named-lifetime.rs:34:21 | ||
| | | ||
| 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 | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| ... | ||
| LL | f(value); | ||
| | -------- 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 | ||
| | | ||
| LL | fn apply_box<'a>( | ||
| | -- hidden type `{closure@$DIR/closure-upvar-named-lifetime.rs:32:5: 32:15}` captures the lifetime `'a` as defined here | ||
| LL | f: Box<dyn Fn(Entry<'a, String, String>) + 'a>, | ||
| LL | ) -> impl Fn(RefCell<HashMap<String, String>>) | ||
| | ----------------------------------------- opaque type defined here | ||
| LL | { | ||
| LL | / move |map| { | ||
| LL | | | ||
| LL | | let value = map.borrow_mut().entry("foo".to_string()); | ||
| ... | | ||
| LL | | f(value); | ||
| LL | | } | ||
| | |_____^ | ||
| | | ||
| help: add a `use<...>` bound to explicitly capture `'a` | ||
| | | ||
| LL | ) -> impl Fn(RefCell<HashMap<String, String>>) + use<'a> | ||
| | +++++++++ | ||
|
|
||
| error: aborting due to 6 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0597, E0700, E0716. | ||
| For more information about an error, try `rustc --explain E0597`. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, the diff is