Skip to content

Commit

Permalink
Partial fix for vyperlang#590. Adds check for a minimum of one return…
Browse files Browse the repository at this point in the history
… statement.
  • Loading branch information
jacqueswww committed Dec 19, 2017
1 parent c5a4a7a commit c5a3b22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions viper/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,18 @@ def __init__(self, vars=None, globals=None, sigs=None, forvars=None, return_type
self.origcode = origcode
# In Loop status. Whether body is currently evaluating within a for-loop or not.
self.in_for_loop = set()
# Count returns in function
self.function_return_count = 0

def set_in_for_loop(self, name_of_list):
self.in_for_loop.add(name_of_list)

def remove_in_for_loop(self, name_of_list):
self.in_for_loop.remove(name_of_list)

def increment_return_counter(self):
self.function_return_count += 1

# Add a new variable
def new_variable(self, name, typ):
if not is_varname_valid(name):
Expand Down Expand Up @@ -470,6 +475,13 @@ def parse_func(code, _globals, sigs, origcode, _vars=None):
['eq', ['mload', 0], method_id_node],
['seq'] + clampers + [parse_body(c, context) for c in code.body] + ['stop']
], typ=None, pos=getpos(code))

# Check for at leasts one return statement if necessary.
if context.return_type and context.function_return_count == 0:
raise StructureException(
"Missing return statement in function '%s' " % sig.name, code
)

o.context = context
o.total_gas = o.gas + calc_mem_gas(o.context.next_mem)
o.func_name = sig.name
Expand Down
1 change: 1 addition & 0 deletions viper/parser/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def parse_return(self):
if not self.stmt.value:
raise TypeMismatchException("Expecting to return a value", self.stmt)
sub = Expr(self.stmt.value, self.context).lll_node
self.context.increment_return_counter()
# Returning a value (most common case)
if isinstance(sub.typ, BaseType):
if not isinstance(self.context.return_type, BaseType):
Expand Down

0 comments on commit c5a3b22

Please sign in to comment.