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

MapExtra cannot infer state type #624

Open
MechSlayer opened this issue Apr 16, 2024 · 1 comment
Open

MapExtra cannot infer state type #624

MechSlayer opened this issue Apr 16, 2024 · 1 comment

Comments

@MechSlayer
Copy link
Contributor

Whenever I try to access the state from MapExtra the compiler complains about not being able to infer the state. It doesn't matter if it's in select!. map_with or anything else.

Example:

fn parser<'a, I>() -> impl Parser<'a, I, (), extra::Full<Rich<'a, Token<'a>, TextSpan>, ParsingUnit, ()>>
    where I: ValueInput<'a, Token=Token<'a>, Span=TextSpan>
{
    let identifier = select! {
        Token::Identifier(string) = ex => ex.state().intern(string)
    };


    identifier.repeated().collect::<()>()
}

And the error:

   |
11 |         Token::Identifier(string) = ex => ex.state().intern(string)
   |                                                      ^^^^^^ cannot infer type
@zesterer
Copy link
Owner

zesterer commented Apr 17, 2024

Unfortunately, this is due to a limitation in rustc (interestingly, other Rust compilers like mrustc doesn't have this limitation and can in theory infer this type without extra hints). Rust will only permit a method call if, by the time that it meets the method call, it knows the full type of the left-hand side (or can infer it from the method call alone).

You can easily provide a hint to Rust by doing (e.state() as &mut MyStateType).intern(string).

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

No branches or pull requests

2 participants