-
-
Notifications
You must be signed in to change notification settings - Fork 905
refactor[codegen]: refactor venom builtins for uniformity and cleanliness #5001
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
banteg
wants to merge
15
commits into
vyperlang:master
Choose a base branch
from
banteg:fix/venom-reduced-builtin-kwargs
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 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8832e9d
fix[venom]: reduce builtin kwarg helpers
banteg 5229c75
fix[venom]: preserve builtin kwarg source order
banteg 9e9070f
refactor[venom]: unify builtin kwarg helpers
banteg 29d696e
refactor[venom]: centralize value materialization
banteg e2c3e45
refactor[venom]: simplify kwarg constants
banteg 85cf224
refactor[venom]: use reduced method id nodes
banteg 51da445
refactor[venom]: separate kwarg validation
banteg 2a92b0d
refactor[venom]: centralize builtin call lowering
banteg f61bc2e
fix[venom]: satisfy builtin call lint
banteg 2f6f1e4
refactor[venom]: use boolean kwarg adapters
banteg 5075470
refactor[venom]: pass builtin call through dispatcher
banteg 053367b
refactor[venom]: cache builtin kwarg validation
banteg 4409616
Merge remote-tracking branch 'origin/master' into fix/venom-reduced-b…
banteg 3ea8f27
docs[venom]: clarify builtin lowering order
banteg 3e8fa3a
refactor[venom]: prelower builtin runtime arguments
banteg 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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,82 @@ | ||
| import pytest | ||
|
|
||
| from vyper import ast as vy_ast | ||
| from vyper.codegen_venom.builtins.abi import _get_bool_kwarg as get_abi_bool_kwarg | ||
| from vyper.codegen_venom.builtins.misc import _get_bool_kwarg as get_misc_bool_kwarg | ||
| from vyper.codegen_venom.builtins._kwargs import ( | ||
| get_bool_kwarg, | ||
| get_kwarg_ast_constants, | ||
| get_literal_kwarg, | ||
| kwarg_is_provided, | ||
| validate_kwargs, | ||
| ) | ||
| from vyper.exceptions import CompilerPanic | ||
|
|
||
|
|
||
| def _call_node(source): | ||
| return vy_ast.parse_to_ast(source).body[0].value | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("get_bool_kwarg", [get_abi_bool_kwarg, get_misc_bool_kwarg]) | ||
| def test_bool_kwarg_uses_reduced_value(get_bool_kwarg): | ||
| def test_kwarg_ast_constants_return_folded_nodes(): | ||
| call_node = _call_node("foo(flag=FLAG)") | ||
| call_node.keywords[0].value._set_folded_value(vy_ast.NameConstant(value=False)) | ||
|
|
||
| assert get_bool_kwarg(call_node, "flag", True) is False | ||
| constants = get_kwarg_ast_constants(call_node, ("flag",)) | ||
|
|
||
| assert constants["flag"].value is False | ||
|
|
||
| @pytest.mark.parametrize("get_bool_kwarg", [get_abi_bool_kwarg, get_misc_bool_kwarg]) | ||
| def test_bool_kwarg_rejects_unreduced_value(get_bool_kwarg): | ||
|
|
||
| def test_kwarg_ast_constants_reject_unfolded_values(): | ||
| call_node = _call_node("foo(flag=FLAG)") | ||
|
|
||
| with pytest.raises(CompilerPanic, match="unfoldable constant kwarg: flag"): | ||
| get_kwarg_ast_constants(call_node, ("flag",)) | ||
|
|
||
|
|
||
| def test_kwarg_helpers_reject_unexpected_kwargs(): | ||
| call_node = _call_node("foo(flag=False)") | ||
|
|
||
| with pytest.raises(CompilerPanic, match="unexpected kwarg: flag"): | ||
| validate_kwargs(call_node, ("other",)) | ||
|
|
||
|
|
||
| def test_kwarg_helpers_reject_duplicate_kwargs(): | ||
| call_node = _call_node("foo(flag=False, flag=True)") | ||
|
|
||
| with pytest.raises(CompilerPanic, match="duplicate kwarg: flag"): | ||
| validate_kwargs(call_node, ("flag",)) | ||
|
|
||
|
|
||
| def test_kwarg_is_provided(): | ||
| call_node = _call_node("foo(flag=False)") | ||
|
|
||
| assert kwarg_is_provided(call_node, "flag") is True | ||
| assert kwarg_is_provided(call_node, "other") is False | ||
|
|
||
|
|
||
| def test_bool_kwarg_uses_reduced_value(): | ||
| call_node = _call_node("foo(flag=FLAG)") | ||
| call_node.keywords[0].value._set_folded_value(vy_ast.NameConstant(value=False)) | ||
| constants = get_kwarg_ast_constants(call_node, ("flag",)) | ||
|
|
||
| assert get_bool_kwarg(constants, "flag", True) is False | ||
|
|
||
|
|
||
| def test_bool_kwarg_rejects_unreduced_value(): | ||
| call_node = _call_node("foo(flag=FLAG)") | ||
|
|
||
| with pytest.raises(CompilerPanic, match="unfoldable constant kwarg: flag"): | ||
| get_kwarg_ast_constants(call_node, ("flag",)) | ||
|
|
||
|
|
||
| def test_literal_kwarg_uses_reduced_value(): | ||
| call_node = _call_node("foo(revert_on_failure=REVERT)") | ||
| call_node.keywords[0].value._set_folded_value(vy_ast.NameConstant(value=False)) | ||
| constants = get_kwarg_ast_constants(call_node, ("revert_on_failure",)) | ||
|
|
||
| assert get_literal_kwarg(constants, "revert_on_failure", True) is False | ||
|
|
||
|
|
||
| def test_literal_kwarg_rejects_unreduced_value(): | ||
| call_node = _call_node("foo(revert_on_failure=REVERT)") | ||
|
|
||
| with pytest.raises(CompilerPanic, match="unfoldable boolean kwarg: flag"): | ||
| get_bool_kwarg(call_node, "flag", True) | ||
| with pytest.raises(CompilerPanic, match="unfoldable constant kwarg: revert_on_failure"): | ||
| get_kwarg_ast_constants(call_node, ("revert_on_failure",)) |
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,100 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterable | ||
| from typing import Any | ||
|
|
||
| from vyper import ast as vy_ast | ||
| from vyper.exceptions import CompilerPanic | ||
|
|
||
|
|
||
| _UNSET = object() | ||
|
|
||
|
|
||
| def validate_kwargs(node: vy_ast.Call, allowed_kwarg_names: Iterable[str]) -> None: | ||
| seen = set() | ||
| for kw in node.keywords: | ||
| if kw.arg in seen: # pragma: nocover | ||
| raise CompilerPanic(f"duplicate kwarg: {kw.arg}", kw) | ||
| seen.add(kw.arg) | ||
|
|
||
| allowed_kwarg_names = set(allowed_kwarg_names) | ||
|
banteg marked this conversation as resolved.
Outdated
|
||
| for kw in node.keywords: | ||
|
banteg marked this conversation as resolved.
|
||
| if kw.arg not in allowed_kwarg_names: # pragma: nocover | ||
| raise CompilerPanic(f"unexpected kwarg: {kw.arg}", kw) | ||
|
|
||
|
|
||
| def kwarg_is_provided(node: vy_ast.Call, kwarg_name: str) -> bool: | ||
|
banteg marked this conversation as resolved.
Outdated
|
||
| return any(kw.arg == kwarg_name for kw in node.keywords) | ||
|
|
||
|
|
||
| def get_kwarg_ast_constants( | ||
| node: vy_ast.Call, | ||
| kwarg_names: Iterable[str], | ||
|
banteg marked this conversation as resolved.
Outdated
|
||
| error_prefix: str = "unfoldable constant kwarg", | ||
| ) -> dict[str, vy_ast.Constant]: | ||
| kwarg_names = set(kwarg_names) | ||
| ret = {} | ||
| for kw in node.keywords: | ||
| if kw.arg not in kwarg_names: | ||
|
banteg marked this conversation as resolved.
Outdated
|
||
| continue | ||
|
|
||
| kw_node = kw.value.reduced() | ||
| if not isinstance(kw_node, vy_ast.Constant): # pragma: nocover | ||
| raise CompilerPanic(f"{error_prefix}: {kw.arg}", kw_node) | ||
| ret[kw.arg] = kw_node | ||
|
banteg marked this conversation as resolved.
Outdated
|
||
|
|
||
| return ret | ||
|
|
||
|
|
||
| def get_kwarg_values( | ||
| node: vy_ast.Call, | ||
| ctx, | ||
| kwarg_names: Iterable[str], | ||
| ): | ||
| from vyper.codegen_venom.expr import Expr | ||
|
|
||
| kwarg_names = set(kwarg_names) | ||
| ret = {} | ||
| for kw in node.keywords: | ||
| if kw.arg in kwarg_names: | ||
| ret[kw.arg] = Expr(kw.value.reduced(), ctx).lower_value() | ||
| return ret | ||
|
|
||
|
|
||
| def _literal_value(node: vy_ast.VyperNode) -> Any: | ||
| if isinstance(node, vy_ast.Int): | ||
| return node.value | ||
| if isinstance(node, vy_ast.NameConstant): | ||
| return node.value | ||
| return _UNSET | ||
|
|
||
|
|
||
| def get_bool_kwarg( | ||
|
banteg marked this conversation as resolved.
Outdated
|
||
| kwarg_constants: dict[str, vy_ast.Constant], | ||
| kwarg_name: str, | ||
| default: bool, | ||
| ) -> bool: | ||
| kw_node = kwarg_constants.get(kwarg_name) | ||
| if kw_node is None: | ||
| return default | ||
| if isinstance(kw_node, vy_ast.NameConstant): | ||
| return kw_node.value | ||
| if isinstance(kw_node, vy_ast.Int): | ||
| return bool(kw_node.value) | ||
| raise CompilerPanic(f"unfoldable boolean kwarg: {kwarg_name}", kw_node) | ||
|
|
||
|
|
||
| def get_literal_kwarg( | ||
| kwarg_constants: dict[str, vy_ast.Constant], | ||
| kwarg_name: str, | ||
| default, | ||
| ): | ||
| kw_node = kwarg_constants.get(kwarg_name) | ||
| if kw_node is None: | ||
| return default | ||
|
|
||
| value = _literal_value(kw_node) | ||
| if value is not _UNSET: | ||
| return value | ||
|
|
||
| raise CompilerPanic(f"unfoldable literal kwarg: {kwarg_name}", kw_node) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.