Merge pull request #1032 from Zokrates/fail-early-on-complex-value-inequality
Fail at compile time on unsatisfied equality of complex values
This commit is contained in:
commit
f780b49aa2
5 changed files with 44 additions and 2 deletions
1
changelogs/unreleased/1032-schaeff
Normal file
1
changelogs/unreleased/1032-schaeff
Normal file
|
@ -0,0 +1 @@
|
|||
Fail at compile time when complex types are known not to be equal
|
|
@ -0,0 +1,3 @@
|
|||
def main():
|
||||
assert([1f] == [2f])
|
||||
return
|
|
@ -2,8 +2,8 @@ use crate::zir::result_folder::fold_statement;
|
|||
use crate::zir::result_folder::ResultFolder;
|
||||
use crate::zir::types::UBitwidth;
|
||||
use crate::zir::{
|
||||
BooleanExpression, FieldElementExpression, Identifier, UExpression, UExpressionInner,
|
||||
ZirExpression, ZirProgram, ZirStatement,
|
||||
BooleanExpression, FieldElementExpression, Identifier, RuntimeError, UExpression,
|
||||
UExpressionInner, ZirExpression, ZirProgram, ZirStatement,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
|
@ -15,6 +15,7 @@ type Constants<'ast, T> = HashMap<Identifier<'ast>, ZirExpression<'ast, T>>;
|
|||
pub enum Error {
|
||||
OutOfBounds(u128, u128),
|
||||
DivisionByZero,
|
||||
AssertionFailed(RuntimeError),
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
|
@ -28,6 +29,7 @@ impl fmt::Display for Error {
|
|||
Error::DivisionByZero => {
|
||||
write!(f, "Division by zero detected in zir during static analysis",)
|
||||
}
|
||||
Error::AssertionFailed(err) => write!(f, "{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,6 +55,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for ZirPropagator<'ast, T> {
|
|||
match s {
|
||||
ZirStatement::Assertion(e, error) => match self.fold_boolean_expression(e)? {
|
||||
BooleanExpression::Value(true) => Ok(vec![]),
|
||||
BooleanExpression::Value(false) => Err(Error::AssertionFailed(error)),
|
||||
e => Ok(vec![ZirStatement::Assertion(e, error)]),
|
||||
},
|
||||
ZirStatement::Definition(a, e) => {
|
||||
|
|
32
zokrates_core_test/tests/tests/assert_array_equality.json
Normal file
32
zokrates_core_test/tests/tests/assert_array_equality.json
Normal file
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"entry_point": "./tests/tests/assert_array_equality.zok",
|
||||
"curves": ["Bn128", "Bls12_381", "Bls12_377", "Bw6_761"],
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": ["1", "2"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": []
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["1", "1"]
|
||||
},
|
||||
"output": {
|
||||
"Err": {
|
||||
"UnsatisfiedConstraint": {
|
||||
"left": "0",
|
||||
"right": "1",
|
||||
"error": {
|
||||
"SourceAssertion": "Assertion failed at ./tests/tests/assert_array_equality.zok:2:2"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
3
zokrates_core_test/tests/tests/assert_array_equality.zok
Normal file
3
zokrates_core_test/tests/tests/assert_array_equality.zok
Normal file
|
@ -0,0 +1,3 @@
|
|||
def main(field[2] a):
|
||||
assert(a == [1, 2])
|
||||
return
|
Loading…
Reference in a new issue