dtolnay/anyhow

Couldn't use with nom error

Dangerise opened this issue · 2 comments

Sorry about my poor English, tell me anything you don't understand

I try to use anyhow when I'm writing a with nom , then I set the return type as anyhow::Result

here is a simple example

fn ftag(input: &str) -> IResult<&str, &str> {
    use nom::bytes::complete::tag;
    tag("a")(input)
}

fn anyhow_with_nom(input: &str) -> anyhow::Result<&str> {
    let (input, _) = ftag(input)?;
    Ok(input)
}

The error message :

error[E0521]: borrowed data escapes outside of function
   --> src\lexer.rs:121:22
    |
120 | fn anyhow_with_nom(input: &str) -> anyhow::Result<&str> {
    |                    -----  - let's call the lifetime of this reference `'1`
    |                    |
    |                    `input` is a reference that is only valid in the function body
121 |     let (input, _) = ftag(input)?;
    |                      ^^^^^^^^^^^
    |                      |
    |                      `input` escapes the function body here
    |                      argument requires that `'1` must outlive `'static`


It will be fine if I turn anyohw::Result into nom::IResult

fn ftag(input: &str) -> IResult<&str, &str> {
    use nom::bytes::complete::tag;
    tag("a")(input)
}

fn anyhow_with_nom(input: &str) -> IResult<&str, &str> {
    let (input, parttern) = ftag(input)?;
    Ok((input, parttern))
}

I don't expect that anyone will be available to help you with this in this issue tracker.

If this is still an issue, you could try taking this question to any of the resources shown in https://www.rust-lang.org/community.

j16r commented

Good answer and solution here: https://stackoverflow.com/a/73506323