Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup lifetimes of PrattParser #824

Merged
merged 1 commit into from Mar 18, 2023
Merged

Conversation

matanui159
Copy link
Contributor

Currently the typings require the passed in callbacks to have the lifetime 'i, and thus any values it reference must also have the same lifetime. If these callbacks return a value with lifetime 'i, the borrow checker incorrectly assumes that those referenced values must also outlive the return value.

For example this code:

fn test_pratt<'a>(pairs: Pairs<'a, Rule>, message: &str) -> &'a str {
    PRATT_PARSER
        .map_primary(|pair| {
            // println!("{message}");
            pair.as_str()
        })
        .map_infix(|lhs, pair, rhs| {
            println!("{message}");
            pair.as_str()
        })
        .parse(pairs)
}

fails to compile with this error:

error[E0621]: explicit lifetime required in the type of `message`
   --> src/compiler/expression.rs:285:5
    |
284 |   fn test_pratt<'a>(pairs: Pairs<'a, Rule>, message: &str) -> &'a str {
    |                                                      ---- help: add explicit lifetime `'a` to the type of `message`: `&'a str`
285 | /     PRATT_PARSER
286 | |         .map_primary(|pair| {
287 | |             // println!("{message}");
288 | |             pair.as_str()
...   |
293 | |         })
294 | |         .parse(pairs)
    | |_____________________^ lifetime `'a` required

This is a small PR that adds a new lifetime to PrattParserMap so the callbacks can have a separate lifetime. This fixes the above issue.

@matanui159 matanui159 requested a review from a team as a code owner March 18, 2023 01:00
@matanui159 matanui159 requested review from tomtau and removed request for a team March 18, 2023 01:00
Copy link
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks for the PR!

@tomtau tomtau merged commit b98805c into pest-parser:master Mar 18, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants