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

allow lists of any type as loop variable #2616

Merged
merged 9 commits into from
Jan 23, 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
10 changes: 4 additions & 6 deletions vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from vyper.codegen.expr import Expr
from vyper.codegen.return_ import make_return_stmt
from vyper.codegen.types import BaseType, ByteArrayType, DArrayType, SArrayType, parse_type
from vyper.codegen.types.convert import new_type_to_old_type
from vyper.exceptions import CompilerPanic, StructureException, TypeCheckFailure


Expand Down Expand Up @@ -299,13 +300,10 @@ def _parse_For_list(self):
with self.context.range_scope():
iter_list = Expr(self.stmt.iter, self.context).lll_node

# TODO relax this restriction
if not isinstance(iter_list.typ.subtype, BaseType):
return

# override with type inferred at typechecking time
subtype = BaseType(self.stmt.target._metadata["type"]._id)
iter_list.typ.subtype = subtype
typ = new_type_to_old_type(self.stmt.iter._metadata["type"])
iter_list.typ = typ
subtype = typ.subtype

# user-supplied name for loop variable
varname = self.stmt.target.id
Expand Down
37 changes: 37 additions & 0 deletions vyper/codegen/types/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# transition module to convert from new types to old types

import vyper.codegen.types as old
import vyper.semantics.types as new
from vyper.exceptions import InvalidType


def new_type_to_old_type(typ: new.BasePrimitive) -> old.NodeType:
if isinstance(typ, new.BoolDefinition):
return old.BaseType("bool")
if isinstance(typ, new.AddressDefinition):
return old.BaseType("address")
if isinstance(typ, new.Bytes32Definition):
return old.BaseType("bytes32")
if isinstance(typ, new.BytesArrayDefinition):
return old.BytesType(typ.count)
if isinstance(typ, new.StringDefinition):
return old.StringType(typ.count)
if isinstance(typ, new.DecimalDefinition):
return old.BaseType("decimal")
if isinstance(typ, new.SignedIntegerAbstractType):
bits = typ._bits # type: ignore
return old.BaseType("int" + str(bits))
if isinstance(typ, new.UnsignedIntegerAbstractType):
bits = typ._bits # type: ignore
return old.BaseType("uint" + str(bits))
if isinstance(typ, new.ArrayDefinition):
return old.SArrayType(new_type_to_old_type(typ.value_type), typ.length)
if isinstance(typ, new.DynamicArrayDefinition):
return old.DArrayType(new_type_to_old_type(typ.value_type), typ.length)
if isinstance(typ, new.TupleDefinition):
return old.TupleType(typ.value_type)
if isinstance(typ, new.StructDefinition):
return old.StructType(
{n: new_type_to_old_type(t) for (n, t) in typ.members.items()}, typ._id
)
raise InvalidType(f"unknown type {typ}")
7 changes: 0 additions & 7 deletions vyper/semantics/validation/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
TupleDefinition,
)
from vyper.semantics.types.user.event import Event
from vyper.semantics.types.user.struct import StructDefinition
from vyper.semantics.types.utils import get_type_from_annotation
from vyper.semantics.types.value.address import AddressDefinition
from vyper.semantics.types.value.array_value import StringDefinition
Expand Down Expand Up @@ -369,12 +368,6 @@ def visit_For(self, node):
if not type_list:
raise InvalidType("Not an iterable type", node.iter)

if next((i for i in type_list if isinstance(i, ArrayDefinition)), False):
raise StructureException("Cannot iterate over a nested list", node.iter)

if next((i for i in type_list if isinstance(i, StructDefinition)), False):
raise StructureException("Cannot iterate over a list of structs", node.iter)

if isinstance(node.iter, (vy_ast.Name, vy_ast.Attribute)):
# check for references to the iterated value within the body of the loop
assign = _check_iterator_assign(node.iter, node)
Expand Down