Skip to content

Commit

Permalink
feat: remove builtin constants (vyperlang#3350)
Browse files Browse the repository at this point in the history
Builtin constants like `MAX_INT128`, `MIN_INT128`, `MIN_DECIMAL`,
`MAX_DECIMAL` and `MAX_UINT256` have been deprecated since v0.3.4 with
the introduction of the `min_value` and `max_value` builtins, and
further back for `ZERO_ADDRESS` and `EMPTY_BYTES32` with the `empty`
builtin.

This PR removes them from the language entirely, and will be a breaking
change.
  • Loading branch information
tserg authored Oct 17, 2023
1 parent 74a8e02 commit 5482bbc
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 148 deletions.
43 changes: 0 additions & 43 deletions tests/ast/test_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,49 +132,6 @@ def test_replace_constant_no(source):
assert vy_ast.compare_nodes(unmodified_ast, folded_ast)


builtins_modified = [
"ZERO_ADDRESS",
"foo = ZERO_ADDRESS",
"foo: int128[ZERO_ADDRESS] = 42",
"foo = [ZERO_ADDRESS]",
"def foo(bar: address = ZERO_ADDRESS): pass",
"def foo(): bar = ZERO_ADDRESS",
"def foo(): return ZERO_ADDRESS",
"log foo(ZERO_ADDRESS)",
"log foo(42, ZERO_ADDRESS)",
]


@pytest.mark.parametrize("source", builtins_modified)
def test_replace_builtin_constant(source):
unmodified_ast = vy_ast.parse_to_ast(source)
folded_ast = vy_ast.parse_to_ast(source)

folding.replace_builtin_constants(folded_ast)

assert not vy_ast.compare_nodes(unmodified_ast, folded_ast)


builtins_unmodified = [
"ZERO_ADDRESS = 2",
"ZERO_ADDRESS()",
"def foo(ZERO_ADDRESS: int128 = 42): pass",
"def foo(): ZERO_ADDRESS = 42",
"def ZERO_ADDRESS(): pass",
"log ZERO_ADDRESS(42)",
]


@pytest.mark.parametrize("source", builtins_unmodified)
def test_replace_builtin_constant_no(source):
unmodified_ast = vy_ast.parse_to_ast(source)
folded_ast = vy_ast.parse_to_ast(source)

folding.replace_builtin_constants(folded_ast)

assert vy_ast.compare_nodes(unmodified_ast, folded_ast)


userdefined_modified = [
"FOO",
"foo = FOO",
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/features/decorators/test_private.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test(a: bytes32) -> (bytes32, uint256, int128):
b: uint256 = 1
c: int128 = 1
d: int128 = 123
f: bytes32 = EMPTY_BYTES32
f: bytes32 = empty(bytes32)
f, b, c = self._test(a)
assert d == 123
return f, b, c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,9 @@ def foo() -> (address, Bytes[3], address): view
@external
def bar(arg1: address) -> (address, Bytes[3], address):
a: address = ZERO_ADDRESS
a: address = empty(address)
b: Bytes[3] = b""
c: address = ZERO_ADDRESS
c: address = empty(address)
a, b, c = Foo(arg1).foo()
return a, b, c
"""
Expand Down Expand Up @@ -808,9 +808,9 @@ def foo() -> (address, Bytes[3], address): view
@external
def bar(arg1: address) -> (address, Bytes[3], address):
a: address = ZERO_ADDRESS
a: address = empty(address)
b: Bytes[3] = b""
c: address = ZERO_ADDRESS
c: address = empty(address)
a, b, c = Foo(arg1).foo()
return a, b, c
"""
Expand Down Expand Up @@ -841,9 +841,9 @@ def foo() -> (address, Bytes[3], address): view
@external
def bar(arg1: address) -> (address, Bytes[3], address):
a: address = ZERO_ADDRESS
a: address = empty(address)
b: Bytes[3] = b""
c: address = ZERO_ADDRESS
c: address = empty(address)
a, b, c = Foo(arg1).foo()
return a, b, c
"""
Expand Down Expand Up @@ -1538,7 +1538,7 @@ def out_literals() -> (int128, address, Bytes[10]) : view
@external
def test(addr: address) -> (int128, address, Bytes[10]):
a: int128 = 0
b: address = ZERO_ADDRESS
b: address = empty(address)
c: Bytes[10] = b""
(a, b, c) = Test(addr).out_literals()
return a, b,c
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/features/iteration/test_for_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def iterate_return_second() -> address:
count += 1
if count == 2:
return i
return ZERO_ADDRESS
return empty(address)
"""

c = get_contract_with_gas_estimation(code)
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/features/test_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def foo():
@external
def foo():
y: int128 = 1
z: bytes32 = EMPTY_BYTES32
z: bytes32 = empty(bytes32)
z = y
""",
"""
Expand All @@ -344,7 +344,7 @@ def foo():
@external
def foo():
y: uint256 = 1
z: bytes32 = EMPTY_BYTES32
z: bytes32 = empty(bytes32)
z = y
""",
],
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/features/test_memory_dealloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def sendit(): nonpayable
@external
def foo(target: address) -> uint256[2]:
log Shimmy(ZERO_ADDRESS, 3)
log Shimmy(empty(address), 3)
amount: uint256 = 1
flargen: uint256 = 42
Other(target).sendit()
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/functions/test_abi_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_abi_decode_complex(get_contract):
@external
def abi_decode(x: Bytes[160]) -> (address, int128, bool, decimal, bytes32):
a: address = ZERO_ADDRESS
a: address = empty(address)
b: int128 = 0
c: bool = False
d: decimal = 0.0
Expand All @@ -39,7 +39,7 @@ def abi_decode_struct(x: Bytes[544]) -> Human:
name: "",
pet: Animal({
name: "",
address_: ZERO_ADDRESS,
address_: empty(address),
id_: 0,
is_furry: False,
price: 0.0,
Expand Down
18 changes: 0 additions & 18 deletions tests/parser/functions/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,24 +534,6 @@ def foo(a: {typ}) -> Status:
assert_compile_failed(lambda: get_contract_with_gas_estimation(contract), TypeMismatch)


# TODO CMC 2022-04-06 I think this test is somewhat unnecessary.
@pytest.mark.parametrize(
"builtin_constant,out_type,out_value",
[("ZERO_ADDRESS", "bool", False), ("msg.sender", "bool", True)],
)
def test_convert_builtin_constant(
get_contract_with_gas_estimation, builtin_constant, out_type, out_value
):
contract = f"""
@external
def convert_builtin_constant() -> {out_type}:
return convert({builtin_constant}, {out_type})
"""

c = get_contract_with_gas_estimation(contract)
assert c.convert_builtin_constant() == out_value


# uint256 conversion is currently valid due to type inference on literals
# not quite working yet
same_type_conversion_blocked = sorted(TEST_TYPES - {UINT256_T})
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/functions/test_default_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_basic_default_default_param_function(w3, get_logs, get_contract_with_ga
@external
@payable
def fooBar(a: int128 = 12345) -> int128:
log Sent(ZERO_ADDRESS)
log Sent(empty(address))
return a
@external
Expand Down
20 changes: 10 additions & 10 deletions tests/parser/functions/test_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def foo():
self.foobar = empty(address)
bar = empty(address)
assert self.foobar == ZERO_ADDRESS
assert bar == ZERO_ADDRESS
assert self.foobar == empty(address)
assert bar == empty(address)
""",
"""
@external
Expand Down Expand Up @@ -214,12 +214,12 @@ def foo():
self.foobar = empty(address[3])
bar = empty(address[3])
assert self.foobar[0] == ZERO_ADDRESS
assert self.foobar[1] == ZERO_ADDRESS
assert self.foobar[2] == ZERO_ADDRESS
assert bar[0] == ZERO_ADDRESS
assert bar[1] == ZERO_ADDRESS
assert bar[2] == ZERO_ADDRESS
assert self.foobar[0] == empty(address)
assert self.foobar[1] == empty(address)
assert self.foobar[2] == empty(address)
assert bar[0] == empty(address)
assert bar[1] == empty(address)
assert bar[2] == empty(address)
""",
],
)
Expand Down Expand Up @@ -376,14 +376,14 @@ def foo():
assert self.foobar.c == False
assert self.foobar.d == 0.0
assert self.foobar.e == 0x0000000000000000000000000000000000000000000000000000000000000000
assert self.foobar.f == ZERO_ADDRESS
assert self.foobar.f == empty(address)
assert bar.a == 0
assert bar.b == 0
assert bar.c == False
assert bar.d == 0.0
assert bar.e == 0x0000000000000000000000000000000000000000000000000000000000000000
assert bar.f == ZERO_ADDRESS
assert bar.f == empty(address)
"""

c = get_contract_with_gas_estimation(code)
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/functions/test_return_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def out_literals() -> (int128, address, Bytes[10]):
@external
def test() -> (int128, address, Bytes[10]):
a: int128 = 0
b: address = ZERO_ADDRESS
b: address = empty(address)
c: Bytes[10] = b""
(a, b, c) = self._out_literals()
return a, b, c
Expand Down Expand Up @@ -138,7 +138,7 @@ def test2() -> (int128, address):
@external
def test3() -> (address, int128):
x: address = ZERO_ADDRESS
x: address = empty(address)
self.a, self.c, x, self.d = self._out_literals()
return x, self.a
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/integration/test_escrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_arbitration_code(w3, get_contract_with_gas_estimation, assert_tx_failed
@external
def setup(_seller: address, _arbitrator: address):
if self.buyer == ZERO_ADDRESS:
if self.buyer == empty(address):
self.buyer = msg.sender
self.seller = _seller
self.arbitrator = _arbitrator
Expand Down Expand Up @@ -43,7 +43,7 @@ def test_arbitration_code_with_init(w3, assert_tx_failed, get_contract_with_gas_
@external
@payable
def __init__(_seller: address, _arbitrator: address):
if self.buyer == ZERO_ADDRESS:
if self.buyer == empty(address):
self.buyer = msg.sender
self.seller = _seller
self.arbitrator = _arbitrator
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/syntax/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def foo() -> bool:
"""
@external
def foo() -> bool:
a: address = ZERO_ADDRESS
a: address = empty(address)
return a == 1
""",
(
Expand Down Expand Up @@ -137,7 +137,7 @@ def foo() -> bool:
"""
@external
def foo2(a: address) -> bool:
return a != ZERO_ADDRESS
return a != empty(address)
""",
]

Expand Down
4 changes: 2 additions & 2 deletions tests/parser/syntax/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test():
@external
def test():
a: address(ERC20) = ZERO_ADDRESS
a: address(ERC20) = empty(address)
""",
InvalidType,
),
Expand Down Expand Up @@ -306,7 +306,7 @@ def some_func(): nonpayable
@external
def __init__():
self.my_interface[self.idx] = MyInterface(ZERO_ADDRESS)
self.my_interface[self.idx] = MyInterface(empty(address))
""",
"""
interface MyInterface:
Expand Down
12 changes: 6 additions & 6 deletions tests/parser/syntax/test_no_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def foo():
"""
@external
def foo():
bar: bytes32 = EMPTY_BYTES32
bar: bytes32 = empty(bytes32)
bar = None
""",
"""
@external
def foo():
bar: address = ZERO_ADDRESS
bar: address = empty(address)
bar = None
""",
"""
Expand Down Expand Up @@ -104,13 +104,13 @@ def foo():
"""
@external
def foo():
bar: bytes32 = EMPTY_BYTES32
bar: bytes32 = empty(bytes32)
assert bar is None
""",
"""
@external
def foo():
bar: address = ZERO_ADDRESS
bar: address = empty(address)
assert bar is None
""",
]
Expand Down Expand Up @@ -148,13 +148,13 @@ def foo():
"""
@external
def foo():
bar: bytes32 = EMPTY_BYTES32
bar: bytes32 = empty(bytes32)
assert bar == None
""",
"""
@external
def foo():
bar: address = ZERO_ADDRESS
bar: address = empty(address)
assert bar == None
""",
]
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/syntax/test_tuple_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def out_literals() -> (int128, int128, Bytes[10]):
@external
def test() -> (int128, address, Bytes[10]):
a: int128 = 0
b: address = ZERO_ADDRESS
b: address = empty(address)
a, b = self.out_literals() # tuple count mismatch
return
""",
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/syntax/test_unbalanced_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def valid_address(sender: address) -> bool:
"""
@internal
def valid_address(sender: address) -> bool:
if sender == ZERO_ADDRESS:
if sender == empty(address):
selfdestruct(sender)
_sender: address = sender
else:
Expand Down Expand Up @@ -144,7 +144,7 @@ def test() -> int128:
"""
@external
def test() -> int128:
x: bytes32 = EMPTY_BYTES32
x: bytes32 = empty(bytes32)
if False:
if False:
return 0
Expand Down
Loading

0 comments on commit 5482bbc

Please sign in to comment.