feat: add code messages when using expects on constrs

This commit is contained in:
microproofs 2024-01-02 21:59:22 -05:00 committed by Kasey
parent 71cfb6f6af
commit aa51ce3e3e
3 changed files with 16 additions and 6 deletions

View File

@ -45,7 +45,7 @@ use self::{
lookup_data_type_by_tipo, modify_cyclic_calls, modify_self_calls, rearrange_list_clauses, lookup_data_type_by_tipo, modify_cyclic_calls, modify_self_calls, rearrange_list_clauses,
AssignmentProperties, ClauseProperties, CodeGenSpecialFuncs, CycleFunctionNames, AssignmentProperties, ClauseProperties, CodeGenSpecialFuncs, CycleFunctionNames,
DataTypeKey, FunctionAccessKey, HoistableFunction, Variant, CONSTR_NOT_EMPTY, 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}, tree::{AirExpression, AirTree, TreePath},
}; };
@ -1062,9 +1062,16 @@ impl<'a> CodeGenerator<'a> {
sequence.push(constructor_val); 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( let assert_constr = AirTree::assert_constr_index(
index, index,
AirTree::local_var(&constructor_name, tipo.clone()), AirTree::local_var(&constructor_name, tipo.clone()),
msg,
); );
sequence.push(assert_constr); sequence.push(assert_constr);
@ -4442,15 +4449,13 @@ impl<'a> CodeGenerator<'a> {
arg_stack.push(term); arg_stack.push(term);
} }
Air::AssertConstr { constr_index } => { Air::AssertConstr { constr_index, msg } => {
let constr = arg_stack.pop().unwrap(); let constr = arg_stack.pop().unwrap();
let mut term = arg_stack.pop().unwrap(); let mut term = arg_stack.pop().unwrap();
let trace_term = if self.tracing { let trace_term = if self.tracing {
Term::Error.delayed_trace(Term::var( Term::Error.delayed_trace(Term::string(msg))
self.special_functions.use_function(INCORRECT_CONSTR),
))
} else { } else {
Term::Error Term::Error
}; };

View File

@ -89,6 +89,7 @@ pub enum Air {
}, },
AssertConstr { AssertConstr {
constr_index: usize, constr_index: usize,
msg: String,
}, },
AssertBool { AssertBool {
is_true: bool, is_true: bool,

View File

@ -127,6 +127,7 @@ pub enum AirStatement {
AssertConstr { AssertConstr {
constr_index: usize, constr_index: usize,
constr: Box<AirTree>, constr: Box<AirTree>,
msg: String,
}, },
AssertBool { AssertBool {
is_true: bool, is_true: bool,
@ -493,11 +494,12 @@ impl AirTree {
value: value.into(), 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 { AirTree::Statement {
statement: AirStatement::AssertConstr { statement: AirStatement::AssertConstr {
constr_index, constr_index,
constr: constr.into(), constr: constr.into(),
msg,
}, },
hoisted_over: None, hoisted_over: None,
} }
@ -946,9 +948,11 @@ impl AirTree {
AirStatement::AssertConstr { AirStatement::AssertConstr {
constr, constr,
constr_index, constr_index,
msg,
} => { } => {
air_vec.push(Air::AssertConstr { air_vec.push(Air::AssertConstr {
constr_index: *constr_index, constr_index: *constr_index,
msg: msg.clone(),
}); });
constr.create_air_vec(air_vec); constr.create_air_vec(air_vec);
} }