Skip to content

Commit

Permalink
handle const struct member
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Sep 25, 2023
1 parent a0af3ad commit 80e9dbd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 11 additions & 0 deletions tests/parser/functions/test_default_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ def foo(a: address = empty(address)):
def foo(a: int112 = min_value(int112)):
self.A = a
""",
"""
struct X:
x: int128
y: address
BAR: constant(X) = X({x: 1, y: 0x0000000000000000000000000000000000012345})
@external
def out_literals(a: int128 = BAR.x + 1) -> X:
return BAR
""",
]


Expand Down
10 changes: 8 additions & 2 deletions vyper/semantics/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ def _failwith(type_name):


def derive_folded_value(node: vy_ast.VyperNode):
if isinstance(node, vy_ast.BinOp):
if isinstance(node, vy_ast.Attribute):
val = derive_folded_value(node.value)
# constant struct members
if isinstance(val, dict):
return val[node.attr]
return None
elif isinstance(node, vy_ast.BinOp):
left = derive_folded_value(node.left)
right = derive_folded_value(node.right)
if left is None or right is None:
Expand Down Expand Up @@ -181,7 +187,7 @@ def derive_folded_value(node: vy_ast.VyperNode):
values = [derive_folded_value(v) for v in node.values]
if any(v is None for v in values):
return None
return {k: v for (k, v) in zip(node.keys, values)}
return {k.id: v for (k, v) in zip(node.keys, values)}
elif isinstance(node, (vy_ast.List, vy_ast.Tuple)):
val = [derive_folded_value(e) for e in node.elements]
if None in val:
Expand Down

0 comments on commit 80e9dbd

Please sign in to comment.