fix: do not panic with zero-sized result

This commit is contained in:
electria 2026-10-06 16:11:40 -07:00
commit 00dfcdb10c
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -28,8 +28,8 @@ impl Nodes {
let len = nodes.len(); let len = nodes.len();
collapse_toplevel(nodes, len)?; collapse_toplevel(nodes, len)?;
match nodes.pop().unwrap() { match nodes.pop() {
Node::Number(n) => Ok(n), Some(Node::Number(n)) => Ok(n),
_ => Err(Error::CollapseFailed(self)), _ => Err(Error::CollapseFailed(self)),
} }
} }
@ -193,6 +193,8 @@ impl Function {
#[cfg(test)] #[cfg(test)]
use super::PREC; use super::PREC;
#[cfg(test)]
use std::assert_matches;
#[test] #[test]
fn test() { fn test() {
@ -211,3 +213,11 @@ fn test() {
1.9092974268256817, 1.9092974268256817,
); );
} }
#[test]
fn test_zerosized() {
assert_matches!(
Nodes(vec![Node::ParenthesisStart, Node::ParenthesisEnd]).evaluate(),
Err(Error::CollapseFailed(Nodes(v))) if v.is_empty()
)
}