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 uint256 as iterator type in range-based for loop #2180

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,10 @@ class NameConstant(Constant):


class Name(VyperNode):
__slots__ = ("id",)
__slots__ = (
"id",
"_type",
)


class Expr(VyperNode):
Expand Down
1 change: 1 addition & 0 deletions vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class NameConstant(Constant): ...

class Name(VyperNode):
id: str = ...
_type: str = ...

class Expr(VyperNode):
value: VyperNode = ...
Expand Down
6 changes: 5 additions & 1 deletion vyper/context/validation/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
FunctionDeclarationException,
ImmutableViolation,
InvalidLiteral,
InvalidOperation,
InvalidType,
IteratorException,
NonPayableViolation,
Expand Down Expand Up @@ -369,8 +370,11 @@ def visit_For(self, node):
try:
for n in node.body:
self.visit(n)
# attach type information to allow non `int128` types in `vyper.parser.stmt`
# this is a temporary solution until `vyper.parser` has been refactored
node.target._type = type_._id
return
except TypeMismatch as exc:
except (TypeMismatch, InvalidOperation) as exc:
for_loop_exceptions.append(exc)

if len(set(str(i) for i in for_loop_exceptions)) == 1:
Expand Down
12 changes: 8 additions & 4 deletions vyper/parser/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ def parse_For(self):
if not 0 < len(self.stmt.iter.args) < 3:
return

# attempt to use the type specified by type checking, fall back to `int128`
# this is a stopgap solution to allow uint256 - it will be properly solved
# once we refactor `vyper.parser`
iter_typ = self.stmt.target.get("_type") or "int128"
block_scope_id = id(self.stmt)
with self.context.make_blockscope(block_scope_id):
# Get arg0
Expand All @@ -240,15 +244,15 @@ def parse_For(self):
# Type 1 for, e.g. for i in range(10): ...
if num_of_args == 1:
arg0_val = self._get_range_const_value(arg0)
start = LLLnode.from_list(0, typ="int128", pos=getpos(self.stmt))
start = LLLnode.from_list(0, typ=iter_typ, pos=getpos(self.stmt))
rounds = arg0_val

# Type 2 for, e.g. for i in range(100, 110): ...
elif self._check_valid_range_constant(self.stmt.iter.args[1], raise_exception=False)[0]:
arg0_val = self._get_range_const_value(arg0)
arg1_val = self._get_range_const_value(self.stmt.iter.args[1])
start = LLLnode.from_list(arg0_val, typ="int128", pos=getpos(self.stmt))
rounds = LLLnode.from_list(arg1_val - arg0_val, typ="int128", pos=getpos(self.stmt))
start = LLLnode.from_list(arg0_val, typ=iter_typ, pos=getpos(self.stmt))
rounds = LLLnode.from_list(arg1_val - arg0_val, typ=iter_typ, pos=getpos(self.stmt))

# Type 3 for, e.g. for i in range(x, x + 10): ...
else:
Expand All @@ -261,7 +265,7 @@ def parse_For(self):
return

varname = self.stmt.target.id
pos = self.context.new_variable(varname, BaseType("int128"), pos=getpos(self.stmt))
pos = self.context.new_variable(varname, BaseType(iter_typ), pos=getpos(self.stmt))
self.context.forvars[varname] = True
lll_node = LLLnode.from_list(
["repeat", pos, start, rounds, parse_body(self.stmt.body, self.context)],
Expand Down