Skip to content

Commit

Permalink
fix: typechecking of folded builtins (#3490)
Browse files Browse the repository at this point in the history
some builtins would allow decimals during typechecking and then panic
during codegen

---------

Co-authored-by: Tanguy Rocher <tanguy.rocher@protonmail.com>
  • Loading branch information
trocher and Tanguy Rocher authored Jul 1, 2023
1 parent 3d01e94 commit 29b02dd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
27 changes: 27 additions & 0 deletions tests/parser/syntax/test_addmulmod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from vyper.exceptions import InvalidType

fail_list = [
( # bad AST nodes given as arguments
"""
@external
def foo() -> uint256:
return uint256_addmod(1.1, 1.2, 3.0)
""",
InvalidType,
),
( # bad AST nodes given as arguments
"""
@external
def foo() -> uint256:
return uint256_mulmod(1.1, 1.2, 3.0)
""",
InvalidType,
),
]


@pytest.mark.parametrize("code,exc", fail_list)
def test_add_mod_fail(assert_compile_failed, get_contract, code, exc):
assert_compile_failed(lambda: get_contract(code), exc)
14 changes: 7 additions & 7 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ def evaluate(self, node):

validate_call_args(node, 2)
for arg in node.args:
if not isinstance(arg, vy_ast.Num):
if not isinstance(arg, vy_ast.Int):
raise UnfoldableNode
if arg.value < 0 or arg.value >= 2**256:
raise InvalidLiteral("Value out of range for uint256", arg)
Expand All @@ -1396,7 +1396,7 @@ def evaluate(self, node):

validate_call_args(node, 2)
for arg in node.args:
if not isinstance(arg, vy_ast.Num):
if not isinstance(arg, vy_ast.Int):
raise UnfoldableNode
if arg.value < 0 or arg.value >= 2**256:
raise InvalidLiteral("Value out of range for uint256", arg)
Expand All @@ -1422,7 +1422,7 @@ def evaluate(self, node):

validate_call_args(node, 2)
for arg in node.args:
if not isinstance(arg, vy_ast.Num):
if not isinstance(arg, vy_ast.Int):
raise UnfoldableNode
if arg.value < 0 or arg.value >= 2**256:
raise InvalidLiteral("Value out of range for uint256", arg)
Expand All @@ -1447,7 +1447,7 @@ def evaluate(self, node):
self.__class__._warned = True

validate_call_args(node, 1)
if not isinstance(node.args[0], vy_ast.Num):
if not isinstance(node.args[0], vy_ast.Int):
raise UnfoldableNode

value = node.args[0].value
Expand All @@ -1474,7 +1474,7 @@ def evaluate(self, node):
self.__class__._warned = True

validate_call_args(node, 2)
if [i for i in node.args if not isinstance(i, vy_ast.Num)]:
if [i for i in node.args if not isinstance(i, vy_ast.Int)]:
raise UnfoldableNode
value, shift = [i.value for i in node.args]
if value < 0 or value >= 2**256:
Expand Down Expand Up @@ -1522,10 +1522,10 @@ class _AddMulMod(BuiltinFunction):

def evaluate(self, node):
validate_call_args(node, 3)
if isinstance(node.args[2], vy_ast.Num) and node.args[2].value == 0:
if isinstance(node.args[2], vy_ast.Int) and node.args[2].value == 0:
raise ZeroDivisionException("Modulo by 0", node.args[2])
for arg in node.args:
if not isinstance(arg, vy_ast.Num):
if not isinstance(arg, vy_ast.Int):
raise UnfoldableNode
if arg.value < 0 or arg.value >= 2**256:
raise InvalidLiteral("Value out of range for uint256", arg)
Expand Down

0 comments on commit 29b02dd

Please sign in to comment.