Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks preventing assigning values on defining types. #470

Merged
merged 1 commit into from Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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