1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

Merge pull request #823 from Zokrates/fail-on-assertion

Detect assertion failures at compile time on constant expressions
This commit is contained in:
Thibaut Schaeffer 2021-04-19 20:50:01 +02:00 committed by GitHub
commit f041822ad7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 3 deletions

View file

@ -0,0 +1 @@
Detect assertion failures at compile time on constant expressions

View file

@ -1,3 +1,3 @@
def main() -> ():
assert(1f == 2f)
assert(1f + 1f == 2f)
return

View file

@ -0,0 +1,3 @@
def main():
assert(1f == 2f)
return

View file

@ -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

View file

@ -2,5 +2,5 @@ def foo() -> field:
return 1
def main():
assert(foo() + (1 + 44*3) == 1)
assert(foo() + (1 + 44*3) == 134)
return

View file

@ -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:

View file

@ -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),