Skip to content

Commit

Permalink
feat: enforcement that spend first arg is option
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Aug 22, 2024
1 parent 3fa1211 commit 5850c74
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
4 changes: 4 additions & 0 deletions crates/aiken-lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ impl TypedFunction {
})
}

pub fn is_spend(&self) -> bool {
self.name == HANDLER_SPEND
}

pub fn has_valid_purpose_name(&self) -> bool {
self.name == HANDLER_SPEND
|| self.name == HANDLER_PUBLISH
Expand Down
30 changes: 19 additions & 11 deletions crates/aiken-lang/src/tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn expect_multi_patterns() {
fn validator_correct_form() {
let source_code = r#"
validator foo {
spend(d, r, oref, c) {
spend(d: Option<Data>, r, oref, c) {
True
}
}
Expand Down Expand Up @@ -389,7 +389,7 @@ fn validator_in_lib_warning() {
fn multi_validator() {
let source_code = r#"
validator foo(foo: ByteArray, bar: Int) {
spend(_d, _r, _oref, _c) {
spend(_d: Option<Data>, _r, _oref, _c) {
foo == #"aabb"
}
Expand All @@ -408,7 +408,7 @@ fn multi_validator() {
fn multi_validator_warning() {
let source_code = r#"
validator foo(foo: ByteArray, bar: Int) {
spend(_d, _r, _oref, _c) {
spend(_d: Option<Data>, _r, _oref, _c) {
foo == #"aabb"
}
Expand Down Expand Up @@ -458,7 +458,7 @@ fn exhaustiveness_simple() {
fn validator_args_no_annotation() {
let source_code = r#"
validator hello(d) {
spend(a, b, oref, c) {
spend(a: Option<Data>, b, oref, c) {
True
}
}
Expand All @@ -475,9 +475,13 @@ fn validator_args_no_annotation() {
assert!(param.tipo.is_data());
});

validator.handlers[0].arguments.iter().for_each(|arg| {
assert!(arg.tipo.is_data());
})
validator.handlers[0]
.arguments
.iter()
.skip(1)
.for_each(|arg| {
assert!(arg.tipo.is_data());
})
})
}

Expand Down Expand Up @@ -2451,8 +2455,10 @@ fn validator_private_type_leak() {
}
validator bar {
spend(datum: Datum, redeemer: Redeemer, _oref, _ctx) {
datum.foo == redeemer.bar
spend(datum: Option<Datum>, redeemer: Redeemer, _oref, _ctx) {
expect Some(d) = datum
d.foo == redeemer.bar
}
}
"#;
Expand All @@ -2475,8 +2481,10 @@ fn validator_public() {
}
validator bar {
spend(datum: Datum, redeemer: Redeemer, _oref, _ctx) {
datum.foo == redeemer.bar
spend(datum: Option<Datum>, redeemer: Redeemer, _oref, _ctx) {
expect Some(d) = datum
d.foo == redeemer.bar
}
}
"#;
Expand Down
10 changes: 10 additions & 0 deletions crates/aiken-lang/src/tipo/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ fn infer_definition(
});
}

if typed_fun.is_spend() && !typed_fun.arguments[0].tipo.is_option() {
return Err(Error::CouldNotUnify {
location: typed_fun.arguments[0].location,
expected: Type::option(typed_fun.arguments[0].tipo.clone()),
given: typed_fun.arguments[0].tipo.clone(),
situation: None,
rigid_type_names: Default::default(),
});
}

for arg in typed_fun.arguments.iter_mut() {
if arg.tipo.is_unbound() {
arg.tipo = Type::data();
Expand Down

0 comments on commit 5850c74

Please sign in to comment.