I have a very small repo which currently does not compile, with the following error:
error[E0597]: `line` does not live long enough
--> src/main.rs:19:20
|
19 | let line = line.trim();
| ^^^^-------
| |
| borrowed value does not live long enough
| argument requires that `line` is borrowed for `'static`
...
22 | }
| - `line` dropped here while still borrowed
I've had trouble producing a minimal example demoing the problem: this (playground) works as expected, despite that fn main
is identical to the one which fails, as are the signatures of ItemParser::parse
and Item::pretty_to
.
Inlining a section of fn main
, and some signatures:
let parser = ItemParser::new();
let stdin = stdin();
let reader = stdin.lock();
let reader = BufReader::new(reader);
let stdout = stdout();
let mut writer = stdout.lock();
for line in reader.lines() {
let line = line?;
let line = line.trim();
let item = parser.parse(line)?;
item.pretty_to(&mut writer)?;
}
The same issue persists when I comment out item.pretty_to(&mut writer)?;
, so I believe that that isn't the problem.
I can't show the actual code for ItemParser
as it's generated by LALRPOP, but the signature as reported by rustdoc is
pub struct ItemParser { /* fields omitted */ }
pub fn parse<'input>(
&self,
input: &'input str
) -> Result<Item<'input>, ParseError<usize, Token<'input>, &'static str>>
Unlike this issue, nothing in this crate explicitly requires a 'static
lifetime.
My expectation is that at the head of the for
loop, item
has type io::Result<String>
. After we discard the error and trim the edges, it should have type &'a str
, where 'a
is the lifetime scoped to this iteration of the for
loop. In that case, parsing the line should produce an Item<'a>
with the same lifetime. It drops before the lifetime ends, in appropriate sequence. As nothing visibly requests a 'static
lifetime, I don't know where that requirement is coming from.