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: in and not in comparisons for dynamic arrays of enums #3526

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 0 additions & 13 deletions tests/parser/syntax/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,6 @@
STAFF
ADMIN

@external
def foo(x: Roles) -> bool:
return x in [Roles.USER, Roles.ADMIN]
""",
TypeMismatch,
),
(
"""
enum Roles:
USER
STAFF
ADMIN

@external
def foo(x: Roles) -> Roles:
return x.USER # can't dereference on enum instance
Expand Down
41 changes: 41 additions & 0 deletions tests/parser/types/test_dynamic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,24 @@ def check(a: {type}) -> bool:
assert c.check(false_value) is False


def test_enum_member_in_list(get_contract_with_gas_estimation):
code = """
enum Foo:
A
B
C

@external
def check(a: Foo) -> bool:
x: DynArray[Foo, 2] = [Foo.A, Foo.C]
return a in x
"""
c = get_contract_with_gas_estimation(code)
assert c.check(1) is True
assert c.check(4) is True
assert c.check(2) is False


@pytest.mark.parametrize("type_", ("uint256", "bytes32", "address"))
def test_member_in_empty_list(get_contract_with_gas_estimation, type_):
code = f"""
Expand All @@ -461,6 +479,29 @@ def check_not_in(s: uint128) -> bool:
assert c.check_not_in(s) is True


def test_enum_member_in_empty_list(get_contract_with_gas_estimation):
code = """
enum Foo:
A
B
C

@external
def check_in(s: Foo) -> bool:
x: DynArray[Foo, 2] = []
return s in x

@external
def check_not_in(s: Foo) -> bool:
x: DynArray[Foo, 2] = []
return s not in x
"""
c = get_contract_with_gas_estimation(code)
for s in (1, 2, 4):
assert c.check_in(s) is False
assert c.check_not_in(s) is True


@pytest.mark.parametrize(
"type,values,false_values",
[
Expand Down
4 changes: 3 additions & 1 deletion vyper/semantics/analysis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ def types_from_Compare(self, node):
# x in y
left = self.get_possible_types_from_node(node.left)
right = self.get_possible_types_from_node(node.right)
if any(isinstance(t, EnumT) for t in left):
if any(isinstance(t, EnumT) for t in left) and not any(
isinstance(i, (DArrayT, SArrayT)) for i in right
):
types_list = get_common_types(node.left, node.right)
_validate_op(node, types_list, "validate_comparator")
return [BoolT()]
Expand Down