Skip to content

try_map error changing spans if used with padded_by #918

@ablomm

Description

@ablomm

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

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