diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-11-28 18:54:51 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-11-28 18:55:03 +0100 |
| commit | 2007f30b1137eec20678e3e9cd96788dc6c1b222 (patch) | |
| tree | 3a4b37729a03a67e5d6b9789c475f9d1831b1d9a | |
| parent | b5ef789315f156ad79bff19afb4aadf6eec45ca2 (diff) | |
Better error message for named instead of positional argument
| -rw-r--r-- | crates/typst/src/foundations/args.rs | 20 | ||||
| -rw-r--r-- | tests/typ/compiler/call.typ | 5 |
2 files changed, 23 insertions, 2 deletions
diff --git a/crates/typst/src/foundations/args.rs b/crates/typst/src/foundations/args.rs index 8732c7b5..74e8e2be 100644 --- a/crates/typst/src/foundations/args.rs +++ b/crates/typst/src/foundations/args.rs @@ -2,7 +2,7 @@ use std::fmt::{self, Debug, Formatter}; use ecow::{eco_format, eco_vec, EcoString, EcoVec}; -use crate::diag::{bail, At, SourceDiagnostic, SourceResult}; +use crate::diag::{bail, error, At, SourceDiagnostic, SourceResult}; use crate::foundations::{ func, repr, scope, ty, Array, Dict, FromValue, IntoValue, Repr, Str, Value, }; @@ -121,10 +121,26 @@ impl Args { { match self.eat()? { Some(v) => Ok(v), - None => bail!(self.span, "missing argument: {what}"), + None => bail!(self.missing_argument(what)), } } + /// The error message for missing arguments. + fn missing_argument(&self, what: &str) -> SourceDiagnostic { + for item in &self.items { + let Some(name) = item.name.as_deref() else { continue }; + if name == what { + return error!( + item.span, + "the argument `{what}` is positional"; + hint: "try removing `{}:`", name, + ); + } + } + + error!(self.span, "missing argument: {what}") + } + /// Find and consume the first castable positional argument. pub fn find<T>(&mut self) -> SourceResult<Option<T>> where diff --git a/tests/typ/compiler/call.typ b/tests/typ/compiler/call.typ index 5297ec15..e48eabfd 100644 --- a/tests/typ/compiler/call.typ +++ b/tests/typ/compiler/call.typ @@ -48,6 +48,11 @@ #set text(font: "Arial", font: "Helvetica") --- +// Error: 4-15 the argument `amount` is positional +// Hint: 4-15 try removing `amount:` +#h(amount: 0.5) + +--- // Error: 2-6 expected function, found boolean #true() |
