Incorrect direction/place for "returning this value requires that `<variable>` is borrowed for `'static`" for E0597
Open
Nobody has claimed this yet.
A-borrow-checker
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
#[derive(Debug)]
pub struct Report(Vec<Box<dyn std::fmt::Debug>>);
fn main() -> Result<(), Report> {
let string = "".to_string();
let (_, b) = string
.split(' ')
.filter_map(|x| x.split_once(' '))
.last()
.unwrap();
b.split_once(' ').ok_or_else(|| Report(vec![Box::new(b)]))?;
Ok(())
}
Current output
error[E0597]: `string` does not live long enough
--> test.rs:6:18
|
5 | let string = "".to_string();
| ------ binding `string` declared here
6 | let (_, b) = string
| ^^^^^^ borrowed value does not live long enough
7 | .split(' ')
8 | .filter_map(|x| x.split_once(' '))
| ----------------- returning this value requires that `string` is borrowed for `'static`
...
13 | }
| - `string` dropped here while still borrowed
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.
Desired output
error[E0597]: `string` does not live long enough
--> test.rs:6:18
|
5 | let string = "".to_string();
| ------ binding `string` declared here
6 | let (_, b) = string
| ^^^^^^ borrowed value does not live long enough
...
11 | b.split_once(' ').ok_or_else(|| Report(vec![Box::new(b)]))?;
| ^ - `string` dropped here while still borrowed
| |
| returning this value requires that `string` is borrowed for `'static`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.
┌──────┐
│ OR │
└──────┘
error[E0515]: cannot return value referencing local variable `string`
--> test.rs:11:63
|
6 | let (_, b) = string
| ------ `string` is borrowed here
...
11 | b.split_once(' ').ok_or_else(|| Report(vec![Box::new(b)]))?;
| ^ returns a value referencing data owned by the current function
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0515`.
Rationale and extra context
No response
Other cases
This error message look me quite a while to reproduce, so it seems to be pretty specific with the structure of `Option<Option<…>>`.
Changing `ok_or_else` to `ok_or` causes the same error.
Rust Version
rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6
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 supplied example with rustc 1.77.0 and compare the E0597 and E0515 diagnostics. Trace the compiler diagnostic code responsible for the borrow-checker span and message placement, then add a regression test for this nested Option case. Done means the diagnostic points to the returning expression as shown, or the alternative E0515 output is produced.
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
- 38/100