Merge pull request #823 from Zokrates/fail-on-assertion
Detect assertion failures at compile time on constant expressions
This commit is contained in:
commit
f041822ad7
8 changed files with 25 additions and 3 deletions
1
changelogs/unreleased/823-dark64
Normal file
1
changelogs/unreleased/823-dark64
Normal file
|
@ -0,0 +1 @@
|
|||
Detect assertion failures at compile time on constant expressions
|
|
@ -1,3 +1,3 @@
|
|||
def main() -> ():
|
||||
assert(1f == 2f)
|
||||
assert(1f + 1f == 2f)
|
||||
return
|
3
zokrates_cli/examples/compile_errors/assertion.zok
Normal file
3
zokrates_cli/examples/compile_errors/assertion.zok
Normal file
|
@ -0,0 +1,3 @@
|
|||
def main():
|
||||
assert(1f == 2f)
|
||||
return
|
|
@ -0,0 +1,7 @@
|
|||
def foo<N>(field[N] inputs) -> bool:
|
||||
assert(N <= 5)
|
||||
return true
|
||||
|
||||
def main():
|
||||
bool b = foo([1, 2, 3, 4, 5, 6])
|
||||
return
|
|
@ -2,5 +2,5 @@ def foo() -> field:
|
|||
return 1
|
||||
|
||||
def main():
|
||||
assert(foo() + (1 + 44*3) == 1)
|
||||
assert(foo() + (1 + 44*3) == 134)
|
||||
return
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
def foo(field a, field b) -> (field, field):
|
||||
assert(a == b + 2)
|
||||
assert(a == b)
|
||||
return a, b
|
||||
|
||||
def main() -> field:
|
||||
|
|
|
@ -657,6 +657,17 @@ impl<'ast, 'a, T: Field> ResultFolder<'ast, T> for Propagator<'ast, 'a, T> {
|
|||
|
||||
Ok(statements)
|
||||
}
|
||||
TypedStatement::Assertion(e) => {
|
||||
let e_str = e.to_string();
|
||||
let expr = self.fold_boolean_expression(e)?;
|
||||
match expr {
|
||||
BooleanExpression::Value(v) if !v => Err(Error::Type(format!(
|
||||
"Assertion failed on expression `{}`",
|
||||
e_str
|
||||
))),
|
||||
_ => Ok(vec![TypedStatement::Assertion(expr)]),
|
||||
}
|
||||
}
|
||||
s @ TypedStatement::PushCallLog(..) => Ok(vec![s]),
|
||||
s @ TypedStatement::PopCallLog => Ok(vec![s]),
|
||||
s => fold_statement(self, s),
|
||||
|
|
Loading…
Reference in a new issue