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

fix: reorder AST folding after semantics validation #2832

Closed
wants to merge 185 commits into from
Closed
Changes from all commits
Commits
Show all changes
185 commits
Select commit Hold shift + click to select a range
44adac1
fix binop folding for constants with type def
tserg Apr 30, 2022
a077371
add binop tests
tserg Apr 30, 2022
4c5e4f6
add fuzzing marker for test
tserg Apr 30, 2022
b75f72d
improve type mismatch error message
tserg May 1, 2022
94ad4c3
add compare folding type checking and bounds checking
tserg May 1, 2022
37cccb3
add compare tests
tserg May 1, 2022
bf28688
revert exception type
tserg May 1, 2022
ef95b39
defer type mismatch to semantics stage
tserg May 1, 2022
4c7a11d
fix test
tserg May 1, 2022
93e5d9f
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg May 1, 2022
4f04feb
minor change to test
tserg May 2, 2022
02603bf
move typechecking to folding
tserg May 5, 2022
e77e9e1
fix type annotation and minor refactoring
tserg May 5, 2022
47cf12c
more refactoring
tserg May 5, 2022
8f388ce
undo comment delete
tserg May 5, 2022
ece28b4
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg Jun 7, 2022
b58f108
partially reorder folding
tserg Jun 8, 2022
27ed095
update tests
tserg Jun 8, 2022
a1a8f75
remove typedef import
tserg Jun 8, 2022
b28f228
wip: shift order
tserg Jun 9, 2022
6388073
fix decimal type
tserg Jun 9, 2022
1bd7aec
fix folding decimal tests
tserg Jun 9, 2022
af05570
fix lint
tserg Jun 9, 2022
18a88f1
update tests
tserg Jun 9, 2022
4a391a8
fix black lint
tserg Jun 9, 2022
7d7fdbb
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg Jun 9, 2022
2eb1204
fix: change folding order; add env constants to namespace
tserg Jun 10, 2022
e8bf86f
revert numeric bounds folding
tserg Jun 10, 2022
9693823
fix lint
tserg Jun 10, 2022
d16b48b
annotate keccak
tserg Jun 10, 2022
bc4ab08
wip: typechecking foldable nodes
tserg Jun 10, 2022
aba271a
fix missing arg
tserg Jun 10, 2022
129835d
fix methodid
tserg Jun 10, 2022
73e5855
fix derive
tserg Jun 10, 2022
00828c1
fix int256 tests
tserg Jun 10, 2022
0c20161
fix int128 tests
tserg Jun 10, 2022
a533b97
annotate as_wei_value folded node
tserg Jun 10, 2022
ab339a1
annotate floor folded
tserg Jun 10, 2022
1ba8817
fix decimal tests
tserg Jun 10, 2022
40d2bad
fix constants
tserg Jun 10, 2022
ff92f85
fix validate_foldable
tserg Jun 10, 2022
cba4d62
fix: add vyper module
tserg Jun 10, 2022
fd645b7
fix decimal folding tests
tserg Jun 10, 2022
089a076
fix integer folding tests
tserg Jun 10, 2022
598d6b1
wip fix constants
tserg Jun 10, 2022
13ef8b8
fix raw_call; remove vyper module for get constant value
tserg Jun 10, 2022
b7b8fcf
fix dyn array tests
tserg Jun 10, 2022
9c2dda3
fix static array tests
tserg Jun 10, 2022
95300fa
fix constant structs
tserg Jun 10, 2022
70b3f88
fix constant nested structs
tserg Jun 10, 2022
094abfd
fix ceil
tserg Jun 10, 2022
0f27ef2
add comments
tserg Jun 10, 2022
dd4af18
skip mypy lint
tserg Jun 10, 2022
073a1c6
fix minmax
tserg Jun 10, 2022
c8b9c72
fix constant structs
tserg Jun 10, 2022
efe3366
fix exceptions and constancy check
tserg Jun 10, 2022
976bc51
fix interfaces int256 bounds test
tserg Jun 10, 2022
cd8f47b
fix constant annotation and test
tserg Jun 10, 2022
51e804a
fix check_kwargable
tserg Jun 10, 2022
48dc2fb
fix env constants derivation; refactor
tserg Jun 10, 2022
2c15d14
fix lint
tserg Jun 10, 2022
93c4f0f
fix dynarray size
tserg Jun 10, 2022
6c8242b
add not_assignable; fix for range with calldata
tserg Jun 11, 2022
5e9d6ae
fix lint
tserg Jun 11, 2022
3f97ab9
fix not_assignable order for arraydef
tserg Jun 11, 2022
eea8c82
fix tests and compare folding
tserg Jun 11, 2022
dd33bce
fix get_constant_value gets constant defs
tserg Jun 11, 2022
388a268
fix is_constant flag for env vars
tserg Jun 11, 2022
78da052
refactor builtin constants import
tserg Jun 11, 2022
a4a048c
convert builtin constants value to dict
tserg Jun 12, 2022
f5df60b
remove redundant constant validation in local
tserg Jun 12, 2022
da5567f
fix lint; revert methodid derive
tserg Jun 12, 2022
795571e
fix kwargable
tserg Jun 12, 2022
fd234d5
fix abs; add tests
tserg Jun 12, 2022
53b458e
refactor minmax; add tests
tserg Jun 12, 2022
2256d82
refactor as_wei_value; add tests
tserg Jun 12, 2022
daa6298
refactor bitwise fns; update tests
tserg Jun 12, 2022
aab7cad
fix bitwise and shift; update tests
tserg Jun 12, 2022
c09912d
fix modmath; add tests
tserg Jun 13, 2022
3cdb928
refactor abs
tserg Jun 13, 2022
59188a2
fix floor ceil tests
tserg Jun 13, 2022
bfb6faf
fix builtin functions numeric literals; tests
tserg Jun 13, 2022
142480f
add binop tests; fix asweivalue
tserg Jun 13, 2022
3663526
fix literal value derivation
tserg Jun 13, 2022
2a7eff5
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg Jun 13, 2022
0cbb9a0
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg Jul 6, 2022
4f5b494
fix merge conflict
tserg Jul 6, 2022
108449e
fix dynarray from_annotation
tserg Jul 6, 2022
b924083
fix merge conflicts
tserg Jul 7, 2022
82597a4
check foldable kwargs
tserg Jul 7, 2022
5b273b7
fix lint
tserg Jul 7, 2022
50553f2
refactor various derivation helpers
tserg Jul 7, 2022
c38f8c0
refactor check_kwargable and check_constant'
tserg Jul 7, 2022
446e7b1
refactor local visit_For
tserg Jul 7, 2022
65ba7b2
fix outdated code
tserg Jul 7, 2022
4d0d1ed
fix lint
tserg Jul 7, 2022
3d2428c
revert wrong deletion of struct branch for check_constant and check_k…
tserg Jul 7, 2022
27dff52
fix lint
tserg Jul 7, 2022
0ef74c3
refactor local validation of for loop
tserg Jul 7, 2022
cb41e50
refactor dynarray from_annotation
tserg Jul 7, 2022
d2a8c09
remove folding from unfolded ast mode
tserg Jul 7, 2022
a001b50
refactor get_index_value
tserg Jul 7, 2022
2d58223
fix lint
tserg Jul 7, 2022
de97286
fix get_index_value
tserg Jul 7, 2022
10e781b
add test for foldable minmax in constants
tserg Jul 8, 2022
2ea9694
aggressive refactoring of nodes
tserg Jul 8, 2022
be5ddef
fix lint
tserg Jul 8, 2022
b8b45f7
handle bitwise ops for decimals
tserg Jul 8, 2022
9c71cdd
fix lint
tserg Jul 8, 2022
1e52028
refactor process_kwarg condition for require_literal
tserg Jul 8, 2022
0601bf2
fix lint
tserg Jul 8, 2022
d1fec0e
refactor derive for compare
tserg Jul 8, 2022
08b8780
remove validation in nodes
tserg Jul 8, 2022
4b06676
remove annotate_foldable_literals
tserg Jul 8, 2022
b35bd75
remove annotate_foldable_minmax
tserg Jul 8, 2022
6f642a0
remove derive from nodes
tserg Jul 8, 2022
c72a88c
add evaluate for name
tserg Jul 9, 2022
743362a
refactor binops; add name and call
tserg Jul 9, 2022
7075549
remove foldable minmax
tserg Jul 9, 2022
14a2264
fix lint
tserg Jul 9, 2022
190521c
remove foldable numeric literal
tserg Jul 9, 2022
dfb3d6c
rename not_assignable to is_assignable
tserg Jul 9, 2022
f40637f
fix mypy lint
tserg Jul 9, 2022
44e9706
remove zero division check in types_from_BinOp
tserg Jul 9, 2022
ef7b777
add validate_expr
tserg Jul 9, 2022
aedd43e
refactor binop folding tests
tserg Jul 9, 2022
a4974cf
fix lint
tserg Jul 9, 2022
c6aadfd
refactor ast nodes folding tests
tserg Jul 9, 2022
c234883
fix visit_Compare
tserg Jul 9, 2022
4b6fa48
fix annotation tests
tserg Jul 9, 2022
75c1dd7
fix visit_Call
tserg Jul 9, 2022
6b4cdd0
wip refactoring and fixes
tserg Jul 9, 2022
cc96c55
wip fixes
tserg Jul 9, 2022
ef3f93b
Merge branch 'master' into fix/constant_typecheck
tserg Jul 9, 2022
b5d0bc7
fix lint
tserg Jul 9, 2022
f30eac9
fix semantics
tserg Jul 9, 2022
1f593d0
fix zerodivision
tserg Jul 9, 2022
5845b13
fix folding and semantics wip
tserg Jul 9, 2022
0c0b107
fix Name node evaluate
tserg Jul 9, 2022
ee61f2f
fix visit_Int annotation
tserg Jul 9, 2022
08bf5ab
fix visit_For in local
tserg Jul 9, 2022
207f8d3
fix Name node evaluate
tserg Jul 9, 2022
071ecbe
fix lint
tserg Jul 9, 2022
0bc5562
revert changes for folding tests
tserg Jul 9, 2022
085e612
revert folding functions tests
tserg Jul 9, 2022
e14a5cd
remove deleted tests
tserg Jul 9, 2022
c27d1fa
fix Call evaluate
tserg Jul 9, 2022
e1d1924
fix validate_numeric bounds; type annotation in evaluating nodes
tserg Jul 9, 2022
d5f2c06
fix folding tests; division by zero condition
tserg Jul 9, 2022
4129cb6
increase duration for folding of compare in and notin
tserg Jul 10, 2022
83cf7c5
remove validate_expr from vyper module
tserg Jul 10, 2022
a308b96
remove annotate_struct_members
tserg Jul 10, 2022
7c47573
fix lint
tserg Jul 10, 2022
7e4f60a
fix get_index_value
tserg Jul 10, 2022
be09ea7
revert tests
tserg Jul 10, 2022
c540067
update folding of builtin functions
tserg Jul 10, 2022
98507c2
revert builtin functions folding; add validate_expr for constant structs
tserg Jul 10, 2022
244da16
fix lint
tserg Jul 10, 2022
d9837de
remove dead code from types_from_Subscript
tserg Jul 10, 2022
e6895a5
update raw_call
tserg Jul 10, 2022
13874f1
remove dead code for annotation visit_Subscript
tserg Jul 10, 2022
ec713e7
fix unaryop evaluate
tserg Jul 10, 2022
0927402
add division by zero example to hypothesis
tserg Jul 10, 2022
028b37a
add shift folding tests
tserg Jul 11, 2022
38c472c
remove duplicated code in minmax fetch_call_return
tserg Jul 11, 2022
3ffe9bf
remove dead code in minmax
tserg Jul 11, 2022
2e4ef7c
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg Jul 11, 2022
f4d0bbb
add zero division test for decimal folding
tserg Jul 11, 2022
eb67935
add as_wei_value test
tserg Jul 11, 2022
18aa2eb
move zero division check to binop for unfoldable ops
tserg Jul 11, 2022
b1e54da
remove unused import
tserg Jul 11, 2022
33e7169
update abs folding tests
tserg Jul 11, 2022
35b9f10
fix shift folding test name
tserg Jul 11, 2022
cbd7c77
fix lint
tserg Jul 11, 2022
dcb9655
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg Jul 14, 2022
a62b051
Merge branch 'master' of https://github.com/vyperlang/vyper into fix/…
tserg Jul 28, 2022
291b122
fix folding of multiple constant ops within a constant def
tserg Jul 28, 2022
fda0ee4
fix merge conflict for min_value and max_value
tserg Jul 28, 2022
88fa2ec
fix lint
tserg Jul 28, 2022
2e1abfe
fix minmax oob failing tests
tserg Jul 28, 2022
7ec96af
fix shift return type
tserg Jul 28, 2022
9ea1134
fix complex constants folding
tserg Jul 28, 2022
74eb601
fix exception type for failing test
tserg Jul 28, 2022
11cbfde
fix mypy lint
tserg Jul 28, 2022
2001f11
fix constant structs
tserg Jul 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/ast/nodes/test_evaluate_binop_decimal.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
@example(left=Decimal("0.0000000001"), right=Decimal("0.9999999999"))
@example(left=Decimal("0.9999999999"), right=Decimal("0.9999999999"))
@example(left=Decimal("0.0000000001"), right=Decimal("0.0000000001"))
@example(left=Decimal("0.0000000001"), right=Decimal("0.0000000000"))
@pytest.mark.parametrize("op", "+-*/%")
def test_binop_decimal(get_contract, assert_tx_failed, op, left, right):
source = f"""
1 change: 1 addition & 0 deletions tests/ast/nodes/test_evaluate_binop_int.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
@example(left=1, right=-1)
@example(left=-1, right=1)
@example(left=-1, right=-1)
@example(left=1, right=0)
@pytest.mark.parametrize("op", "+-*/%")
def test_binop_int128(get_contract, assert_tx_failed, op, left, right):
source = f"""
4 changes: 2 additions & 2 deletions tests/ast/nodes/test_evaluate_compare.py
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ def foo(a: uint128, b: uint128) -> bool:


