From ae05dc08765b8db8e149a56627cd29a878a0bce5 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 5 Oct 2021 19:45:38 +0200 Subject: [PATCH] Always use first positional argument in `expect()` This changes `#h(100)` from "missing argument: spacing" to "expected linear, found integer". --- src/eval/function.rs | 15 ++++++++++----- src/library/utility.rs | 8 +++----- tests/typ/utility/collection.typ | 2 +- tests/typ/utility/math.typ | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/eval/function.rs b/src/eval/function.rs index 205690dfb..d9f79adfe 100644 --- a/src/eval/function.rs +++ b/src/eval/function.rs @@ -93,16 +93,21 @@ impl Args { None } - /// Find and consume the first castable positional argument, returning a - /// `missing argument: {what}` error if no match was found. + /// Try to cast the first positional argument ir returning a `missing + /// argument: {what}` error if no positional argument is left. pub fn expect(&mut self, what: &str) -> TypResult where T: Cast>, { - match self.eat() { - Some(found) => Ok(found), - None => bail!(self.span, "missing argument: {}", what), + for (i, slot) in self.items.iter().enumerate() { + if slot.name.is_none() { + let value = self.items.remove(i).value; + let span = value.span; + return T::cast(value).at(span); + } } + + bail!(self.span, "missing argument: {}", what); } /// Find and consume all castable positional arguments. diff --git a/src/library/utility.rs b/src/library/utility.rs index 5de674642..3a2f49b7f 100644 --- a/src/library/utility.rs +++ b/src/library/utility.rs @@ -6,11 +6,9 @@ use crate::color::{Color, RgbaColor}; /// `assert`: Ensure that a condition is fulfilled. pub fn assert(_: &mut EvalContext, args: &mut Args) -> TypResult { - let Spanned { v, span } = args.expect("condition")?; - match v { - Value::Bool(true) => {} - Value::Bool(false) => bail!(span, "assertion failed"), - v => bail!(span, "expected boolean, found {}", v.type_name()), + let Spanned { v, span } = args.expect::>("condition")?; + if !v { + bail!(span, "assertion failed"); } Ok(Value::None) } diff --git a/tests/typ/utility/collection.typ b/tests/typ/utility/collection.typ index a97184d35..92ec28673 100644 --- a/tests/typ/utility/collection.typ +++ b/tests/typ/utility/collection.typ @@ -19,7 +19,7 @@ #len() --- -// Error: 6-10 expected string, array or dictionary +// Error: 6-10 expected string, array or dictionary, found length #len(12pt) --- diff --git a/tests/typ/utility/math.typ b/tests/typ/utility/math.typ index aeb0d6adb..7217babef 100644 --- a/tests/typ/utility/math.typ +++ b/tests/typ/utility/math.typ @@ -16,7 +16,7 @@ #abs(10pt + 50%) --- -// Error: 6-17 expected numeric value +// Error: 6-17 expected numeric value, found string #abs("no number") ---