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

Commit 209601b

Browse files
authored
fix: filter creates an error with an incorrect found (#839)
2 parents 8740951 + 7f31950 commit 209601b

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

src/combinator.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ where
252252
{
253253
#[inline(always)]
254254
fn go<M: Mode>(&self, inp: &mut InputRef<'src, '_, I, E>) -> PResult<M, O> {
255+
let found = inp.peek_maybe();
255256
let before = inp.cursor();
256257
// Remove the pre-inner alt, to be reinserted later so we always preserve it
257258
let old_alt = inp.errors.alt.take();
@@ -260,30 +261,29 @@ where
260261
let span = inp.span_since(&before);
261262
let new_alt = inp.errors.alt.take();
262263

264+
inp.errors.alt = old_alt;
263265
match res {
264266
Ok(out) => {
265267
if (self.filter)(&out) {
266268
// If successful, reinsert the original alt and then apply the new alt on top of it, since both are valid
267-
inp.errors.alt = old_alt;
268269
if let Some(new_alt) = new_alt {
269270
inp.add_alt_err(&new_alt.pos, new_alt.err);
270271
}
271272
Ok(M::bind(|| out))
272273
} else {
273274
// If unsuccessful, reinsert the original alt but replace the new alt with the "something else" error (since it overrides it)
274275
let expected = [DefaultExpected::SomethingElse];
275-
let err = E::Error::expected_found(expected, None, span);
276-
inp.errors.alt = old_alt;
276+
// TODO: Use something more detailed than the next token as the found
277+
let err = E::Error::expected_found(expected, found, span);
277278
inp.add_alt_err(&before.inner, err);
278279
Err(())
279280
}
280281
}
281282

282283
Err(_) => {
283-
inp.errors.alt = old_alt;
284-
if let Some(new_alt) = new_alt {
285-
inp.add_alt_err(&new_alt.pos, new_alt.err);
286-
}
284+
// Can't fail!
285+
let new_alt = new_alt.unwrap();
286+
inp.add_alt_err(&new_alt.pos, new_alt.err);
287287
Err(())
288288
}
289289
}

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3840,6 +3840,17 @@ mod tests {
38403840
fn filter() {
38413841
use crate::{DefaultExpected, LabelError};
38423842

3843+
let parser = just::<_, _, extra::Err<Rich<_>>>("a").filter(|_| false);
3844+
3845+
assert_eq!(
3846+
parser.parse("a").into_result(),
3847+
Err(vec![LabelError::<&str, _>::expected_found(
3848+
[DefaultExpected::SomethingElse],
3849+
Some('a'.into()),
3850+
SimpleSpan::new((), 0..1)
3851+
),])
3852+
);
3853+
38433854
let parser = group((
38443855
just("a").or_not(),
38453856
just("b").filter(|_| false).or_not(),

0 commit comments

Comments
 (0)