Better error message for named instead of positional argument

This commit is contained in:
Laurenz 2023-11-28 18:54:51 +01:00
parent b5ef789315
commit 2007f30b11
2 changed files with 23 additions and 2 deletions

View File

@ -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

View File

@ -47,6 +47,11 @@
// Error: 26-30 duplicate argument: font
#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()