-
-
Couldn't load subscription status.
- Fork 869
feat(flag): enable iteration via Flag.__values__ #4739
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
Open
z80dev
wants to merge
9
commits into
vyperlang:master
Choose a base branch
from
z80dev:feat/iterate-flags
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
974dd55
feat(flag): require iteration via Flag.__values__
z80dev 26eb76c
chore: remove agents.md from repo
z80dev 2bee3dc
fix: resolve import cycle in user.py
z80dev 3ca2abc
fix: clean up unrelated change
z80dev bc44129
fix: remove unrelated formatting changes
z80dev 5b31a52
fix: lint errors
z80dev 262af81
fix: properly fix lint
z80dev 77fd597
fix: keep flag member lookups type-only
z80dev 2fde4bd
Merge branch 'master' into feat/iterate-flags
z80dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
tests/functional/codegen/features/test_flag_iteration_type.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| def test_iterate_over_flag_type(get_contract): | ||
| code = """ | ||
| flag Permission: | ||
| A | ||
| B | ||
| C | ||
|
|
||
| @pure | ||
| @external | ||
| def sum_mask() -> uint256: | ||
| acc: uint256 = 0 | ||
| for p: Permission in Permission.__values__: | ||
| acc = acc | convert(p, uint256) | ||
| return acc | ||
| """ | ||
| c = get_contract(code) | ||
| # 1 | 2 | 4 = 7 | ||
| assert c.sum_mask() == 7 | ||
|
|
||
|
|
||
| def test_iterate_over_flag_type_count(get_contract): | ||
| code = """ | ||
| flag Permission: | ||
| A | ||
| B | ||
| C | ||
| D | ||
|
|
||
| @pure | ||
| @external | ||
| def count() -> uint256: | ||
| cnt: uint256 = 0 | ||
| for p: Permission in Permission.__values__: | ||
| cnt += 1 | ||
| return cnt | ||
| """ | ||
| c = get_contract(code) | ||
| assert c.count() == 4 | ||
|
|
||
|
|
||
| def test_iterate_over_flag_type_order(get_contract): | ||
| code = """ | ||
| flag Permission: | ||
| A | ||
| B | ||
| C | ||
| D | ||
|
|
||
| @pure | ||
| @external | ||
| def order_sum() -> uint256: | ||
| acc: uint256 = 0 | ||
| idx: uint256 = 0 | ||
| for p: Permission in Permission.__values__: | ||
| acc = acc + (convert(p, uint256) << idx) | ||
| idx += 1 | ||
| return acc | ||
| """ | ||
| c = get_contract(code) | ||
| # 1 + (2<<1) + (4<<2) + (8<<3) = 1 + 4 + 16 + 64 = 85 | ||
| assert c.order_sum() == 85 | ||
|
|
||
|
|
||
| def test_flag_iter_target_type_mismatch(assert_compile_failed, get_contract): | ||
| from vyper.exceptions import TypeMismatch | ||
|
|
||
| code = """ | ||
| flag A: | ||
| X | ||
| flag B: | ||
| Y | ||
|
|
||
| @pure | ||
| @external | ||
| def f() -> uint256: | ||
| s: uint256 = 0 | ||
| for p: B in A.__values__: | ||
| s += convert(p, uint256) | ||
| return s | ||
| """ | ||
| assert_compile_failed(lambda: get_contract(code), TypeMismatch) | ||
|
|
||
|
|
||
| def test_flag_iter_invalid_iterator(assert_compile_failed, get_contract): | ||
| from vyper.exceptions import InvalidType | ||
|
|
||
| code = """ | ||
| flag P: | ||
| A | ||
|
|
||
| @pure | ||
| @external | ||
| def f() -> uint256: | ||
| s: uint256 = 0 | ||
| for p: P in 5: | ||
| s += 1 | ||
| return s | ||
| """ | ||
| assert_compile_failed(lambda: get_contract(code), InvalidType) | ||
|
|
||
|
|
||
| def test_flag_iter_wrong_target_type(assert_compile_failed, get_contract): | ||
| from vyper.exceptions import TypeMismatch | ||
|
|
||
| code = """ | ||
| flag P: | ||
| A | ||
| B | ||
|
|
||
| @pure | ||
| @external | ||
| def f() -> uint256: | ||
| s: uint256 = 0 | ||
| for p: uint256 in P.__values__: | ||
| s += p # wrong type; loop var must be P | ||
| return s | ||
| """ | ||
| assert_compile_failed(lambda: get_contract(code), TypeMismatch) | ||
|
|
||
|
|
||
| def test_flag_instance_member_access(assert_compile_failed, get_contract): | ||
| from vyper.exceptions import UnknownAttribute | ||
|
|
||
| code = """ | ||
| flag P: | ||
| A | ||
| B | ||
|
|
||
| @pure | ||
| @external | ||
| def f(p: P) -> uint256: | ||
| return convert(p.A, uint256) | ||
| """ | ||
| assert_compile_failed(lambda: get_contract(code), UnknownAttribute) | ||
|
|
||
|
|
||
| def test_nested_flag_type_iteration(get_contract): | ||
| code = """ | ||
| flag A: | ||
| X | ||
| Y | ||
| Z | ||
|
|
||
| flag B: | ||
| P | ||
| Q | ||
|
|
||
| @pure | ||
| @external | ||
| def product_sum() -> uint256: | ||
| s: uint256 = 0 | ||
| for a: A in A.__values__: | ||
| for b: B in B.__values__: | ||
| s += convert(a, uint256) * convert(b, uint256) | ||
| return s | ||
| """ | ||
| c = get_contract(code) | ||
| # a in {1,2,4}, b in {1,2} => (1+2+4)*(1+2) = 7*3 = 21 | ||
| assert c.product_sum() == 21 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Cyclic import Note