@pytest.mark.fuzzing
@settings(max_examples=20, deadline=500)
@settings(max_examples=20, deadline=2500)
@given(left=st.integers(), right=st.lists(st.integers(), min_size=1, max_size=16))
def test_compare_in(left, right, get_contract):
source = f"""
@@ -66,7 +66,7 @@ def foo(a: int128, b: int128[{len(right)}]) -> bool:


@pytest.mark.fuzzing
@settings(max_examples=20, deadline=500)
@settings(max_examples=20, deadline=2500)
@given(left=st.integers(), right=st.lists(st.integers(), min_size=1, max_size=16))
def test_compare_not_in(left, right, get_contract):
source = f"""
11 changes: 2 additions & 9 deletions tests/functional/context/types/test_type_from_annotation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import pytest

from vyper.exceptions import (
ArrayIndexException,
InvalidType,
StructureException,
UndeclaredDefinition,
)
from vyper.exceptions import ArrayIndexException, InvalidType, StructureException
from vyper.semantics.types import get_primitive_types
from vyper.semantics.types.bases import DataLocation
from vyper.semantics.types.indexable.mapping import MappingDefinition
@@ -74,9 +69,7 @@ def test_base_types_as_multidimensional_arrays(build_node, namespace, type_str):
@pytest.mark.parametrize("idx", ["0", "-1", "0x00", "'1'", "foo", "[1]", "(1,)"])
def test_invalid_index(build_node, idx, type_str):
node = build_node(f"{type_str}[{idx}]")
with pytest.raises(
(ArrayIndexException, InvalidType, StructureException, UndeclaredDefinition)
):
with pytest.raises((ArrayIndexException, InvalidType)):
get_type_from_annotation(node, DataLocation.STORAGE)


