From b87cd88476a0eb4c0187321a6ab42c7644531b67 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Fri, 9 Jul 2021 16:14:53 -0400 Subject: [PATCH] syntax: include only the start of the character class on error This fixes a bug where the caret in some types of error messages was not quite correct. Fixes #792, Closes #794 --- regex-syntax/src/ast/parse.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/regex-syntax/src/ast/parse.rs b/regex-syntax/src/ast/parse.rs index 579bee8bf..0b0efa819 100644 --- a/regex-syntax/src/ast/parse.rs +++ b/regex-syntax/src/ast/parse.rs @@ -1927,7 +1927,7 @@ impl<'s, P: Borrow> ParserI<'s, P> { })); if !self.bump_and_bump_space() { return Err(self.error( - Span::new(start, self.pos()), + Span::new(start, start), ast::ErrorKind::ClassUnclosed, )); } @@ -5515,14 +5515,23 @@ bar assert_eq!( parser("[-").parse_set_class_open().unwrap_err(), TestError { - span: span(0..2), + span: span(0..0), kind: ast::ErrorKind::ClassUnclosed, } ); assert_eq!( parser("[--").parse_set_class_open().unwrap_err(), TestError { - span: span(0..3), + span: span(0..0), + kind: ast::ErrorKind::ClassUnclosed, + } + ); + + // See: https://github.com/rust-lang/regex/issues/792 + assert_eq!( + parser("(?x)[-#]").parse_with_comments().unwrap_err(), + TestError { + span: span(4..4), kind: ast::ErrorKind::ClassUnclosed, } );