Skip to content

Commit

Permalink
clean up has_folded_value branches
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Dec 29, 2023
1 parent 6eca837 commit 5432063
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
9 changes: 4 additions & 5 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class Expr:
# TODO: Once other refactors are made reevaluate all inline imports

def __init__(self, node, context):
if isinstance(node, vy_ast.VyperNode):
node = node.get_folded_value() if node.has_folded_value else node
if isinstance(node, vy_ast.VyperNode) and node.has_folded_value:
node = node.get_folded_value()

self.expr = node
self.context = context
Expand Down Expand Up @@ -193,9 +193,8 @@ def parse_Name(self):
# using the folded value metadata
assert isinstance(varinfo.typ, StructT)
value_node = varinfo.decl_node.value
value_node = (
value_node.get_folded_value() if value_node.has_folded_value else value_node
)
if value_node.has_folded_value:
value_node = value_node.get_folded_value()
return Expr.parse_value_expr(value_node, self.context)

assert varinfo.modifiability == Modifiability.IMMUTABLE, "not an immutable!"
Expand Down
18 changes: 6 additions & 12 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,15 +758,8 @@ def _analyse_range_call(node: vy_ast.Call) -> list[VyperType]:
"""
validate_call_args(node, (1, 2), kwargs=["bound"])
kwargs = {s.arg: s.value for s in node.keywords or []}
arg0 = node.args[0].get_folded_value if node.args[0].has_folded_value else node.args[0]
start, end = (
(vy_ast.Int(value=0), arg0)
if len(node.args) == 1
else (
arg0,
node.args[1].get_folded_value() if node.args[1].has_folded_value else node.args[1],
)
)
start, end = (vy_ast.Int(value=0), node.args[0]) if len(node.args) == 1 else node.args
start, end = [i.get_folded_value() if i.has_folded_value else i for i in (start, end)]

all_args = (start, end, *kwargs.values())
for arg1 in all_args:
Expand All @@ -778,10 +771,11 @@ def _analyse_range_call(node: vy_ast.Call) -> list[VyperType]:

if "bound" in kwargs:
bound = kwargs["bound"]
folded_bound = bound.get_folded_value() if bound.has_folded_value else bound
if not isinstance(folded_bound, vy_ast.Num):
if bound.has_folded_value:
bound = bound.get_folded_value()
if not isinstance(bound, vy_ast.Num):
raise StateAccessViolation("Bound must be a literal", bound)
if folded_bound.value <= 0:
if bound.value <= 0:
raise StructureException("Bound must be at least 1", bound)
if isinstance(start, vy_ast.Num) and isinstance(end, vy_ast.Num):
error = "Please remove the `bound=` kwarg when using range with constants"
Expand Down
5 changes: 2 additions & 3 deletions vyper/semantics/types/subscriptable.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,8 @@ def from_annotation(cls, node: vy_ast.Subscript) -> "DArrayT":
raise StructureException(err_msg, node.slice)

length_node = node.slice.value.elements[1]
length_node = (
length_node.get_folded_value() if length_node.has_folded_value else length_node
)
if length_node.has_folded_value:
length_node = length_node.get_folded_value()

if not isinstance(length_node, vy_ast.Int):
raise StructureException(err_msg, length_node)
Expand Down
5 changes: 4 additions & 1 deletion vyper/semantics/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ def get_index_value(node: vy_ast.Index) -> int:
# TODO: revisit this!
from vyper.semantics.analysis.utils import get_possible_types_from_node

value = node.value.get_folded_value() if node.value.has_folded_value else node.value
value = node.value
if value.has_folded_value:
value = value.get_folded_value()

if not isinstance(value, vy_ast.Int):
if hasattr(node, "value"):
# even though the subscript is an invalid type, first check if it's a valid _something_
Expand Down

0 comments on commit 5432063

Please sign in to comment.