I'm using winnow for parsing alongside bumpalo, and I have some code that goes like this. The problematic part is right at the end.
#[derive(Debug, Clone, Copy)]
pub struct ArenaString<'a> {
pub content: &'a str,
pub arena: &'a Bump,
}
// Also a bunch of other traits that make ArenaString usable as a Parser input delegating to the underlying string
pub fn arena_separated<'a, O, O2, E, ParseNext, SepParser>(
mut item: ParseNext,
mut sep: SepParser,
) -> impl for<'c> FnMut(&'c mut ArenaString<'a>) -> winnow::Result<BumpVec<'a, O>, E>
+ Parser<ArenaString<'a>, BumpVec<'a, O>, E>
// ^^ This ought to be blanket implemented but I'm explicitly asking for it just to be sure
where
E: ParserError<ArenaString<'a>>,
ParseNext: Parser<ArenaString<'a>, O, E>,
SepParser: Parser<ArenaString<'a>, O2, E>,
{ /* ... */ }
enum Metavar<'a, T> {
Var(Span<'a>, &'static str),
Literal(T),
}
impl<'a, T: Syntax<'a>> Syntax<'a> for Metavar<'a, T> {
fn parse<'b>(input: &'b mut ArenaString<'a>) -> winnow::Result<Self> {
/* ... */
}
}
impl<'a> Syntax<'a> for Tags<'a> {
fn parse<'b>(input: &'b mut ArenaString<'a>) -> winnow:Result<Self> {
let ArenaString { arena, content } = *input;
let semi = ArenaString {
arena,
content: ";",
};
let mut p = arena_separated(Metavar::parse, semi).map(Tags);
p.parse_next(input)
}
}
Tags::parse
fails to compile with the following error:
177 | impl<'a> Syntax<'a> for Tags<'a> {
| -- lifetime `'a` defined here
178 | fn parse<'b>(input: &'b mut ArenaString<'a>) -> winnow::Result<Self> {
| ----- `input` is a reference that is only valid in the associated function body
...
184 | let mut p = arena_separated(Metavar::parse, semi).map(Tags);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `input` escapes the associated function body here
| argument requires that `'a` must outlive `'static`
This feels like it ought to work, because although I'm being passed a &'b mut
reference, I can dereference it to get hold of a &'a T
which is Copy
, and return that inner object. As a result, I can't understand why it's complaining that anything must outlive the current function body or why the inner lifetime must outlive 'static
. Can anyone help?