Skip to content

Clone trait error in recursive #878

@ivanka2012

Description

@ivanka2012

This isn't necessarily an issue per se, but I thought the following problem I encountered would be worth documenting since this took me a while to finally figure out:

If you use recursive and call a function in it that returns a parser, then make sure that the parser that function returns also implements Clone.

This doesn't work:

fn another_parser<'a>() -> impl Parser<'a, &'a str, ()> {
    // ...
}

fn test(){
    // the trait bound `impl chumsky::Parser<'_, &str, ()>: Clone` is not satisfied
    // the trait `Clone` is not implemented for `impl chumsky::Parser<'_, &str, ()>`
    recursive(|r| {
        let parser = another_parser();

        parser
    });
}

This works:

// Note the Clone
fn another_parser<'a>() -> impl Parser<'a, &'a str, ()> + Clone {
    // ...
}

fn test(){
    recursive(|r| {
        let parser = another_parser();

        parser
    });
}

This also applies to cases where the parser passes useful data i.e. impl Parser<'a, &'a str, SomeType> instead of impl Parser<'a, &'a str, ()>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions