Skip to content

Commit

Permalink
Merge pull request #1258 from charles-cooper/fix_valency
Browse files Browse the repository at this point in the history
Fix valency error in if-statements
  • Loading branch information
jacqueswww committed Feb 26, 2019
2 parents 0ebae4b + 09661c2 commit 028b8ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
27 changes: 27 additions & 0 deletions tests/parser/syntax/test_conditionals.py
@@ -0,0 +1,27 @@
import pytest
from vyper import compiler

valid_list = [
"""
@private
def mkint() -> int128:
return 1
@public
def test_zerovalent():
if True:
self.mkint()
@public
def test_valency_mismatch():
if True:
self.mkint()
else:
pass
"""
]


@pytest.mark.parametrize('good_code', valid_list)
def test_conditional_return_code(good_code):
assert compiler.compile_code(good_code) is not None
4 changes: 0 additions & 4 deletions vyper/parser/lll_node.py
Expand Up @@ -107,12 +107,8 @@ def __init__(self, value, args=None, typ=None, location=None, pos=None, annotati
elif self.value == 'if':
if len(self.args) == 3:
self.gas = self.args[0].gas + max(self.args[1].gas, self.args[2].gas) + 3
if self.args[1].valency != self.args[2].valency:
raise Exception("Valency mismatch between then and else clause: %r %r" % (self.args[1], self.args[2]))
if len(self.args) == 2:
self.gas = self.args[0].gas + self.args[1].gas + 17
if self.args[1].valency:
raise Exception("2-clause if statement must have a zerovalent body: %r" % self.args[1])
if not self.args[0].valency:
raise Exception("Can't have a zerovalent argument as a test to an if statement! %r" % self.args[0])
if len(self.args) not in (2, 3):
Expand Down
9 changes: 5 additions & 4 deletions vyper/parser/stmt.py
Expand Up @@ -252,7 +252,7 @@ def parse_if(self):
if self.stmt.orelse:
block_scope_id = id(self.stmt.orelse)
with self.context.make_blockscope(block_scope_id):
add_on = [parse_body(self.stmt.orelse, self.context)]
add_on = [['seq', parse_body(self.stmt.orelse, self.context)]]
else:
add_on = []

Expand All @@ -262,12 +262,13 @@ def parse_if(self):

if not self.is_bool_expr(test_expr):
raise TypeMismatchException('Only boolean expressions allowed', self.stmt.test)

body = ['if', test_expr,
['seq', parse_body(self.stmt.body, self.context)]] \
+ add_on
o = LLLnode.from_list(
['if', test_expr, parse_body(self.stmt.body, self.context)] + add_on,
body,
typ=None, pos=getpos(self.stmt)
)

return o

def _clear(self):
Expand Down

0 comments on commit 028b8ec

Please sign in to comment.