23 changes: 15 additions & 8 deletions tests/functions/folding/test_abs.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

from vyper import ast as vy_ast
from vyper import builtin_functions as vy_fn
from vyper.exceptions import OverflowException
from vyper.exceptions import InvalidType, OverflowException


@pytest.mark.fuzzing
@@ -29,14 +29,22 @@ def foo(a: int256) -> int256:
@pytest.mark.fuzzing
@settings(max_examples=50, deadline=1000)
@given(a=st.integers(min_value=2 ** 255, max_value=2 ** 256 - 1))
def test_abs_upper_bound_folding(get_contract, a):
def test_abs_upper_bound_folded_1(get_contract, assert_compile_failed, a):
source = f"""
@external
def foo(a: int256) -> int256:
def foo() -> int256:
return abs({a})
"""
with pytest.raises(OverflowException):
get_contract(source)
assert_compile_failed(lambda: get_contract(source), InvalidType)


def test_abs_upper_bound_folded_2(get_contract, assert_compile_failed):
source = """
@external
def foo() -> int256:
return abs(2**256)
"""
assert_compile_failed(lambda: get_contract(source), OverflowException)


def test_abs_lower_bound(get_contract, assert_tx_failed):
@@ -50,11 +58,10 @@ def foo(a: int256) -> int256:
assert_tx_failed(lambda: contract.foo(-(2 ** 255)))


