Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(core): panics in Weir (#2493)

Elijah Potterd357225764b7
diff --git a/harper-core/src/weir/error.rs b/harper-core/src/weir/error.rs
index 014119d31..9322bf040 100644
--- a/harper-core/src/weir/error.rs
+++ b/harper-core/src/weir/error.rs
@@ -1,6 +1,6 @@
use thiserror::Error;
-#[derive(Debug, Error)]
+#[derive(Debug, Error, Eq, PartialEq)]
pub enum Error {
#[error("Encountered a token that is unsupported by the parser.")]
UnsupportedToken(String),
diff --git a/harper-core/src/weir/mod.rs b/harper-core/src/weir/mod.rs
index 0b03fcae4..8d1460021 100644
--- a/harper-core/src/weir/mod.rs
+++ b/harper-core/src/weir/mod.rs
@@ -262,6 +262,10 @@ impl ExprLinter for WeirLinter {
#[cfg(test)]
pub mod tests {
+ use quickcheck_macros::quickcheck;
+
+ use crate::weir::Error;
+
use super::{TestResult, WeirLinter};
#[track_caller]
@@ -353,4 +357,16 @@ pub mod tests {
assert_passes_all(&mut linter);
assert_eq!(1, linter.count_tests());
}
+
+ #[test]
+ fn errors_properly_with_missing_expr() {
+ let source = "expr main";
+ let res = WeirLinter::new(source);
+ assert_eq!(res.err(), Some(Error::ExpectedVariableUndefined))
+ }
+
+ #[quickcheck]
+ fn does_not_panic(s: String) {
+ let _ = WeirLinter::new(s.as_str());
+ }
}
diff --git a/harper-core/src/weir/parsing/stmt.rs b/harper-core/src/weir/parsing/stmt.rs
index b8ec10994..1bf161c99 100644
--- a/harper-core/src/weir/parsing/stmt.rs
+++ b/harper-core/src/weir/parsing/stmt.rs
@@ -133,7 +133,10 @@ fn parse_stmt(tokens: &[Token], source: &[char]) -> Result<FoundNode<Option<AstS
Ok(FoundNode::new(
Some(AstStmtNode::create_set_expr(
tokens[cursor + 2].span.get_content_string(source),
- AstExprNode::Seq(parse_seq(&tokens[cursor + 4..end], source)?),
+ AstExprNode::Seq(parse_seq(
+ &tokens[(cursor + 4).min(end)..end],
+ source,
+ )?),
)),
end + 1,
))