Skip to content

Commit

Permalink
fix: add getters for public constants and immutables to contract name…
Browse files Browse the repository at this point in the history
…space (#3334)
  • Loading branch information
tserg authored Apr 7, 2023
1 parent 8afe613 commit 026f85f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
20 changes: 20 additions & 0 deletions tests/parser/syntax/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ def kick(): payable
kickers: HashMap[address, MyInterface]
""",
"""
interface ITestInterface:
def foo() -> uint256: view
implements: ITestInterface
foo: public(constant(uint256)) = 1
""",
"""
interface ITestInterface:
def foo() -> uint256: view
implements: ITestInterface
foo: public(immutable(uint256))
@external
def __init__(x: uint256):
foo = x
""",
]


Expand Down
32 changes: 23 additions & 9 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ def visit_VariableDecl(self, node):
node.target._metadata["varinfo"] = var_info # TODO maybe put this in the global namespace
node._metadata["type"] = type_

def _finalize():
# add the variable name to `self` namespace if the variable is either
# 1. a public constant or immutable; or
# 2. a storage variable, whether private or public
if (node.is_constant or node.is_immutable) and not node.is_public:
return

try:
self.namespace["self"].typ.add_member(name, var_info)
node.target._metadata["type"] = type_
except NamespaceCollision:
raise NamespaceCollision(
f"Value '{name}' has already been declared", node
) from None
except VyperException as exc:
raise exc.with_annotation(node) from None

if node.is_constant:
if not node.value:
raise VariableDeclarationException("Constant must be declared with a value", node)
Expand All @@ -219,7 +236,8 @@ def visit_VariableDecl(self, node):
self.namespace[name] = var_info
except VyperException as exc:
raise exc.with_annotation(node) from None
return

return _finalize()

if node.value:
var_type = "Immutable" if node.is_immutable else "Storage"
Expand All @@ -237,19 +255,15 @@ def visit_VariableDecl(self, node):
self.namespace[name] = var_info
except VyperException as exc:
raise exc.with_annotation(node) from None
return

return _finalize()

try:
self.namespace.validate_assignment(name)
except NamespaceCollision as exc:
raise exc.with_annotation(node) from None
try:
self.namespace["self"].typ.add_member(name, var_info)
node.target._metadata["type"] = type_
except NamespaceCollision:
raise NamespaceCollision(f"Value '{name}' has already been declared", node) from None
except VyperException as exc:
raise exc.with_annotation(node) from None

return _finalize()

def visit_EnumDef(self, node):
obj = EnumT.from_EnumDef(node)
Expand Down

0 comments on commit 026f85f

Please sign in to comment.