def test_abs_lower_bound_folded(get_contract, assert_tx_failed):
def test_abs_lower_bound_folded(get_contract, assert_compile_failed):
source = """
@external
def foo() -> int256:
return abs(-2**255)
"""
with pytest.raises(OverflowException):
get_contract(source)
assert_compile_failed(lambda: get_contract(source), OverflowException)
27 changes: 27 additions & 0 deletions tests/functions/folding/test_shift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from hypothesis import given, settings
from hypothesis import strategies as st

from vyper import ast as vy_ast
from vyper import builtin_functions as vy_fn

st_uint256 = st.integers(min_value=0, max_value=256)
st_int128 = st.integers(min_value=-128, max_value=127)


@pytest.mark.fuzzing
@settings(max_examples=100, deadline=1000)
@given(a=st_uint256, b=st_int128)
def test_shift_uint256(get_contract, a, b):
source = """
@external
def foo(a: uint256, b: int128) -> uint256:
return shift(a, b)
"""
contract = get_contract(source)

vyper_ast = vy_ast.parse_to_ast(f"shift({a}, {b})")
old_node = vyper_ast.body[0].value
new_node = vy_fn.Shift().evaluate(old_node)

assert contract.foo(a, b) == new_node.value
5 changes: 0 additions & 5 deletions tests/parser/exceptions/test_argument_exception.py
Original file line number Diff line number Diff line change
@@ -6,11 +6,6 @@
fail_list = [
"""
@external
def foo():
x = as_wei_value(5, "vader")
""",
"""
@external
def foo(x: int128, x: int128): pass
""",
"""
5 changes: 5 additions & 0 deletions tests/parser/exceptions/test_undeclared_definition.py
Original file line number Diff line number Diff line change
@@ -61,6 +61,11 @@ def foo():
def foo():
bork = zork
""",
"""
@external
def foo():
x = as_wei_value(5, "vader")
""",
]


