Skip to content

Commit

Permalink
feat: add code messages when using expects on constrs
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Jan 3, 2024
1 parent 72458ab commit 80f7e71
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
15 changes: 10 additions & 5 deletions crates/aiken-lang/src/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use self::{
lookup_data_type_by_tipo, modify_cyclic_calls, modify_self_calls, rearrange_list_clauses,
AssignmentProperties, ClauseProperties, CodeGenSpecialFuncs, CycleFunctionNames,
DataTypeKey, FunctionAccessKey, HoistableFunction, Variant, CONSTR_NOT_EMPTY,
INCORRECT_BOOLEAN, INCORRECT_CONSTR, LIST_NOT_EMPTY, TOO_MANY_ITEMS,
INCORRECT_CONSTR, LIST_NOT_EMPTY, TOO_MANY_ITEMS,
},
tree::{AirExpression, AirTree, TreePath},
};
Expand Down Expand Up @@ -1062,9 +1062,16 @@ impl<'a> CodeGenerator<'a> {

sequence.push(constructor_val);

let msg = get_src_code_by_span(
&props.module_name,
&props.location,
&self.module_src,
);

let assert_constr = AirTree::assert_constr_index(
index,
AirTree::local_var(&constructor_name, tipo.clone()),
msg,
);

sequence.push(assert_constr);
Expand Down Expand Up @@ -4442,15 +4449,13 @@ impl<'a> CodeGenerator<'a> {

arg_stack.push(term);
}
Air::AssertConstr { constr_index } => {
Air::AssertConstr { constr_index, msg } => {
let constr = arg_stack.pop().unwrap();

let mut term = arg_stack.pop().unwrap();

let trace_term = if self.tracing {
Term::Error.delayed_trace(Term::var(
self.special_functions.use_function(INCORRECT_CONSTR),
))
Term::Error.delayed_trace(Term::string(msg))
} else {
Term::Error
};
Expand Down
1 change: 1 addition & 0 deletions crates/aiken-lang/src/gen_uplc/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub enum Air {
},
AssertConstr {
constr_index: usize,
msg: String,
},
AssertBool {
is_true: bool,
Expand Down
6 changes: 5 additions & 1 deletion crates/aiken-lang/src/gen_uplc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub enum AirStatement {
AssertConstr {
constr_index: usize,
constr: Box<AirTree>,
msg: String,
},
AssertBool {
is_true: bool,
Expand Down Expand Up @@ -493,11 +494,12 @@ impl AirTree {
value: value.into(),
})
}
pub fn assert_constr_index(constr_index: usize, constr: AirTree) -> AirTree {
pub fn assert_constr_index(constr_index: usize, constr: AirTree, msg: String) -> AirTree {
AirTree::Statement {
statement: AirStatement::AssertConstr {
constr_index,
constr: constr.into(),
msg,
},
hoisted_over: None,
}
Expand Down Expand Up @@ -946,9 +948,11 @@ impl AirTree {
AirStatement::AssertConstr {
constr,
constr_index,
msg,
} => {
air_vec.push(Air::AssertConstr {
constr_index: *constr_index,
msg: msg.clone(),
});
constr.create_air_vec(air_vec);
}
Expand Down

0 comments on commit 80f7e71

Please sign in to comment.