Closure captures not indicated in borrow check error
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
Description
Code
fn foo(x: &[u32]) -> impl Fn() -> u32 + 'static {
let p = &x[0];
move || {
let y = p + 1;
y
}
}
fn main() {}
Current output
error: lifetime may not live long enough
--> src/main.rs:3:5
|
1 | fn foo(x: &[u32]) -> impl Fn() -> u32 + 'static {
| - let's call the lifetime of this reference `'1`
2 | let p = &x[0];
3 | / move || {
4 | | let y = p + 1;
5 | | y
6 | | }
| |_____^ returning this value requires that `'1` must outlive `'static`
|
help: consider changing `impl Fn() -> u32 + 'static`'s explicit `'static` bound to the lifetime of argument `x`
|
1 | fn foo(x: &[u32]) -> impl Fn() -> u32 + '_ {
| ~~
help: alternatively, add an explicit `'static` bound to this reference
|
1 | fn foo(x: &'static [u32]) -> impl Fn() -> u32 + 'static {
| ~~~~~~~~~~~~~~
Desired output
error: lifetime may not live long enough
--> src/main.rs:3:5
|
1 | fn foo(x: &[u32]) -> impl Fn() -> u32 + 'static {
| - let's call the lifetime of this reference `'1`
2 | let p = &x[0];
3 | / move || {
4 | | let y = p + 1;
| | ^ capturing this value means the closure cannot outlive `'1`
5 | | y
6 | | }
| |_____^ returning this value requires that `'1` must outlive `'static`
|
help: consider changing `impl Fn() -> u32 + 'static`'s explicit `'static` bound to the lifetime of argument `x`
|
1 | fn foo(x: &[u32]) -> impl Fn() -> u32 + '_ {
| ~~
help: alternatively, add an explicit `'static` bound to this reference
|
1 | fn foo(x: &'static [u32]) -> impl Fn() -> u32 + 'static {
| ~~~~~~~~~~~~~~
Rationale and extra context
The trouble with the current output is that while it indicates that the closure cannot outlive the reference x, it does not indicate where in the closure a reference was captured that is tied to x and therefore forces it to be short-lived. If the closure is large this may require some care to locate, especially since the captured variable need not be x itself and instead is something derived from it.
Other cases
No response
Rust Version
rustc 1.78.0-nightly (c67326b06 2024-03-15)
binary: rustc
commit-hash: c67326b063bd27ed04f306ba2e372cd92e0a8751
commit-date: 2024-03-15
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the provided Rust snippet and rustc 1.78.0-nightly, then trace the borrow-checker diagnostic that points at the closure body. The change is done when the diagnostic identifies the captured value's use and explains that it prevents the closure from outliving the referenced lifetime, with existing help messages preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100