feat(validator): move return type and arity check to infer

This commit is contained in:
rvcas
2023-02-15 12:58:18 -05:00
committed by Lucas
parent a88a193383
commit d03288cece
3 changed files with 45 additions and 83 deletions

View File

@@ -756,6 +756,30 @@ The best thing to do from here is to remove it."#))]
location: Span,
name: String,
},
#[error("A validator must return {}", "Bool".bright_blue().bold())]
#[diagnostic(code("illegal::validator_return_type"))]
#[diagnostic(help(r#"While analyzing the return type of your validator, I found it to be:
╰─▶ {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."#
, type_Bool = "Bool".bright_blue().bold()
, signature = return_type.to_pretty(0).red()
))]
ValidatorMustReturnBool {
#[label("invalid return type")]
location: Span,
return_type: Arc<Type>,
},
#[error("Validators requires at least {} arguments.", at_least.to_string().purple().bold())]
#[diagnostic(code("illegal::validator_arity"))]
WrongValidatorArity {
at_least: u8,
#[label("not enough arguments")]
location: Span,
},
}
impl Error {
@@ -1210,13 +1234,16 @@ pub enum Warning {
name: String,
},
#[error("I came across a validator in a {} module\nwhich means I'm going to ignore it.\n", "lib/".purple())]
#[error("I came across a validator in a {} module which means\nI'm going to ignore it.\n", "lib/".purple())]
#[diagnostic(help(
"No big deal, but you might want to move it to a\n{} module or remove it to get rid of that warning.",
"No big deal, but you might want to move it to a {} module\nor remove it to get rid of that warning.",
"validators/".purple()
))]
#[diagnostic(code("unused::validator"))]
ValidatorInLibraryModule { location: Span },
ValidatorInLibraryModule {
#[label("unused")]
location: Span,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

View File

@@ -268,19 +268,22 @@ fn infer_definition(
environment,
kind,
)? {
// Do we want to do this here?
// or should we remove this and keep the later check
// the later check has a much more specific error message
// we may want to not do this here
environment.unify(
typed_fun.return_type.clone(),
builtins::bool(),
typed_fun.location,
false,
)?;
if !typed_fun.return_type.is_bool() {
return Err(Error::ValidatorMustReturnBool {
return_type: typed_fun.return_type.clone(),
location: typed_fun.location,
});
}
let typed_params = typed_fun.arguments.drain(0..params_length).collect();
if typed_fun.arguments.len() < 2 {
return Err(Error::WrongValidatorArity {
at_least: 2,
location: typed_fun.location,
});
}
Ok(Definition::Validator(Validator {
doc,
end_position,