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: add convert for enum #2977

Merged
merged 10 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions docs/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -622,5 +622,6 @@ All type conversions in Vyper must be made explicitly using the built-in ``conve
* Narrowing conversions (e.g., ``int256 -> int128``) check that the input is in bounds for the output type.
* Converting between bytes and int types results in sign-extension if the output type is signed. For instance, converting ``0xff`` (``bytes1``) to ``int8`` returns ``-1``.
* Converting between bytes and int types which have different sizes follows the rule of going through the closest integer type, first. For instance, ``bytes1 -> int16`` is like ``bytes1 -> int8 -> int16`` (signextend, then widen). ``uint8 -> bytes20`` is like ``uint8 -> uint160 -> bytes20`` (rotate left 12 bytes).
* Enums can be converted to ``uint256`` only.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the issues to converting to lesser values? Can't you determine the upper bound for a given Enum and throw a compiler or runtime error if it exceeds it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since enums are uint256 under the hood, I don't think there are any issues with supporting conversion to smaller integer types. It is more of whether we want to support that conversion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fubuloubu i think the whole range of ints is a bit harder to test; we can add them later too


A small Python reference implementation is maintained as part of Vyper's test suite, it can be found `here <https://github.com/vyperlang/vyper/blob/c4c6afd07801a0cc0038cdd4007cc43860c54193/tests/parser/functions/test_convert.py#L318>`_. The motivation and more detailed discussion of the rules can be found `here <https://github.com/vyperlang/vyper/issues/2507>`_.
19 changes: 19 additions & 0 deletions tests/parser/functions/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,25 @@ def test_memory_variable_convert(x: {i_typ}) -> {o_typ}:
assert c4.test_memory_variable_convert(val) == expected_val


@pytest.mark.parametrize("typ", ["uint8", "int128", "int256", "uint256"])
@pytest.mark.parametrize("val", [1, 2, 2 ** 128, 2 ** 256 - 1, 2 ** 256 - 2])
def test_enum_conversion(get_contract_with_gas_estimation, assert_compile_failed, val, typ):
roles = "\n ".join([f"ROLE_{i}" for i in range(256)])
contract = f"""
enum Roles:
{roles}

@external
def foo(a: Roles) -> {typ}:
return convert(a, {typ})
"""
if typ == "uint256":
c = get_contract_with_gas_estimation(contract)
assert c.foo(val) == val
else:
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",
Expand Down
6 changes: 6 additions & 0 deletions vyper/builtin_functions/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
is_base_type,
is_bytes_m_type,
is_decimal_type,
is_enum_type,
is_integer_type,
)
from vyper.exceptions import (
Expand Down Expand Up @@ -326,6 +327,11 @@ def to_int(expr, arg, out_typ):
elif is_decimal_type(arg.typ):
arg = _fixed_to_int(arg, out_typ)

elif is_enum_type(arg.typ):
if out_typ.typ != "uint256":
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
_FAIL(arg.typ, out_typ, expr)
arg = _int_to_int(arg, out_typ)

elif is_integer_type(arg.typ):
arg = _int_to_int(arg, out_typ)

Expand Down
6 changes: 6 additions & 0 deletions vyper/codegen/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def parse_decimal_info(typename: str) -> DecimalTypeInfo:
return DecimalTypeInfo(bits=168, decimals=10, is_signed=True)


def is_enum_type(t: "NodeType") -> bool:
return isinstance(t, EnumType)


def _basetype_to_abi_type(t: "BaseType") -> ABIType:
if is_integer_type(t):
info = t._int_info
Expand Down Expand Up @@ -237,6 +241,8 @@ def __repr__(self):
return f"enum {self.name}"

def __eq__(self, other):
if type(self) is not type(other):
return False
return self.name == other.name and self.members == other.members

@property
Expand Down