Original file line number Diff line number Diff line change
@@ -379,7 +379,7 @@ def test_int128_too_long(get_contract, assert_tx_failed):
contract_1 = """
@external
def foo() -> int256:
return (2**255)-1
return 2**254
"""

c = get_contract(contract_1)
8 changes: 6 additions & 2 deletions tests/parser/features/iteration/test_for_in_list.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
InvalidType,
IteratorException,
NamespaceCollision,
OverflowException,
StateAccessViolation,
StructureException,
TypeMismatch,
@@ -686,13 +687,16 @@ def foo():
""",
StateAccessViolation,
),
"""
(
"""
@external
def foo():
a: int128 = 6
for i in range(a,a-3):
pass
""",
StructureException,
),
# invalid argument length
(
"""
@@ -758,7 +762,7 @@ def test_for() -> int128:
a = i
return a
""",
TypeMismatch,
OverflowException,
),
(
"""
73 changes: 72 additions & 1 deletion tests/parser/functions/test_bitwise.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

from vyper.compiler import compile_code
from vyper.evm.opcodes import EVM_VERSIONS
from vyper.exceptions import TypeMismatch
from vyper.exceptions import InvalidLiteral, InvalidType, OverflowException, TypeMismatch

code = """
@external
@@ -136,10 +136,81 @@ def foo(x: uint8, y: int128) -> uint256:
return shift(x, y)
""",
TypeMismatch,
),
(
"""
@external
def foo():
a: uint256 = shift(
115792089237316195423570985008687907853269984665640564039457584007913129639936,
2
)
""",
OverflowException,
),
(
"""
@external
def foo():
a: uint256 = shift(-1, 2)
""",
TypeMismatch,
),
(
"""
@external
def foo():
a: uint256 = shift(1, 170141183460469231731687303715884105728)
""",
InvalidLiteral,
),
]


@pytest.mark.parametrize("bad_code,exc", fail_list)
def test_shift_fail(get_contract_with_gas_estimation, bad_code, exc, assert_compile_failed):
assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc)


@pytest.mark.parametrize("op", ["bitwise_and", "bitwise_or", "bitwise_xor"])
def test_bitwise_fail(get_contract_with_gas_estimation, assert_compile_failed, op):
# 2 ** 256 is out of all numeric bounds
c1 = f"""
@external
def foo():
a: uint256 = {op}(
1,
115792089237316195423570985008687907853269984665640564039457584007913129639936
)
"""

# Negative integers are not allowed
c2 = f"""
@external
def foo():
a: uint256 = {op}(-1, 1)
"""

assert_compile_failed(lambda: get_contract_with_gas_estimation(c1), OverflowException)
assert_compile_failed(lambda: get_contract_with_gas_estimation(c2), InvalidType)


def test_bitwise_not_fail(get_contract_with_gas_estimation, assert_compile_failed):
# 2 ** 256 is out of all numeric bounds
c1 = """
@external
def foo():
a: uint256 = bitwise_not(
115792089237316195423570985008687907853269984665640564039457584007913129639936
)
"""

# Negative integers are not allowed
c2 = """
@external
def foo():
a: uint256 = bitwise_not(-1)
"""

assert_compile_failed(lambda: get_contract_with_gas_estimation(c1), OverflowException)
assert_compile_failed(lambda: get_contract_with_gas_estimation(c2), InvalidType)
4 changes: 2 additions & 2 deletions tests/parser/functions/test_interfaces.py
Original file line number Diff line number Diff line change
@@ -318,7 +318,7 @@ def ok() -> {typ}:

@external
def should_fail() -> int256:
return -2**255 # OOB for all int/uint types with less than 256 bits
return -2**254 # OOB for all int/uint types with less than 256 bits
"""

code = f"""
@@ -358,7 +358,7 @@ def test_fail3() -> int256:
interface_codes={"BadCode": {"type": "vyper", "code": external_contract}},
)
assert bad_c.ok() == 1
assert bad_c.should_fail() == -(2 ** 255)
assert bad_c.should_fail() == -(2 ** 254)

assert c.test_ok() == 1
assert_tx_failed(lambda: c.test_fail())
16 changes: 16 additions & 0 deletions tests/parser/functions/test_minmax.py
Original file line number Diff line number Diff line change
@@ -259,3 +259,19 @@ def foo4() -> int128:
assert c.foo2() == -(2 ** 127)
assert c.foo3() == 2 ** 127 - 1
assert c.foo4() == 2 ** 127 - 1


def test_minmax_in_constant(get_contract_with_gas_estimation):
code = """
a: constant(uint256) = max(10, 20)
b: constant(uint256) = min(10, 20)
c: constant(uint256) = max(min(10, 20), max(30, 40))
d: constant(uint256) = max(10, 20 + 57)
e: constant(uint256) = min(10 - 5, 7)

@external
def foo() -> (uint256, uint256, uint256, uint256, uint256):
return a, b, c, d, e
"""
c = get_contract_with_gas_estimation(code)
assert c.foo() == [20, 10, 40, 77, 5]
13 changes: 3 additions & 10 deletions tests/parser/functions/test_minmax_value.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
parse_decimal_info,
parse_integer_typeinfo,
)
from vyper.exceptions import InvalidType, OverflowException
from vyper.exceptions import OverflowException
from vyper.utils import int_bounds


@@ -42,15 +42,8 @@ def foo():
a: {typ} = min_value({typ}) - 1
"""

if typ == "uint256":
assert_compile_failed(lambda: get_contract(upper), OverflowException)
else:
assert_compile_failed(lambda: get_contract(upper), InvalidType)

if typ == "int256":
assert_compile_failed(lambda: get_contract(lower), OverflowException)
else:
assert_compile_failed(lambda: get_contract(lower), InvalidType)
assert_compile_failed(lambda: get_contract(upper), OverflowException)
assert_compile_failed(lambda: get_contract(lower), OverflowException)


@pytest.mark.parametrize("typ", sorted(DECIMAL_TYPES))
4 changes: 2 additions & 2 deletions tests/parser/functions/test_unary.py
Original file line number Diff line number Diff line change
@@ -68,11 +68,11 @@ def bar() -> decimal:

def test_negation_int128(get_contract):
code = """
a: constant(int128) = -2**127
a: constant(int128) = MIN_INT128

@external
def foo() -> int128:
return -2**127
return MIN_INT128

@external
def bar() -> int128:
Loading