From bd93ced647ceaed82b3ede51ae1032ee55e72933 Mon Sep 17 00:00:00 2001 From: rvcas Date: Mon, 20 Feb 2023 15:03:49 -0500 Subject: [PATCH] feat: improve validator arity check * add count to dynamically adjust message * check if args is greater than 3 * delete unused project level errors --- crates/aiken-lang/src/tipo/error.rs | 32 +++++++++++++++++++++++---- crates/aiken-lang/src/tipo/infer.rs | 4 ++-- crates/aiken-project/src/error.rs | 34 ----------------------------- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index 87b3c6c7..f2f3304e 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -763,7 +763,7 @@ The best thing to do from here is to remove it."#))] ╰─▶ {signature} -...but I expected this to be a {type_Bool}. If I am inferring the wrong type, you may want to add a type annotation to the function."# +...but I expected this to be a {type_Bool}. If I am inferring the wrong type, try annotating the validator's return type with Bool"# , type_Bool = "Bool".bright_blue().bold() , signature = return_type.to_pretty(0).red() ))] @@ -773,11 +773,35 @@ The best thing to do from here is to remove it."#))] return_type: Arc, }, - #[error("Validators requires at least {} arguments.", at_least.to_string().purple().bold())] + #[error("Validators require at least 2 arguments and at most 3 arguments.")] #[diagnostic(code("illegal::validator_arity"))] + #[diagnostic(help( + "Validators require either 2 or 3 arguments {}.\nIf you don't need one of the required arguments use an underscore `_datum`.", + if *count < 2 { + let missing = 2 - count; + + let mut arguments = "argument".to_string(); + + if missing > 1 { + arguments.push('s'); + } + + format!("please add the {} missing {arguments}", missing.to_string().yellow()) + } else { + let extra = count - 3; + + let mut arguments = "argument".to_string(); + + if extra > 1 { + arguments.push('s'); + } + + format!("please remove the {} extra {arguments}", extra.to_string().yellow()) + } + ))] IncorrectValidatorArity { - at_least: u8, - #[label("not enough arguments")] + count: u32, + #[label("{} arguments", if *count < 2 { "not enough" } else { "too many" })] location: Span, }, } diff --git a/crates/aiken-lang/src/tipo/infer.rs b/crates/aiken-lang/src/tipo/infer.rs index 8bb633d7..df8f3a29 100644 --- a/crates/aiken-lang/src/tipo/infer.rs +++ b/crates/aiken-lang/src/tipo/infer.rs @@ -281,9 +281,9 @@ fn infer_definition( let typed_params = typed_fun.arguments.drain(0..params_length).collect(); - if typed_fun.arguments.len() < 2 { + if typed_fun.arguments.len() < 2 || typed_fun.arguments.len() > 3 { return Err(Error::IncorrectValidatorArity { - at_least: 2, + count: typed_fun.arguments.len() as u32, location: typed_fun.location, }); } diff --git a/crates/aiken-project/src/error.rs b/crates/aiken-project/src/error.rs index 5cabd9c5..49d29a90 100644 --- a/crates/aiken-project/src/error.rs +++ b/crates/aiken-project/src/error.rs @@ -87,24 +87,6 @@ pub enum Error { error: tipo::error::Error, }, - #[error("Validator functions must return Bool")] - ValidatorMustReturnBool { - path: PathBuf, - src: String, - named: NamedSource, - location: Span, - }, - - #[error("Validator\n\n{name}\n\nrequires at least {at_least} arguments")] - WrongValidatorArity { - name: String, - at_least: u8, - location: Span, - path: PathBuf, - src: String, - named: Box, - }, - #[error("{name} failed{}", if *verbose { format!("\n{src}") } else { String::new() } )] TestFailure { name: String, @@ -259,8 +241,6 @@ impl Diagnostic for Error { Error::MissingManifest { .. } => None, Error::TomlLoading { .. } => Some(Box::new("aiken::loading::toml")), Error::Format { .. } => None, - Error::ValidatorMustReturnBool { .. } => Some(Box::new("aiken::scripts")), - Error::WrongValidatorArity { .. } => Some(Box::new("aiken::validators")), Error::TestFailure { path, .. } => Some(Box::new(path.to_str().unwrap_or(""))), Error::Http(_) => Some(Box::new("aiken::packages::download")), Error::ZipExtract(_) => None, @@ -292,8 +272,6 @@ impl Diagnostic for Error { Error::MissingManifest { .. } => Some(Box::new("Try running `aiken new ` to initialise a project with an example manifest.")), Error::TomlLoading { .. } => None, Error::Format { .. } => None, - Error::ValidatorMustReturnBool { .. } => Some(Box::new("Try annotating the validator's return type with Bool")), - Error::WrongValidatorArity { .. } => Some(Box::new("Validators require a minimum number of arguments please add the missing arguments.\nIf you don't need one of the required arguments use an underscore `_datum`.")), Error::TestFailure { evaluation_hint, .. } =>{ match evaluation_hint { None => None, @@ -366,12 +344,6 @@ impl Diagnostic for Error { } } Error::Format { .. } => None, - Error::ValidatorMustReturnBool { location, .. } => Some(Box::new( - vec![LabeledSpan::new_with_span(None, *location)].into_iter(), - )), - Error::WrongValidatorArity { location, .. } => Some(Box::new( - vec![LabeledSpan::new_with_span(None, *location)].into_iter(), - )), Error::TestFailure { .. } => None, Error::Http(_) => None, Error::ZipExtract(_) => None, @@ -396,8 +368,6 @@ impl Diagnostic for Error { Error::MissingManifest { .. } => None, Error::TomlLoading { named, .. } => Some(named.deref()), Error::Format { .. } => None, - Error::ValidatorMustReturnBool { named, .. } => Some(named), - Error::WrongValidatorArity { named, .. } => Some(named.deref()), Error::TestFailure { .. } => None, Error::Http(_) => None, Error::ZipExtract(_) => None, @@ -422,8 +392,6 @@ impl Diagnostic for Error { Error::MissingManifest { .. } => None, Error::TomlLoading { .. } => None, Error::Format { .. } => None, - Error::ValidatorMustReturnBool { .. } => None, - Error::WrongValidatorArity { .. } => None, Error::TestFailure { .. } => None, Error::Http { .. } => None, Error::ZipExtract { .. } => None, @@ -448,8 +416,6 @@ impl Diagnostic for Error { Error::MissingManifest { .. } => None, Error::TomlLoading { .. } => None, Error::Format { .. } => None, - Error::ValidatorMustReturnBool { .. } => None, - Error::WrongValidatorArity { .. } => None, Error::TestFailure { .. } => None, Error::Http { .. } => None, Error::ZipExtract { .. } => None,