Skip to content

Commit

Permalink
Merge pull request #470 from jacqueswww/410_invalid_annotate_assign
Browse files Browse the repository at this point in the history
Add checks preventing assigning values on defining types.
  • Loading branch information
DavidKnott committed Nov 15, 2017
2 parents 0b9062b + b905905 commit 65845c6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/parser/exceptions/test_structure_exception.py
Expand Up @@ -131,6 +131,16 @@ def foo():
"""
def foo():
x = y = 3
""",
"""
def foo() -> num:
q:num = 111
return q
""",
"""
q:num = 111
def foo() -> num:
return self.q
"""
]

Expand Down
4 changes: 3 additions & 1 deletion viper/parser/parser.py
Expand Up @@ -171,7 +171,9 @@ def add_contract(code):


def add_globals_and_events(_defs, _events, _getters, _globals, item):
if isinstance(item.annotation, ast.Call) and item.annotation.func.id == "__log__":
if item.value is not None:
raise StructureException('May not assign value whilst defining type', item)
elif isinstance(item.annotation, ast.Call) and item.annotation.func.id == "__log__":
if _globals or len(_defs):
raise StructureException("Events must all come before global declarations and function definitions", item)
_events.append(item)
Expand Down
2 changes: 2 additions & 0 deletions viper/parser/stmt.py
Expand Up @@ -69,6 +69,8 @@ def parse_pass(self):
return LLLnode.from_list('pass', typ=None, pos=getpos(self.stmt))

def ann_assign(self):
if self.stmt.value is not None:
raise StructureException('May not assign value whilst defining type', self.stmt)
typ = parse_type(self.stmt.annotation, location='memory')
varname = self.stmt.target.id
pos = self.context.new_variable(varname, typ)
Expand Down

0 comments on commit 65845c6

Please sign in to comment.