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

feat[lang]: allow hex literals for unsigned integer types #4387

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
17 changes: 16 additions & 1 deletion tests/functional/codegen/types/numbers/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tests.utils import ZERO_ADDRESS, decimal_to_int
from vyper.compiler import compile_code
from vyper.exceptions import TypeMismatch
from vyper.utils import MemoryPositions
from vyper.utils import MemoryPositions, hex_to_int


def search_for_sublist(ir, sublist):
Expand Down Expand Up @@ -244,3 +244,18 @@ def contains(a: int128) -> bool:
assert c.contains(44) is True
assert c.contains(33) is True
assert c.contains(3) is False


def test_constant_hex_int(get_contract):
test_value = "0xfa"
code = f"""
X: constant(uint8) = {test_value}
@external
def test() -> uint8:
y: uint8 = X
return y
"""

c = get_contract(code)

assert c.test() == hex_to_int(test_value)
15 changes: 13 additions & 2 deletions tests/functional/codegen/types/numbers/test_unsigned_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@


@pytest.mark.parametrize("typ", types)
def test_uint_literal(get_contract, typ):
@pytest.mark.parametrize("is_hex_int", [True, False])
def test_uint_literal(get_contract, typ, is_hex_int):
lo, hi = typ.ast_bounds

good_cases = [0, 1, 2, 3, hi // 2 - 1, hi // 2, hi // 2 + 1, hi - 1, hi]
Expand All @@ -222,11 +223,21 @@
return o
"""

def _to_hex_int(v):
Fixed Show fixed Hide fixed
n_nibbles = typ.bits // 4
return "0x" + hex(v)[2:].rjust(n_nibbles, "0")

for val in good_cases:
c = get_contract(code_template.format(typ=typ, val=val))
input_val = val
if is_hex_int:
n_nibbles = typ.bits // 4
input_val = "0x" + hex(val)[2:].rjust(n_nibbles, "0")
c = get_contract(code_template.format(typ=typ, val=input_val))
assert c.test() == val

for val in bad_cases:
if is_hex_int:
return
exc = (
TypeMismatch
if SizeLimits.MIN_INT256 <= val <= SizeLimits.MAX_UINT256
Expand Down
8 changes: 8 additions & 0 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
SizeLimits,
annotate_source_code,
evm_div,
hex_to_int,
quantize,
sha256sum,
)
Expand Down Expand Up @@ -883,6 +884,13 @@ def bytes_value(self):
"""
return bytes.fromhex(self.value.removeprefix("0x"))

@property
def int_value(self):
"""
This value as integer
"""
return hex_to_int(self.value)


class Str(Constant):
__slots__ = ()
Expand Down
2 changes: 2 additions & 0 deletions vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class Decimal(Num): ...
class Hex(Num):
@property
def n_bytes(self): ...
@property
def int_value(self): ...

class Str(Constant): ...
class Bytes(Constant): ...
Expand Down
7 changes: 7 additions & 0 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
is_array_like,
is_bytes_m_type,
is_flag_type,
is_integer_type,
is_numeric_type,
is_tuple_like,
make_setter,
Expand Down Expand Up @@ -133,6 +134,12 @@ def parse_Hex(self):

return IRnode.from_list(val, typ=t)

elif is_integer_type(t):
assert not t.is_signed

val = self.expr.int_value
return IRnode.from_list(val, typ=t)

# String literals
def parse_Str(self):
bytez, bytez_length = string_to_bytes(self.expr.value)
Expand Down
11 changes: 8 additions & 3 deletions vyper/semantics/types/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,14 @@ def is_signed(self) -> bool:
def validate_literal(self, node: vy_ast.Constant) -> None:
super().validate_literal(node)
lower, upper = self.ast_bounds
if node.value < lower:

value = node.value
if isinstance(node, vy_ast.Hex):
value = node.int_value

if value < lower:
raise OverflowException(f"Value is below lower bound for given type ({lower})", node)
if node.value > upper:
if value > upper:
raise OverflowException(f"Value exceeds upper bound for given type ({upper})", node)

def validate_numeric_op(
Expand Down Expand Up @@ -242,7 +247,7 @@ class IntegerT(NumericT):

typeclass = "integer"

_valid_literal = (vy_ast.Int,)
_valid_literal = (vy_ast.Hex, vy_ast.Int)
_equality_attrs = ("is_signed", "bits")

ast_type = int
Expand Down
Loading