Skip to content
This repository was archived by the owner on Apr 2, 2026. It is now read-only.
This repository was archived by the owner on Apr 2, 2026. It is now read-only.

Returning WithCtx trait is missing type information from Parser #953

Description

@ryansuhartanto

Take a look at this:
https://codesandbox.io/p/devbox/jolly-engelbart-967dnc?embed=1&file=%2Fsrc%2Flib.rs

I expect the .with_ctx(...) can be easily be piped before .parse(...), but it throws E0284 instead.
A bit of an issue too, the .configure(...)'s second parameter (ctx) throws E0282 without explicit type information.

I provided a workaround by making WithCtx into a Parser first.

Here's the actual code from the link:

use chumsky::prelude::*;

#[derive(Debug, Clone, Copy, Default)]
pub struct ExtraContext {
    pub dont_parse: bool,
}

pub fn parser<'src>() -> impl Parser<'src, &'src str, bool, extra::Context<ExtraContext>> {
    any::<'src, _, extra::Context<ExtraContext>>() // I need this so `configure` has proper `ctx` type
        .contextual()
        .configure(|_cfg, ctx| !ctx.dont_parse)
        .to(true)
}

// Workaround
pub fn parser_with_ctx<'src>(
    ctx: ExtraContext,
) -> impl Parser<'src, &'src str, bool, extra::Context<ExtraContext>> {
    parser().with_ctx(ctx)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parser() {
        assert_eq!(
            parser()
                // Error if uncommented
                //.with_ctx(ExtraContext { dont_parse: false })
                .parse("a")
                .into_result(),
            Ok(true)
        )
    }

    #[test]
    fn test_parser_with_ctx() {
        assert_eq!(
            parser_with_ctx(ExtraContext { dont_parse: false })
                .parse("a")
                .into_result(),
            Ok(true)
        )
    }
}
error[E0282]: type annotations needed for `&_`
  --> src/lib.rs:11:27
   |
11 |         .configure(|_cfg, ctx| !ctx.dont_parse)
   |                           ^^^   -------------- type must be known at this point
   |
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
   |
11 |         .configure(|_cfg, ctx: &_| !ctx.dont_parse)
   |                              ++++
error[E0284]: type annotations needed
  --> src/lib.rs:32:18
   |
32 |                 .parse("a")
   |                  ^^^^^
   |
   = note: cannot satisfy `<_ as ParserExtra<'_, &str>>::Error == _`

I'm quite new to Rust. Is this intended or simply a Rust limitation that's outside the scope of this library?

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