-
-
Notifications
You must be signed in to change notification settings - Fork 810
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
VIP: require loop variable annotation #3475
Comments
Do we want to consider disallowing literal lists if there is more than one possible type? This seems like a reasonable restriction given that one of vyper's goals is strong typing. Instead of the first snippet, we require users to do the latter:
|
I think with the iterator variable finally having typing, we can forward propagate the type to the list literal |
I was thinking that between the two un-pythonic choices of (1) type annotation for the iterator variable in |
i disagree. annotating iterator variables is simpler from a UX perspective. it also increases consistency of vyper's variable system, since iterator variables are currently the only variables in vyper that are not annotated, whereas, requiring lists used as an iterator to not be literals decreases vyper's internal consistency since is inconsistent with .. most of the rest of vyper, untyped list literals are almost always allowed including when the type is unspecified, e.x. |
I have a sketch at this PR - during pre-parsing, we transform |
Simple Summary
as title. require for statements like
for i: uint256 in [1, 2, 3]:
instead offor i in [1, 2, 3]
(h/t to @trocher for suggesting this change to the language!)
Motivation
loop variable type inference is one of the most complicated areas of the vyper type checker, taking up 150 loc (that is, 30%) of vyper/semantics/analysis/local.py:
vyper/vyper/semantics/analysis/local.py
Lines 336 to 493 in 7f18aee
because the compiler currently has to loop over all possible types for the loop variable, it
for
loop #3443, bug: inconsistent typechecking behaviour of integer iterator in afor
loop #3441, bug: iterator modification check has false positives and false negatives #3429, typechecker state committer has incorrect behavior for nested loops #3374uint256
(thanks to @trocher for these examples):these above two examples could be fixed by attempting to (at compile-time) unroll the loop and fold in the loop variable, and reject types which result in folding failures. this would "correctly" (i.e., match the user's intuition) infer the type of
x
asint256
in both of the above two cases. however, this would result in more complexity in the loop variable type inference routine, and would result in more runtime complexity as well.lastly, the fact that loop variables are currently not annotated is actually a peculiar outcome of PEP526 - "In addition, one cannot annotate variables used in a for or with statement". i consider this to be a flaw in PEP484+PEP526 itself, as loop variables cannot be annotated in python without commented type hints.
also, while it might seem unfamiliar to users at first, from the point of view of vyper, the fact that loop variables are not annotated is inconsistent with all other vyper variables, which do not run into the same type inference issues (because they are all required to be annotated in the first place).
Specification
require loop variables to be annotated.
an implementation note: the fact that type annotations are not allowed for loop variables causes some issues for us at the syntactic level, but they can be worked around (e.g. by adding a preprocessing rule which converts
for i: uint256 in ..:
to some currently unused syntactic expression, ex.for *(i, uint256) in ..:
, which can then be converted into a type annotation during conversion from python to vyper ast.Backwards Compatibility
this is a breaking change.
Dependencies
If this VIP depends on any other VIPs being implemented, please mention them.
References
Add any references that this VIP might reference (other VIPs/issues, links to blog posts, etc.)
Copyright
Copyright and related rights waived via CC0
The text was updated successfully, but these errors were encountered: