Skip to content

Commit

Permalink
Ensure blocks in if/else + while are semantically checked
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Nov 17, 2020
1 parent 8e0866b commit 2bf3390
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions compiler/tests/evm_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ fn test_assert() {
case("if_statement.fe", vec![6], Some(u256_token(1))),
case("if_statement.fe", vec![4], Some(u256_token(0))),
case("if_statement_2.fe", vec![6], Some(u256_token(1))),
case("if_statement_with_block_declaration.fe", vec![], Some(u256_token(1))),
case("call_statement_without_args.fe", vec![], Some(u256_token(100))),
case("call_statement_with_args.fe", vec![], Some(u256_token(100))),
case("call_statement_with_args_2.fe", vec![], Some(u256_token(100))),
Expand Down
28 changes: 21 additions & 7 deletions semantics/src/traversal/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,25 @@ pub fn func_def(

context.borrow_mut().add_function(def, attributes);

for stmt in body.iter() {
func_stmt(Rc::clone(&function_scope), Rc::clone(&context), stmt)?
}
traverse_statements(function_scope.clone(), context, body)?;

return Ok(());
}

unreachable!()
}

fn traverse_statements(
scope: Shared<FunctionScope>,
context: Shared<Context>,
body: &[Spanned<fe::FuncStmt>],
) -> Result<(), SemanticError> {
for stmt in body.iter() {
func_stmt(Rc::clone(&scope), Rc::clone(&context), stmt)?
}
Ok(())
}

fn validate_all_paths_return_or_revert(
block: &[Spanned<fe::FuncStmt>],
) -> Result<(), SemanticError> {
Expand Down Expand Up @@ -183,9 +192,13 @@ fn if_statement(
match &stmt.node {
fe::FuncStmt::If {
test,
body: _,
or_else: _,
} => verify_is_boolean(scope, context, test),
body,
or_else,
} => {
traverse_statements(scope.clone(), context.clone(), body)?;
traverse_statements(scope.clone(), context.clone(), or_else)?;
verify_is_boolean(scope, context, test)
}
_ => unreachable!(),
}
}
Expand All @@ -198,12 +211,13 @@ fn while_loop(
match &stmt.node {
fe::FuncStmt::While {
test,
body: _,
body,
or_else,
} => {
if !or_else.is_empty() {
unimplemented!();
}
traverse_statements(scope.clone(), context.clone(), body)?;
verify_is_boolean(scope, context, test)
}
_ => unreachable!(),
Expand Down

0 comments on commit 2bf3390

Please sign in to comment.