From b905905c483fb976cc1166b39088c0b02b03d6a4 Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Wed, 15 Nov 2017 16:03:05 +0200 Subject: [PATCH] Add checks preventing assigning values on defining types. --- tests/parser/exceptions/test_structure_exception.py | 10 ++++++++++ viper/parser/parser.py | 4 +++- viper/parser/stmt.py | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/parser/exceptions/test_structure_exception.py b/tests/parser/exceptions/test_structure_exception.py index 5591d98dc2..764aea7517 100644 --- a/tests/parser/exceptions/test_structure_exception.py +++ b/tests/parser/exceptions/test_structure_exception.py @@ -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 """ ] diff --git a/viper/parser/parser.py b/viper/parser/parser.py index 9e8394c5f5..ffcbcde519 100644 --- a/viper/parser/parser.py +++ b/viper/parser/parser.py @@ -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) diff --git a/viper/parser/stmt.py b/viper/parser/stmt.py index 635bcd54a8..a4715f4570 100644 --- a/viper/parser/stmt.py +++ b/viper/parser/stmt.py @@ -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)