Allow prop test argument to be (optionally) annotated.

This commit is contained in:
KtorZ 2024-03-02 11:19:21 +01:00
parent 93347d8e7b
commit cf61387a41
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
6 changed files with 87 additions and 6 deletions

View File

@ -691,6 +691,7 @@ pub struct ArgVia<T, Expr> {
pub location: Span, pub location: Span,
pub via: Expr, pub via: Expr,
pub tipo: T, pub tipo: T,
pub annotation: Option<Annotation>,
} }
impl<T, Ann> From<ArgVia<T, Ann>> for Arg<T> { impl<T, Ann> From<ArgVia<T, Ann>> for Arg<T> {

View File

@ -12,12 +12,13 @@ Test(
location: 9..10, location: 9..10,
is_validator_param: false, is_validator_param: false,
}, },
location: 9..16, location: 9..10,
via: Var { via: Var {
location: 15..16, location: 15..16,
name: "f", name: "f",
}, },
tipo: (), tipo: (),
annotation: None,
}, },
ArgVia { ArgVia {
arg_name: Named { arg_name: Named {
@ -26,12 +27,13 @@ Test(
location: 18..19, location: 18..19,
is_validator_param: false, is_validator_param: false,
}, },
location: 18..25, location: 18..19,
via: Var { via: Var {
location: 24..25, location: 24..25,
name: "g", name: "g",
}, },
tipo: (), tipo: (),
annotation: None,
}, },
], ],
body: Var { body: Var {

View File

@ -12,7 +12,7 @@ Test(
location: 9..10, location: 9..10,
is_validator_param: false, is_validator_param: false,
}, },
location: 9..27, location: 9..10,
via: FieldAccess { via: FieldAccess {
location: 15..27, location: 15..27,
label: "any_int", label: "any_int",
@ -22,6 +22,7 @@ Test(
}, },
}, },
tipo: (), tipo: (),
annotation: None,
}, },
], ],
body: Var { body: Var {

View File

@ -0,0 +1,55 @@
---
source: crates/aiken-lang/src/parser/definition/test.rs
description: "Code:\n\ntest foo(x: Int via foo()) {\n True\n}\n"
---
Test(
Function {
arguments: [
ArgVia {
arg_name: Named {
name: "x",
label: "x",
location: 9..10,
is_validator_param: false,
},
location: 9..15,
via: Call {
arguments: [],
fun: Var {
location: 20..23,
name: "foo",
},
location: 20..25,
},
tipo: (),
annotation: Some(
Constructor {
location: 12..15,
module: None,
name: "Int",
arguments: [],
},
),
},
],
body: Var {
location: 33..37,
name: "True",
},
doc: None,
location: 0..26,
name: "foo",
public: false,
return_annotation: Some(
Constructor {
location: 0..39,
module: None,
name: "Bool",
arguments: [],
},
),
return_type: (),
end_position: 38,
can_error: false,
},
)

View File

@ -4,6 +4,7 @@ use crate::{
ast, ast,
expr::UntypedExpr, expr::UntypedExpr,
parser::{ parser::{
annotation,
chain::{call::parser as call, field_access, tuple_index::parser as tuple_index, Chain}, chain::{call::parser as call, field_access, tuple_index::parser as tuple_index, Chain},
error::ParseError, error::ParseError,
expr::{self, var}, expr::{self, var},
@ -67,11 +68,13 @@ pub fn via() -> impl Parser<Token, ast::UntypedArgVia, Error = ParseError> {
} }
}), }),
)) ))
.then(just(Token::Colon).ignore_then(annotation()).or_not())
.then_ignore(just(Token::Via)) .then_ignore(just(Token::Via))
.then(fuzzer()) .then(fuzzer())
.map_with_span(|(arg_name, via), location| ast::ArgVia { .map_with_span(|((arg_name, annotation), via), location| ast::ArgVia {
arg_name, arg_name,
via, via,
annotation,
tipo: (), tipo: (),
location, location,
}) })
@ -144,4 +147,15 @@ mod tests {
"# "#
); );
} }
#[test]
fn def_property_test_annotated_fuzzer() {
assert_definition!(
r#"
test foo(x: Int via foo()) {
True
}
"#
);
}
} }

View File

@ -344,9 +344,16 @@ fn infer_definition(
let typed_via = let typed_via =
ExprTyper::new(environment, lines, tracing).infer(arg.via.clone())?; ExprTyper::new(environment, lines, tracing).infer(arg.via.clone())?;
let (annotation, inner_type) = infer_fuzzer(&typed_via.tipo(), &arg.location)?; let (inferred_annotation, inner_type) =
infer_fuzzer(&typed_via.tipo(), &arg.location)?;
Ok((Some((typed_via, inner_type)), Some(annotation))) if let Some(ref provided_annotation) = arg.annotation {
if !provided_annotation.is_logically_equal(&inferred_annotation) {
todo!("Inferred annotation doesn't match actual annotation.")
}
}
Ok((Some((typed_via, inner_type)), Some(inferred_annotation)))
} }
None => Ok((None, None)), None => Ok((None, None)),
}?; }?;
@ -401,6 +408,7 @@ fn infer_definition(
.to_owned(); .to_owned();
vec![ArgVia { vec![ArgVia {
annotation,
arg_name, arg_name,
location, location,
tipo, tipo,