Skip to content

Commit 82f44ee

Browse files
fix references to immutable variables in constructor (#2627)
with the internal name as is, it doesn't get inserted into the variable namespace properly and so a new memory location gets allocated when it is referenced. this commit changes the key inserted into `context.vars` so that it will get picked up properly in `parse_Name`.
1 parent 4dea0cf commit 82f44ee

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

tests/parser/features/test_immutable.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ def get_value() -> {typ}:
3232
assert c.get_value() == value
3333

3434

35+
@pytest.mark.parametrize("val", [0, 1, 2 ** 256 - 1])
36+
def test_usage_in_constructor(get_contract, val):
37+
code = """
38+
A: immutable(uint256)
39+
a: public(uint256)
40+
41+
42+
@external
43+
def __init__(_a: uint256):
44+
A = _a
45+
self.a = A
46+
47+
48+
@external
49+
@view
50+
def a1() -> uint256:
51+
return A
52+
"""
53+
54+
c = get_contract(code, val)
55+
assert c.a1() == c.a() == val
56+
57+
3558
def test_multiple_immutable_values(get_contract):
3659
code = """
3760
a: immutable(uint256)

vyper/codegen/expr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def parse_Name(self):
326326
is_constructor = self.expr.get_ancestor(vy_ast.FunctionDef).get("name") == "__init__"
327327
if is_constructor:
328328
# store memory position for later access in module.py in the variable record
329-
memory_loc = self.context.new_variable(f"#immutable_{self.expr.id}", var.typ)
329+
memory_loc = self.context.new_variable(self.expr.id, var.typ)
330330
self.context.global_ctx._globals[self.expr.id].pos = memory_loc
331331
# store the data offset in the variable record as well for accessing
332332
data_offset = self.expr._metadata["type"].position.offset

0 commit comments

Comments
 (0)