chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

try_map error changing spans if used with padded_by

Open ablomm opened this issue 1 month ago • 2 comments

Hi,

I am facing an issue where a custom error I throw in a try_map (with the correct span) has the span changed when used with padded_by.

Here is an example:

use chumsky::prelude::*;

fn main() {
    let errors = parser().parse("onetwo").into_errors();

    println!("{:?}", errors);
}

pub fn parser<'src>() -> impl Parser<'src, &'src str, (), extra::Err<Rich<'src, char>>> {
    just("one")
        .padded_by(text::whitespace())
        .then(
            just("two")
                .try_map(|val, span| {
                    if val == "two" {
                        Err(Rich::custom(span, "uh oh"))
                    } else {
                        Ok(val)
                    }
                })
                .map_err(|error| {
                    println!("err1 {:?}", error);
                    error
                }),
        )
        .map_err(|error| {
            println!("err2 {:?}", error);
            error
        })
        .ignored()
}

This will print the following:

err1 uh oh at 3..6
err2 uh oh at 3..4
[uh oh at 3..4]

Notice how the original span (3..6) is converted to 3..4.

If I remove the line .padded_by(text::whitespace()), or I change it to .padded(), then I get the correct spans:

err1 uh oh at 3..6
err2 uh oh at 3..6
[uh oh at 3..6]

I can't tell if I'm doing something wrong, or if this is a bug.

I am using Chumsky v0.11.2

ablomm avatar Nov 25 '25 08:11 ablomm

Thanks for reporting this! I'll try to get a chance to look at this soon, although my usual schedule has been interrupted due to becoming a dad - so if you don't get a reply, I'm not ignoring it!

zesterer avatar Nov 26 '25 00:11 zesterer

Thanks for reporting this! I'll try to get a chance to look at this soon, although my usual schedule has been interrupted due to becoming a dad - so if you don't get a reply, I'm not ignoring it!

No problems. Congrats!

ablomm avatar Nov 26 '25 02:11 ablomm