Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Apr 19, 2021
1 parent 638df61 commit 9a69ccf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
11 changes: 8 additions & 3 deletions compiler/src/yul/mappers/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,15 @@ fn emit(context: &Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
}

fn assert(context: &Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
if let fe::FuncStmt::Assert { test, msg: _ } = &stmt.kind {
if let fe::FuncStmt::Assert { test, msg } = &stmt.kind {
let test = expressions::expr(context, test);

return statement! { if (iszero([test])) { (revert(0, 0)) } };
return match msg {
Some(val) => {
let msg = expressions::expr(context, val);
statement! { if (iszero([test])) { (revert_with_reason_string([msg])) } }
},
None => statement! { if (iszero([test])) { (revert(0, 0)) } }
}
}

unreachable!()
Expand Down
18 changes: 13 additions & 5 deletions compiler/tests/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,25 @@ fn test_assert() {

let exit1 = harness.capture_call(&mut executor, "bar", &[uint_token(4)]);

assert!(matches!(
exit1,
evm::Capture::Exit((evm::ExitReason::Revert(_), _))
));
match exit1 {
evm::Capture::Exit((evm::ExitReason::Revert(_), output)) => assert_eq!(output.len(), 0),
_ => panic!("Did not revert correctly")
}

let exit2 = harness.capture_call(&mut executor, "bar", &[uint_token(42)]);

assert!(matches!(
exit2,
evm::Capture::Exit((evm::ExitReason::Succeed(_), _))
))
));

let exit3 = harness.capture_call(&mut executor, "baz", &[uint_token(4)]);

match exit3 {
evm::Capture::Exit((evm::ExitReason::Revert(_), output)) => assert_eq!(output, encode_error_reason("Must be greater than five")),
_ => panic!("Did not revert correctly")
}

})
}

Expand Down
5 changes: 4 additions & 1 deletion compiler/tests/fixtures/features/assert.fe
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
contract Foo:
pub def bar(baz: u256):
assert baz > 5
assert baz > 5

pub def baz(baz: u256):
assert baz > 5, "Must be greater than five"

0 comments on commit 9a69ccf

Please sign in to comment.